keepalived检测nginx脚本来实现自动漂移VIP
1、keepalived配置脚本A(主):
global_defs {
router_id NodeA
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
英各披 state MASTER
interface ens33
virtual_router_id 50
priority 250
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.98.100
}
}
2、keepalived配置脚随闲调秤本B(备):
global_defs {
router_id NodeB
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 50
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.98.100
}
}
1、利用yum装好nginx填写好相关文件确定其正在运行中.
1、在路径 /etc/keepalived/nginx_check.sh 建立nginx脚本。
内容如下:
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl stop keepalived
fi
检测nginx停止立马停止keepalived,使VIP进行漂移。
2、手动运行
sh /etc/keepalived/nginx_check.sh
确认手动运行脚本没有出现报错。
3、停止nginx服务看keepalived的效果。