主要实现的是一个高可用负载均衡web服务器集群,适合lamp架构。
前端使用两台服务器做lvs+keepalived负载调度器,中间可以用N台做apache+php应用服务器,后面使用两台做mysql高可用双机,最后面用了一台虚拟机做文件服务器。
一共开了7个虚拟机。
系统环境:RHEL6.0 kvm虚拟机
lvs调度服务器:192.168.0.1 192.168.0.2
apache服务器:192.168.0.3 192.168.0.4
mysql服务器:192.168.0.7 192.168.0.8
文件服务器:192.168.0.10
-----------------------------------------------------------------------------------------------------------
一、lvs调度服务器
-----------------------------------------------------------------------------------------------------------
Keepalived+lvs
服务器环境:
系统均是:RHEL6.0 内核:2.6.32-71.el6.i686)
Virtual IP:192.168.0.50
Load Balancer:192.168.0.1
Backup:192.168.0.2
Real Server 1:192.168.0.3
Real Server 2:192.168.0.4
主备机上的软件包安装与配置
yum install ipvsadm kernel-devel -y
wget
tar zxf keepalived-1.1.20.tar.gz
cd keepalived1.1.20
./configure prefix=/usr/local/keepalived withkerneldir=/usr/src/kernels/2.6.32-71.el6.i686/
86/
Keepalived configuration
Keepalived version : 1.1.17
Compiler: gcc
Compiler flags: g O2
Extra Lib: lpopt lssl lcrypto
Use IPVS Framework: Yes;注意编译时一定要支持 lvs
IPVS sync daemon support: Yes
Use VRRP Framework: Yes
Use LinkWatch: No
Use Debug flags: No
make
make install
mkdir /etc/keepalived
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/bin/* /bin/
ln -s /usr/local/keepalived/sbin/* /sbin/
vi /etc/rc.local
modprobe ip_vs
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@example.com#接收警报的 email 地址,可以添加多个
}
notification_email_from root@localhost
smtp_server 127.0.0.1#使用本机转发 email
smtp_connect_timeout 30
router_id LVS_DEVEL#load balancer 的标识 ID,用于 email 警报
}
vrrp_instance VI_1 {
state MASTER#备机改为 BACKUP,此状态是由 priority 的值来决定的,当前
priority 的值小于备机的值,那么将会失去 MASTER 状态
interface eth0#HA 监测网络接口
virtual_router_id 50#主、备机的 virtual_router_id 必须相同
priority 150#主机的优先级,备份机改为 50
advert_int 1#主备之间的通告间隔秒数
authentication {
auth_type PASS#主备切换时的验证
auth_pass 1111
}
virtual_ipaddress {
192.168.0.111#HA 虚拟 ip,可加多个
}
}
virtual_server 192.168.0.111 80 {
delay_loop 6#每隔 6 秒查询 realserver状态
lb_algo rr#lvs 调度算法,这里使用轮叫
lb_kind DR#lvs 负载均衡机制,这里使用直连路由
# persistence_timeout 50 #同一 IP 的连接 60 秒内被分配到同一台 realserver
protocol TCP #用 TCP 协议检查 realserver 状态
real_server 192.168.0.1 80 {
weight 1
TCP_CHECK {
connect_timeout 3 #故障重试秒数
nb_get_retry 3 #重试延迟
delay_before_retry 3
}
}
real_server 192.168.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}