利用脚本方式,实现nginx故障,keepalived服务停掉,vip漂移到备服务器上。
一、取nginx端口数量,已配置的有443 和80 端口
#!/bin/bash
web_info=$(ss -lntup|grep nginx|wc -l)
if [ $web_info -lt 2 ]
then
systemctl stop keepalived
fi
二、脚本添加执行权限
[root@lb1 key]# ll /server/scripts/web_check.sh
-rwxr-xr-x 1 root root 111 Mar 9 15:05 /server/scripts/web_check.sh
三、编写负载均衡配置文件
[root@lb1 key]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script check_web {
script "/server/scripts/web_check.sh" #脚本路径
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
track_script {
check_web #调用脚本
}
}
四、检查测试
[root@lb1 key]# systemctl stop nginx
[root@lb1 key]# systemctl status keepalived.service
0 Comments