keepalive配置

安装

yum install -y keepalived

环境:

10.3.196.31 ap01  
10.3.196.32 ap02  
10.3.196.33 vip  

ap01配置:

! 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 10.3.196.31
   smtp_connect_timeout 30
   router_id zjjg01
script_user root
enable_script_security

}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"    ## 检测nginx状态的脚本路径
    interval 2    ## 检测时间间隔
    weight -20    ## 如果条件成立,权重-20 
}


vrrp_instance VI_1 {
    state MASTER
    interface bond4
    virtual_router_id 33
    mcast_src_ip 10.3.196.31
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }

## 将track_script块加入instance 配置块
    track_script {
        chk_nginx    ## 执行Nginx监控的服务
}

    virtual_ipaddress {
        10.3.196.33
    }
}

ap02配置:

  

! 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 10.3.196.32
   smtp_connect_timeout 30
   router_id zjjg02
   script_user root
enable_script_security
} vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" ## 检测nginx状态的脚本路径 interval 2 ## 检测时间间隔 weight -20 ## 如果条件成立,权重-20 } vrrp_instance VI_1 { state SLAVE interface bond4 virtual_router_id 33 mcast_src_ip 10.3.196.32 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1234 } ## 将track_script块加入instance 配置块 track_script { chk_nginx ## 执行Nginx监控的服务 } virtual_ipaddress { 10.3.196.33 } }

nginx_check.sh  

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

启动

systemctl start keepalived
systemctl enable keepalived
原文地址:https://www.cnblogs.com/litzhiai/p/15562399.html