方法一:killall -0 nginx实现
修改keepalived配置文件
! Configuration File for keepalivedglobal_defs {router_id LVS_141
}
vrrp_script chk_ngx {script "killall -0 nginx"
}
vrrp_instance nginx {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.233.50}track_script {chk_ngx}
}
systemctl start keepalived
systemctl start nginx
方法二:脚本实现
创建脚本:
vim /etc/keepalived/check.sh
脚本:
#!/bin/bashsystemctl status nginx | grep "active(running)" > /dev/nullif [ $? -ne 0 ]; thensystemctl restart nginx &> /dev/nullsleep 1systemctl status nginx | grep "active(running)" > /dev/nullif [ $? -ne 0 ]; thensystemctl stop keepalivedfi
fi
修改keepalived配置文件:
! Configuration File for keepalivedglobal_defs {router_id LVS_141
}
vrrp_script chk_ngx {script "/etc/keepalived/check.sh"
}
vrrp_instance nginx {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.233.50}track_script {chk_ngx}
}
systemctl start keepalived
systemctl start nginx