一.基本规划
VIP:192.168.1.11
Master:192.168.1.105
Backup:192.168.1.102
二.安装
主备分别安装keepalived
yum -y install keepalived
三.Master配置
#vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER #主
interface eth0 #监视网卡
virtual_router_id 51 #主备保持一致
priority 100 #权重,master大于backup
advert_int 1 #组播信息发送间隔
#验证,主备保持一致
authentication {
auth_type PASS
auth_pass 1111
}
#VIP
virtual_ipaddress {
192.168.1.111
}
}
四.backup配置
#vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER #主
interface eth0 #监视网卡
virtual_router_id 51 #主备保持一致
priority 90 #权重,master大于backup
advert_int 1 #组播信息发送间隔
#验证,主备保持一致
authentication {
auth_type PASS
auth_pass 1111
}
#VIP
virtual_ipaddress {
192.168.1.111
}
}
五.测试
停止192.168.1.105上的keepavlied,vip自动漂移到192.168.1.102;启动192.168.1.105上的keepavlied,则复原
六.完善
停止掉192.168.1.105上的80服务,vip不会自动飘移,为解决该问题.我们在80服务停止时kill掉keepalived.
三.Master配置
#vi /etc/keepalived/keepalived.conf
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #主
interface eth0 #监视网卡
virtual_router_id 51 #主备保持一致
priority 100 #权重,master大于backup
advert_int 1 #组播信息发送间隔
#验证,主备保持一致
authentication {
auth_type PASS
auth_pass 1111
}
#检测脚本
track_script {
check_nginx
}
#VIP
virtual_ipaddress {
192.168.1.111
}
}
check_nginx.sh文件内容如下:
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall -9 keepalived
fi