LVS-DR+keepalived(——实现WEB站负载均衡和高可用)
LVS-DR+Keepalived
——实现WEB站负载均衡和高可用
OS | IP | 子网掩码 | 路由网关 |
Centos6.5 (Directory) (Lvs+keepalived主) | Eth0:192.168.25.209(DIP) | 255.255.252.0 | |
Eth0:0:192.168.26.210(VIP) | 255.255.255.255 | Eth0:0 | |
Centos6.6 (Directory) (Lvs+keepalived备) | Eth0:192.168.26.218 | 255.255.252.0 | 192.168.25.3 |
OS | RS | 子网掩码 | 路由网关 |
Centos6.6 (RS1) | Eth0:192.168.25.210(RIP) | 255.255.252.0 | 192.168.25.3 |
Lo:0:192.168.26.210(VIP) | 255.255.255.255 | Lo:0 | |
Centos6.6 (RS2) | Eth0:192.168.25.211(RIP) | 255.255.252.0 | 192.168.25.3 |
Lo:0:192.168.26.210(VIP) | 255.255.255.255 | Lo:0 | |
Centos6.6 (RS3) | Eth0:192.168.25.212(RIP) | 255.255.252.0 | 192.168.25.3 |
Lo:0:192.168.26.210(VIP) | 255.255.255.255 | Lo:0 | |
Win7 | IP:192.168.25.159(CIP) | 255.255.252.0 | 192.168.25.3 |
配置项参数表:
配置LVS-DR服务器脚本:此处无需使用,因为我已经手动配置主备DR的IP地址(主:192.168.25.209与备:192.168.26.218),若单独只做LSV-DR则直接执行以下脚本即可。
vim /LVS-DR-master.sh
ifconfig eth0 192.168.25.209/22 up
ifconfig eth0:0 192.168.26.210 netmask 255.255.255.255 broadcast 192.168.26.210 up
route add -host 192.168.26.210 dev eth0:0
vim /LVS-DR-slave.sh
ifconfig eth0 192.168.26.218/22 up
ifconfig eth0:0 192.168.26.210 netmask 255.255.255.255 broadcast 192.168.26.210 up
route add -host 192.168.26.210 dev eth0:0
配置RS服务器:
RS1: vim LVS-DR-RS1.sh
ifconfig eth0 192.168.25.210/22 up
route add default gw 192.168.25.3 #指定自己网关
#配置系统内核参数:
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
ifconfig lo:0 192.168.26.210 netmask 255.255.255.255 broadcast 192.168.26.210 up
route add -host 192.168.26.210 dev lo:0
RS2: vim LVS-DR-RS2.sh
ifconfig eth0 192.168.25.211/22 up
route add default gw 192.168.25.3
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
ifconfig lo:0 192.168.26.210 netmask 255.255.255.255 broadcast 192.168.26.210 up
route add -host 192.168.26.210 dev lo:0
RS3:vim LVS-DR-RS3.sh
ifconfig eth0 192.168.25.212/22 up
route add default gw 192.168.25.3
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
ifconfig lo:0 192.168.26.210 netmask 255.255.255.255 broadcast 192.168.26.210 up
route add -host 192.168.26.210 dev lo:0
配置Directory:(安装LVS和keepalived主+备)
安装LVS:
yum install -y ipvsadm
#添加进服务项,设置开机启动,启动服务。
chkconfig --add ipvsadm
chkconfig ipvsadm on
service ipvsadm start
yum install -y ipvsadm
#添加进服务项,设置开机启动,启动服务。
chkconfig --add ipvsadm
chkconfig ipvsadm on
service ipvsadm start
安装keepalived-1.2.23:
tar -zxf keepalived-1.2.23.tar.gz #解压文件
cd keepalived-1.2.23
./configure && make && make install #编译安装
编译通过安装中:
接着执行:
cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ #启动脚本
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived #创建配置文件存放路径
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ #复制配置文件
cp /usr/local/sbin/keepalived /usr/sbin/
chkconfig --add keepalived
chkconfig keepalived on
service keepalived start
编辑keepalived配置文件:
vim /etc/keepalived/keepalived.conf
keepalived.conf:
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.26.210
}
}
virtual_server 192.168.26.210 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.25.210 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.25.211 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.25.212 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
安装成功,192.168.26.218安装过程和方法相同,如果编译安装中缺少什么组件,根据提示安装组件再进行编辑安装即可,这里省略。
安装完成后同样启动Keepalived服务。
附备接点keepalived配置文件:
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.26.210
}
}
virtual_server 192.168.26.210 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.25.210 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.25.211 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.25.212 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
测试VIP网络状态:
打开浏览器测试访问效果:
关闭主接点的Keepalived服务:模拟服务器down机情况进行观察。
网络出现了一点波动:
VIP已经转移到备用接点上:
访问正常:
恢复主接点:
查看VIP:
可以看出恢复主接点,VIP没有自动转移回主接点上,这和我们设置参数有关。
关闭备接点的Keepalived服务:模拟服务器down机情况进行观察,同样网络出现小小波动后恢复
VIP又转移回主接点上了。
网站访问正常: