一、 环境准备 Master-lvs:192.168.0.5 Backup-lvs:192.168.0.10 App-web01:192.168.0.15 App-web02192.168.0.20 VIP:192.168.0.50
二、 部署master-lvs服务器
修改内核参数 vi /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
安装ipvsadm软件
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
ln -s /usr/src/kernels/2.6.18-128.el5-PAE-i686 /usr/src/linux
make
make install
安装Keepalived包
ln -s /usr/src/kernels/2.6.18-128.el5-PAE-i686 /usr/src/linux
wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
tar -xvzf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure --sysconfdir=/etc/ --sbindir=/usr/sbin/
make&make install
修改keepalived.conf
!Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.0.50
}
}
virtual_server 192.168.0.50 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.0.15 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
real_server 192.168.0.20 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
三、 部署Backup-lvs服务器
安装ipvsadm以及keepalived的包
内核参数需要修改如下值:
net.ipv4.conf.lo.arp_ignore = 1
将master-lvs上的配置文件拷贝到Backup-lvs服务器上
在master-lvs上运行如下命令拷贝到backup-lvs上面前提是ipvsadm和keepalived已经安装。
Scp /etc/keepalived/keepalived.conf 192.168.0.10:/etc/keepalived/
Scp /sbin/lvsdr.sh 192.168.0.10:/sbin
可能需要输入密码进行文件的传输
主要修改keepalived的配置文件,标红的位置是需要修改的。
!Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.0.50
}
}
virtual_server 192.168.0.50 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.0.15 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
real_server 192.168.0.20 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
分别在master和backup运行keepalived服务
/etc/init.d/keepalived start
四、 配置后端WEB服务器
在192.168.0.15和192.168.0.20上分别建立如下脚本。
vi /sbin/realdr.sh
#!/bin/bash
VIP=172.16.83.126
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "1">/proc/sys/net/ipv4/conf/default/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/default/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p
配置权限
chmod 755 /sbin/realdr.sh
在两台web服务器上分别执行其指命。
/sbin/realdr.sh start
五、 模拟故障测试
测试keepalived
1 当我把master的lvs服务关掉的时候,会将用户请求自动切换到backup上面进行工作。
停止Master-LVS的keepalived服务,人为造成故障
[root@Master-LVS]# service keepalived stop
这时查看Backup-LVS的log信息
[root@Backup-LVS]# tail -f /var/log/message
?[Copy to clipboard]View Code LOG
Sep 3 11:23:28 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE Sep 3 11:23:29 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE Sep 3 11:23:29 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. Sep 3 11:23:29 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.108.180 Sep 3 11:23:29 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.0.50 added Sep 3 11:23:29 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.0.50 added Sep 3 11:23:34 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.0.50 |
由日志可以看出,Backup-LVS监测到Master-LVS故障后立即将自己的身份切换为Master然后将VIP设置到自己的eth0端口上并发送ARP广播。
现在我手动将Master-LVS的keepalived的服务起来,然后再查看Backup-LVS的log信息
[root@Master-LVS]# service keepalived start
[root@Backup-LVS]# tail -f /var/log/message
?[Copy to clipboard]View Code LOG
Sep 3 11:30:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert Sep 3 11:30:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE Sep 3 11:30:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs. Sep 3 11:30:44 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.0.50 removed Sep 3 11:30:44 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.0.50 removed |
由Backup-LVS的日志可以看到,其检测到比自己优先级高的实例后将自己的身份切换成了Backup,然后将VIP从eth0端口移除,并发送心跳给Master-LVS。
测试lvs
1当我把web服务关掉的时候,lvs上面会显示web消失,当启用后,web会自动显示
Web20关闭后:
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 192.168.0.50:80 wrr
-> 192.168.0.15:80 Route 10 0 0
[root@localhost ~]# ipvsadm -ln
Web20启用后:
root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.50:80 wlc
-> 192.168.0.15:80 Route 1 0 0
-> 192.168.0.20:80 Route 1 0 0