KeepAlived防止单点故障
负载均衡器里面,HAProxy比较出名了,但是如果只用一台HAProxy,则会出现单点故障。
这个时候,该KeepAlived出马了。
http://www.cnblogs.com/dkblog/archive/2011/07/06/2098949.html (参考文章)
----------------------------------------------------------------------环境介绍
OS: Ubuntu 12.04
虚拟IP: 192.168.56.2
KeepAlived + HaProxy : 192.168.56.101 (master)
KeepAlived + HaProxy : 192.168.56.102 (backup)
本来应该用HaProxy测试,为了简便,直接用两个redis测试。
后端对应着两个redis 分别安装在192.168.56.101,192.168.56.102上。
----------------------------------------------------------------------
然后101(master)上的配置为
! Configuration File for keepalived global_defs { notification_email { 837500869@qq.com } notification_email_from 837500869@qq.com smtp_server smtp.qq.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth5 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.2 } }
102(backup)的配置为
! Configuration File for keepalived global_defs { notification_email { 837500869@qq.com } notification_email_from 837500869@qq.com smtp_server smtp.qq.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth5 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.2 } }
PS:因为我只需要VIP浮动,负载均衡交给了HAProxy,所以KeepAlived的负载均衡功能没有使用。
分别在101和102上启动KeepAlived ---
启动之前,看看如下:
daemon_mode默认值为0,而赋值的语句如下:
因为我们不需要 start_check_child(); 所以启动参数加上-P即可,前台打日志,则加上 -ln
./bin/keepalived -f ./keepalived/etc/keepalived/keepalived.conf -P -ln
然后看VIP是否可以ping
估计是iptables未容许vrrp协议.
iptables -A INPUT -p vrrp -j ACCEPT 需要保存? iptables -L 是列举出当前的规则 ( )
再尝试看看,还是不行,百度了半天,原来配置文件需要绝对路径。
./bin/keepalived -f /root/keepalived-1.2.8/keepalived/etc/keepalived/keepalived.conf -P -ln
这下可以了
ping的结果:
接下来启动2个redis.
启动101->启动102->启动测试程序->挂掉101->启动101
结果: