Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。关于keepalived的讲解,请参考:http://blog.csdn.net/zhu_tianwei/article/details/41174481
一、网络负载均衡拓扑图
虚IP(VIP):192.168.36.200
LVS主IP(DIP):192.168.36.228
LVS备IP(DIP):192.168.36.61
WEB 服务IP(RIP):192.168.36.99
WEB 服务IP(RIP):192.168.36.86
web服务使用nginx做测试,系统环境关闭SElinux 和 iptables。
二、安装keepalived和lvs略
参考:http://blog.csdn.net/zhu_tianwei/article/details/41171785
http://blog.csdn.net/zhu_tianwei/article/details/43278347
三、配置
LVS主keepalived.conf配置:
! Configuration File for keepalived #全局定义部分 global_defs { notification_email { [email protected] #[email protected] ##设置邮件报警地址,可以设置多个,每行一个 } notification_email_from [email protected] #设置邮件的发送地址 smtp_server smtp.163.com #设置smtp server地址 smtp_connect_timeout 30 #设置连接smtp server的超时时间 router_id LVS_DEVEL #表示运行keepalived服务器的一个标示,发邮件时显示在邮件主题中的信息 } #vrrp实例定义部分 vrrp_instance VI_1 { state MASTER #指定keepalived的角色,备机改成 BACKUP interface eth0 #指定HA检测网络的端口 virtual_router_id 51 #虚拟路由表示,这个表示是一个数字,同一个vrrp实例使用唯一的标示 priority 100 #定义优先级,数字越大,优先级越大,在同一个vrrp_instance下,MASTER的优先级必须高于BACKUP advert_int 1 #设定MASTER和BACKUP负载均衡器之间的同步检查的时间间隔,单位是秒 authentication { auth_type PASS #设定验证类型,主要有PASS和AH两种 auth_pass 1111 #设置密码,在同一个vrrp_instance下,MASTER和BACKUP必须使用相同的密码才能正常通信 } virtual_ipaddress { #设置虚拟IP地址,可以为多个虚拟IP地址,每行一个 192.168.36.200 } } #虚拟服务器定义部分 virtual_server 192.168.36.200 80 { #设置虚拟服务器,需要指定虚拟IP地址和服务器端口,IP与端口之间用空格隔开 delay_loop 6 #设置运行情况检查时间,单位是秒 lb_algo wrr #设置负载调度算法,这里是rr,即轮询算法 lb_kind DR #设置LVS 实现负载均衡的调度机制,有NAT 、TUN和DR三个模式 persistence_timeout 1 #会话保持时间,单位是秒 protocol TCP #指定转发协议的类型 #配置web,RIP real_server 192.168.36.86 80 { weight 10 #服务节点的权值 TCP_CHECK { #realserver的状态检测部分,单位是秒 connect_timeout 3 #表示三秒无响应超时 nb_get_retry 3 #表示重试次数 delay_before_retry 3 #表示重试间隔 connect_port 80 #连接的端口 } } real_server 192.168.36.99 80 { weight 10 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }LVS备机配置略。
在web服务机器上启动lvs配置脚本 rip_server.sh 。
#!/bin/sh VIP=192.168.36.200 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up #/sbin/route add -host $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 "Real Server start ok!" ;; stop) ifconfig lo:0 down #route del $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 "Real Server stoped!" ;; status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0 | grep $VIP` isrothere=`netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ];then # Either the route or the lo:0 device # not found. echo "real server Stopped." else echo "Real Server Running." fi ;; *) echo "Usage:$0 {start|stop|status}" exit 1 esac四、启动测试
keepalived 启动关闭脚本:
service keepalived start
service keepalived stop
service keepalived restart
web服务机器上启动关闭lvs
rip_server.sh start|stop
把ipvsadm和keepalived加入开机自启动:
#cat /etc/rc.local
ipvsadm
/etc/init.d/keepalived start
五、测试切换
1)关闭主keepalived,我们可看到流量走到备机上去:watch /sbin/ipvsadm -l -n
Every 2.0s: /sbin/ipvsadm Fri Feb 6 21:10:33 2015 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.36.200:http wrr persistent 1 -> 192.168.36.86:http Route 10 0 8 -> 192.168.36.99:http Route 10 0 0
检查VIP绑定:ip add |grep 192.168.36.200
2)关闭36.99web服务 :watch /sbin/ipvsadm -l -n
Every 2.0s: ipvsadm Fri Feb 6 21:13:37 2015 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.36.200:http wrr persistent 1 -> 192.168.36.86:http Route 10 0 0我们发现36.99不见了,请求都访问到36.86上。
3)通过日志查看服务变化
tail -f /var/log/messages (关闭主lvs的日志)
主lvs:
Feb 6 21:27:21 localhost Keepalived[11566]: Stopping Keepalived v1.2.13 (02/06,2015) Feb 6 21:27:21 localhost Keepalived_vrrp[11569]: VRRP_Instance(VI_1) sending 0 priority Feb 6 21:27:21 localhost Keepalived_vrrp[11569]: VRRP_Instance(VI_1) removing protocol VIPs. Feb 6 21:27:21 localhost avahi-daemon[1462]: Withdrawing address record for 192.168.36.200 on eth0. Feb 6 21:27:21 localhost Keepalived_healthcheckers[11568]: Removing service [192.168.36.86]:80 from VS [192.168.36.200]:80备lvs:
Feb 6 21:27:29 localhost Keepalived_vrrp[11637]: VRRP_Instance(VI_1) Entering MASTER STATE Feb 6 21:27:29 localhost Keepalived_vrrp[11637]: VRRP_Instance(VI_1) setting protocol VIPs. Feb 6 21:27:29 localhost Keepalived_healthcheckers[11636]: Netlink reflector reports IP 192.168.36.200 added Feb 6 21:27:29 localhost avahi-daemon[1451]: Registering new address record for 192.168.36.200 on eth0.IPv4. Feb 6 21:27:29 localhost Keepalived_vrrp[11637]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.36.200 Feb 6 21:27:34 localhost Keepalived_vrrp[11637]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.36.200
参考文章:
LVS管理与维护:http://www.91linux.com/html/2014/Linux_Server_0327/9367.html
LVS+keepalived负载均衡:http://wangyu.iteye.com/blog/424057
lvs+keepalived配置高可用高性能集群服务:http://wenku.baidu.com/view/f4803b30b4daa58da0114ac7.html
LVS+Keepalived实现高可用集群:http://wenku.baidu.com/view/036e876c168884868762d688.html
lvs+keepalived使用总结 :http://wenku.baidu.com/view/9d129e550b4c2e3f57276375.html
LVS+KEEPALIVED+NAT详解:http://blog.chinaunix.net/uid-27571599-id-3733849.html
企业级WEB的负载均衡高可用之LVS+Keepalived:http://network.51cto.com/art/201006/206831_all.htm
CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡:http://www.cnblogs.com/mchina/archive/2012/08/27/2644391.html
LVS+Keepalived 高可用性负载均衡自动化配置:http://wgkgood.blog.51cto.com/1192594/605322