keepalived 一般用于实现双机热备,两台服务器提供服务,当其中一台挂掉后,平滑切换到另一个服务上。对外提供一个虚拟IP。
环境准备:
主机: 192.168.5.154 --- nginx负载均衡 --- tomcat 集群
备机: 192.168.5.158 --- nginx负载均衡 --- tomcat 集群
vip: 192.168.5.99
一. 安装
wget http://www.keepalived.org/software/keepalived-1.2.17.tar.gz
tar -xvf keepalived-1.2.17.tar.gz
cd keepalived-1.2.17
./configure --prefix=/usr/local/keepalived
make && make install
32位的linux上编译出错:configure: error: No SO_MARK declaration in headers,添加 --disable-fwmark 选项解决
二. 配置
./usr/local/keepalived/sbin/keepalived 传统方式启动后,会生成三个keepalived进程,关闭时太复杂了。为便于使用,将其做成服务。
cp /usr/local/keepalived/sbin/keepalived /usr/sbin cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
修改系统配置:
vi /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS = "-D -d -S 0 -f /usr/local/keepalived/etc/keepalived/keepalived.conf" #指定配置文件路径,-d -S 选项打开调试日志
主机:keepalived.conf
global_defs { notification_email { root@localhost } notification_email_from root@localhost #不需要邮件通知,直接默认本机接收 smtp_server localhost smtp_connect_timeout 30 router_id LVS_DEVEL #默认即可 } vrrp_script check_run { #check_run 后面要跟一个空格+{ script "/usr/local/nginx/check_run.sh" #指定检测脚本路径 interval 2 weight 2 } vrrp_instance VI_1 { state MASTER #主机 interface eth0 #监听网卡eth0 virtual_router_id 52 #路由id,必须与备机一致。运行可能会报错( receive an invalid ip number count associated with VRID! ),更换成其它值即可 priority 100 #优先级,比备机高 advert_int 1 authentication { auth_type PASS #启用密码 auth_pass 1111 #密码必须与备机一致 } track_script { check_run #指定监控脚本。注:配置出错时,keepalived不会退出,只会卡住,一定要检查配置 } virtual_ipaddress { 192.168.5.99 #对外虚拟IP列表,可配置多个 } }
备机:keepalived.conf
vrrp_instance VI_1 { state BACKUP #备机 interface eth0 virtual_router_id 52 #值与主机一致 priority 90 #优先级比主机低 advert_int 1 authentication { auth_type PASS auth_pass 1111 #与主机密码相同 } track_script { check_run } virtual_ipaddress { 192.168.5.99 #虚拟IP与主机一致 } }
check_run.sh nginx运行检测脚本,运行正常返回0,失败返回1,每轮检测2次。
#!/bin/sh CHECK_TIME=2 #检测2次 check(){ curl -m2 http://localhost > /dev/null 2>&1 return $? } while [ $CHECK_TIME -ne 0 ] do let "CHECK_TIME-=1" check NGINX_OK=$? if [ $NGINX_OK -eq 0 ]; then exit 0 fi if [ $NGINX_OK -ne 1 ] && [ $CHECK_TIME -eq 0 ]; then exit 1 fi done
三. 运行
启动与关闭:
service keepalived start
service keepalived stop
启动keepalived后,运行 ip a ,发现生成了vip ,关闭后vip被删除。
[root@test nginx]# ip a 2: eth0:mtu 1500 qdisc mq state UP qlen 1000 link/ether 0c:ca:ba:aa:ce:6a brd ff:ff:ff:ff:ff:ff inet 192.168.5.158/24 brd 192.168.5.255 scope global eth0 inet 192.168.5.99/32 scope global eth0 inet6 fe80::ec4:aaff:fe43:ee6d/64 scope link valid_lft forever preferred_lft forever
测试:
http://192.168.5.99 显示正常页面,killall nginx,查看日志 /var/log/messages 发现主机虚拟IP被移除,备机日志显示切换为主机状态
Jun 29 09:56:09 localhost Keepalived_vrrp[19520]: VRRP_Script(check_run) failed Jun 29 09:56:09 localhost Keepalived_vrrp[19520]: VRRP_Instance(VI_1) Entering FAULT STATE Jun 29 09:56:09 localhost Keepalived_vrrp[19520]: VRRP_Instance(VI_1) removing protocol VIPs. Jun 29 09:56:09 localhost Keepalived_vrrp[19520]: VRRP_Instance(VI_1) Now in FAULT state Jun 29 09:56:09 localhost avahi-daemon[2842]: Withdrawing address record for 192.168.5.99 on eth0. Jun 29 09:56:09 localhost Keepalived_healthcheckers[19518]: Netlink reflector reports IP 192.168.5.99 removed
Jun 29 09:52:13 gw-1 Keepalived_vrrp[12619]: VRRP_Script(check_run) succeeded Jun 29 09:56:10 gw-1 Keepalived_vrrp[12619]: VRRP_Instance(VI_1) Transition to MASTER STATE Jun 29 09:56:10 gw-1 Keepalived_vrrp[12619]: VRRP_Instance(VI_1) Entering MASTER STATE Jun 29 09:56:10 gw-1 Keepalived_vrrp[12619]: VRRP_Instance(VI_1) setting protocol VIPs. Jun 29 09:56:10 gw-1 Keepalived_vrrp[12619]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.5.99 Jun 29 09:56:10 gw-1 avahi-daemon[3351]: Registering new address record for 192.168.5.99 on eth0.IPv4. Jun 29 09:56:10 gw-1 Keepalived_healthcheckers[12618]: Netlink reflector reports IP 192.168.5.99 added Jun 29 09:56:15 gw-1 Keepalived_vrrp[12619]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.5.99
访问 http://192.168.5.99 依旧正常