keepalived启停脚本+配置文件

keepalived启停脚本

#!/bin/bash
# keepalived 家目录
KP_HOME=/xxx/xxx/keepalived
# keepalived 运行脚本路径
KP_CMM_HOME=$KP_HOME/sbin/keepalived
# keepalived 配置文件路径
KP_CONF_HOME=$KP_HOME/etc/keepalived/keepalived.conf
# 所在主机接口名
INTERFACE_NAME=eth0
# keepalived VIP 网段
KEEPALIVED_IP_NAT=10.1.1.11/24

start() {
        KA=`pidof keepalived | wc -l`
if [ $KA -eq 0 ];then
            $KP_CMM_HOME -f $KP_CONF_HOME
            KA=`pidof keepalived | wc -l`
if [ $KA -eq 0 ];then
                   echo -e "keepalived is starting ...  【\033[31mFailed\033[0m】"
                   echo "PLS check the privileges or path!"
                else
                   echo -e "keepalived is starting ...【\033[32mOK\033[0m】"  
                fi
         else
    echo -e "keepalived is starting ...【\033[31mFailed\033[0m】"
            echo "keepalived is already started!"

fi
}
stop(){
    KA=`pidof keepalived | wc -l`
    if [ $KA -eq 0 ];then
        VIP=`ip a | grep $KEEPALIVED_IP_NAT | wc -l`
        if [ $VIP -ne 0 ];then
            ip add del $KEEPALIVED_IP_NAT dev $INTERFACE_NAME

        fi
        echo -e "keepalived is stopping ...【\033[31mFailed\033[0m】"
        echo "keepalived is already stopped!"
    else
        pidof keepalived | xargs kill -9 
        pidof keepalived | xargs kill -9
        KA=`pidof keepalived | wc -w`
        if [ $KA -eq 0 ];then
            VIP=`ip a | grep $KEEPALIVED_IP_NAT | wc -l`
            if [ $VIP -ne 0 ];then
                ip add del $KEEPALIVED_IP_NAT dev $INTERFACE_NAME
            fi
            echo -e "keepalived is stopping ...【\033[32mOK\033[0m】"
        else
            echo -e "keepalived is stopping ...【\033[31mFailed\033[0m】"
            echo "PLS retry again!"  
        fi
    fi
}
restart(){
      stop;
      sleep 2
      start;
}
status(){
     KA=`pidof keepalived | wc -w`
      if [ $KA -eq 0 ];then
        echo "keepalived is stopped!"
      elif [ $KA -eq 3 ];then 
        echo "keepalived is running!"
      else 
        echo "Lacking of process! PLS restart keepalived! "
      fi
}
case "$1" in
    start) 
         start;;
    stop)
         stop;;
    restart)
         restart;;
    status)
         status;;
    *)
       echo $"Usage: $0 {start|stop|status|restart}"
esac

keepalived 配置文件

! Configuration File for keepalived

vrrp_script check_nginx_alive {
    #nginx进程监测脚本
    script "/xxxx/nginx_check/check_nginx_alive.sh"
    interval 2
    weight -20
}
global_defs {
  # notification_email {
     #[email protected]
     #[email protected]
     #[email protected]
   #}

 #notification_email_from [email protected]
   #smtp_server xxx.xx.xx.x
   #smtp_connect_timeout 30
#   router_id LVS_DEVEL_UNICOM
   router_id LVS_DEVEL_TELECOM
   vrrp_skip_check_adv_addr
   #vrrp_strict
   #vrrp_garp_interval 0
   #vrrp_gna_interval 0
}
vrrp_instance VI_1 {
#    state BACKUP
# 主备keepalived配置
    state MASTER
    # 接口名
    interface eth0
    #virtual_router_id 112
    # 集群路由编号
    virtual_router_id 122
    # 优先级
    priority 80
    nopreempt
    advert_int 1  
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # VIP 
    virtual_ipaddress {

        #10.1.1.12/24
        10.1.1.11/24 
        #TELECOM LINE
    }
    track_script {
        check_nginx_alive
    }
}

你可能感兴趣的:(常用接口,服务器,网络,运维,linux,shell)