keepalived+nginx

master

global_defs {
   notification_email {
     root@localhost
      }
   notification_email_from keeplived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id test1
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18
}
vrrp_script chk_nginx {
script  "/etc/keepalived/ch_nginx.sh"
interval 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
            10.211.55.99
    }
    track_script {
       chk_nginx
       }
}

 

backup

global_defs {
   notification_email {
     root@localhost
      }
   notification_email_from keeplived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id test1
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18
}
vrrp_script chk_nginx {
script  "/etc/keepalived/ch_nginx.sh"
interval 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
            10.211.55.99
    }
    track_script {
       chk_nginx
       }
}

 

脚本名字ch_nginx.sh

#!/bin/bash
counter=`ps -ef |grep "nginx: master process" |grep -v grep | wc -l`
if [ $counter -eq 0 ];then      
    service nginx restart;
    sleep 5;
    counter=`ps -ef |grep nginx |grep -v grep | wc -l`;
    if [ $counter -eq 1 ];then            
        systemctl stop keepalived;
    fi
fi

 

posted @ 2021-04-26 19:29  huakai201  阅读(72)  评论(0编辑  收藏  举报