keepalived中腳本不執行的問題

在配置keepalived+nginx環境時,遇到了一個極其caodan的問題,那就是keepalived不執行配置的監控腳本,在網上找了一圈,浪費了我好幾個小時。
如今把解決方法記錄在此,但願能幫到踩坑的你。nginx

腳本調用不成功,日誌一直提示:
Keepalived_vrrp[13096]: Warning - script chk_nginx is not usedide

主配置文件:測試

! Configuration File for keepalived

global_defs {
     router_id keepalived_mjt
     script_user root
     enable_script_security
}

vrrp_script chk_nginx
{
    script /etc/keepalived/chk_ngx.sh
    interval 3
    weight -20
    fall 3
    rise 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.216.200
    }
    track_script{
        chk_nginx
    }
}

備配置文件:日誌

! Configuration File for keepalived

global_defs {
   router_id keepalived_cxq
   script_user root
   enable_script_security

}

vrrp_script chk_nginx {
    script /etc/keepalived/chk_ngx.sh
    interval 3
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.216.200
    }
    track_script{
        chk_nginx
    }
}

監控腳本:code

#!/bin/sh
counter=$(ps -C nginx --no-heading|wc -l)
echo "$counter"
if [ "${counter}" = "0" ]; then
      /usr/sbin/nginx -c /etc/nginx/nginx.conf
      sleep 2
      counter=$(ps -C nginx --no-heading|wc -l)
      if [ "${counter}" = "0" ]; then
          /etc/init.d/keepalived stop
      fi
fi

發現腳本沒有執行時,進行如下檢查:
一、檢查配置文件中的腳本路徑是否正確
二、檢查腳本是否有可執行權限
三、手動測試腳本是否能正常執行router

以上幾個我檢查過了沒有任何問題,還注意到網上說腳本空格什麼的,都檢查了一遍,甚至手寫了一遍腳本,仍是提示腳本is not used。
最後在vrrp_instance VI_1模塊中加上了ip

track_script{
        chk_nginx
    }

重啓以後正常,成千上萬個羊駝奔騰而過。。。。
track_script用於用於追蹤腳本。it

相關文章
相關標籤/搜索