keepalived检测nginx脚本
当主节点Nginx挂掉,keepalived仍然存活时,此时无法访问主节点,因此需要检测Nginx状态,配置自动重启。
一、新增Nginx状态监测与重启脚本
[root@server01 ~]# cat /etc/keepalived/nginx_check.sh #!/bin/bash #Author:Mr.Ding #Mail:1490176741@qq.com #Created Time:2023-06-18 18:40:37 #Name:/etc/keepalived/nginx_check.sh #Description: #Ver: num=`ps -C nginx --no-header | wc -l` if [ $num -eq 0 ];then systemctl start nginx sleep 1 if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then systemctl stop keepalived fi fi
给脚本添加权限
chmod +x /etc/keepalived/nginx_check.sh
二、配置keepalived核心文件,定时执行脚本
[root@server01 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 1270.0.01
smtp_connect_timeout 30
router_id lb02
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
# 引入脚本
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 1 # 设置执行间隔
weight 10 # 运行成功则升级权重+10,否则-10
}
vrrp_instance VI_1 {
state MASTER
interface ens34
virtual_router_id 55
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.24/24 dev ens34 lable ens34:1
}
# 在实例中追踪nginx状态
track_script {
chk_nginx
}
}
重启keepalived服务,使配置生效。
systemctl restart keepalived.service
******************************我也想难过的时候到海边走走,可是我的城市没有海。******************************