centos7配置keepalived检测nginx
1、分别在多台服务器上安装keepalived
yum install -y keepalived
2、分别在多台服务器上安装nginx
yum install -y nginx
3、配置keepalived(主从服务器分别填写自己的ip和权重)
!Configuration File for keepalived global_defs { #一个没重复的名字即可 router_id 1 } # 检测nginx是否运行 vrrp_script chk_nginx { script "/etc/keepalived/chk_nginx.sh" interval 2 weight -5 fall 3 } vrrp_instance VI_1 { # 此处不设置为MASTER,通过priority来竞争master state MASTER # 网卡名字,文章下方会给出如何获取网卡名字的方法 interface ens32 # 同一个keepalived集群的virtual_router_id相同 virtual_router_id 51 # 权重,master要大于slave priority 100 # 主备通讯时间间隔 advert_int 1 # 本机ip mcast_src_ip 192.168.81.128 # 主备保持一致 authentication { auth_type PASS auth_pass 123456 } # 与上方nginx运行状况检测呼应 track_script { chk_nginx } virtual_ipaddress { # 虚拟ip地址(VIP,一个尚未占用的内网ip即可) 192.168.81.180 } }
4、编写检测配置文件
vim /etc/keepalived/chk_nginx.sh
#!/bin/sh counter=$(ps -C nginx --no-heading | wc -l) if [ "${counter}" = 0 ] ;then
systemctl start nginx
fi
5、启动nginx和keepalived
systemctl start nginx
systemctl start keepalived
注意:可能出现检测脚本不执行
可能的原因是selinux和防火墙没有关闭
setenforce 0
vim /etc/selinux/config
将ELINUX的值修改为
ELINUX=disabled