Server1:

#! Configuration File for keepalived
 global_defs {

  notification_email {

      [email protected]

 }

    notification_email_from [email protected]

    smtp_server 127.0.0.1

    smtp_connect_timeout 30

    router_id LVS_DEVEL

 }

 vrrp_script chk_nginx {

    script "/var/shell/check_nginx.sh"

    interval 2

    weight 2

 }


 vrrp_instance VI_1 {

     state BACKUP

     interface eth0

     lvs_sync_daemon_inteface eth0

     virtual_router_id 151

     priority 100

     advert_int 1
     nopreempt    #设置不抢占

     authentication {

         auth_typePASS

         auth_pass  1111

     }

     virtual_ipaddress {

         XXX.XXX.XXX.XXX

     }

     track_script {

     chk_nginx

    }

 }



Server2:

#! Configuration File for keepalived

 global_defs {

  notification_email {

      [email protected]

 }

    notification_email_from [email protected]

    smtp_server 127.0.0.1

    smtp_connect_timeout 30

    router_id LVS_DEVEL

 }

 vrrp_script chk_nginx {

    script "/var/shell/check_nginx.sh"

    interval 2

    weight 2

 }



 vrrp_instance VI_1 {

     state BACKUP

     interface eth0

     lvs_sync_daemon_inteface eth0

     virtual_router_id 151

     priority 201

     advert_int 1
     nopreempt    #设置不抢占


     authentication {

         auth_typePASS

         auth_pass  1111

     }

     virtual_ipaddress {

         XXX.XXX.XXX.XXX

     }

     track_script {

     chk_nginx

    }

 }


检测脚本

check_nginx.sh

#!/bin/bash

#auto check nginx  process
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
        /data/nginx/sbin/nginx
        sleep 3
        if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                  /etc/init.d/keepalived stop
        fi
fi