这边说下persistence_timeout 选项,意思就是在这个一定时间内会讲来自同一用户(根据ip来判断的)route到同一个real
server。我这边给注释掉了。具体根据业务需求,长连接的话最好是配置上,配置值最好跟lvs的配置的timeout一致。
启动keepalived
编写start.sh(stop.sh,restart.sh)脚本方便启动
#!/bin/sh
/etc/init.d/keepalived start
执行脚本
[root@lvs work]# ./start.sh
Starting keepalived: [ OK ]
编写检测脚本watch.sh
#!/bin/sh
watch 'ipvsadm -l -n'
启动检测
[root@lvs work]# ./watch.sh
Every 2.0s: ipvsadm -l -n Tue May 6 12:49:52 2014
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.3.199:80 rr persistent 50
-> 172.16.3.91:80 Route 50 0 0
-> 172.16.3.92:80 Route 50 0 0
可以看到已经检测到172.16.3.91, 172.16.3.92两台服务器。
在Slave DR上做同样配置和脚本。
2.在Real Server1和Real Server2安装nginx
安装nginx过程省略。
安装完nginx之后,需要启动nginx。
配置 realserver.sh脚本
#!/bin/bash
SNS_VIP=172.16.3.199
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
~
启动脚本
[root@rs-1 work]# ./realserver.sh start
RealServer Start
执行ifconfig,可以看到做往常多了一段下图红框内的内容。
测试
在Slave DR上测试
[vagrant@centos-5 conf]$ for((i=0;i<100;i++));do curl 172.16.3.199;done;
或者用webbench模拟并发请求
[vagrant@centos-5 conf]$ webbench -c 10 -t 10
在Master DR上执行watch.sh
Every 2.0s: ipvsadm -l -n Wed May 7 11:45:27 2014
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.3.199:80 rr
-> 172.16.3.91:80 Route 50 0 1763
-> 172.16.3.92:80 Route 50 0 1762
整个配置过程,记得关闭所有虚拟机的防火墙, 这点很重要!!!
[root@lvs work]# service iptables stop
如下命令可查询是否已经关闭防火墙
[root@lvs work]# chkconfig --list | grep iptables
iptables 0:off1:off2:off3:off4:off5:off6:off