Centos7-keepalived高可用配置+nginx自愈

1、安装keepalived (每台服务器都要装)
yum -y install keepalived

2、修改keepalived配置文件
vim /etc/keepalived
! Configuration File for keepalived

global_defs {
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"    # 检测 nginx 状态的脚本路径
    interval 1        # 每隔1秒执行一次脚本
    weight -20        # 如果条件成立,权重-20
}

vrrp_instance VI_1 {
    state MASTER
    interface eno16777728
    virtual_router_id 51
    priority 90
    advert_int 1
    nopreempt        #设置 nopreempt 防止抢占资源
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        192.168.10.100    #vip地址
    }
}

3、添加nginx检测脚本
vim /etc/keepalived/nginx_check.sh
#!/bin/bash

A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ];then
/usr/sbin/nginx
sleep 2        #等待2秒后,发现nginx还没起来,直接关掉keepalived,使得vip能飘走
if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
    /usr/bin/systemctl stop keepalived
fi
fi

4、启动keepalived,并配置开机自启
systemctl restart keepalived && systemctl enable keepalived

你可能感兴趣的:(Centos7-keepalived高可用配置+nginx自愈)