keepalived+nginx配置文件及檢查nginx服務的腳本


腳本一啟動的速度要快一些哦,因為腳本二要判斷兩次以后才啟動哎

這兩個一般配合keepalived使用

腳本一:

#!/bin/bash #author:fansik #description:check nginx service run=`ps -C nginx --no-header | wc -l` if [ $run -eq 0 ] then
        /etc/rc.d/init.d/nginx start sleep 3
fi

腳本二:

#!/bin/bash #author:fansik #description:check nginx service count=0
for (( k=0; k<2; k++ )) do check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://localhost/index.html -o /dev/null )
    if [ "$check_code" != "200" ]; then count=$(expr $count + 1) sleep 3 continue else count=0 break fi
done
if [ "$count" != "0" ]; then
   /etc/init.d/nginx start exit 1
else exit 0
fi

keepalived的配置文件/etc/init.d/keepalived.conf:主配置文件如下,從配置文件將MASTER改成BACKUP,雙主模型的時候在添加一個vrrp_instance VI_1主里面變成BACKUP,從里面變成MASTER,virtual_router_id變一下

! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from fanjb@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/nginx1.sh" interval 1 weight -2 fall 2 rise 1 } vrrp_script chk_notify { script "/etc/keepalived/chk_nginx.sh" interval 1 weight -2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.10.10.250 } track_script { chk_nginx chk_notify } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }

通知腳本:/etc/keepalived/nodify.sh

#!/bin/bash #author:fansik #description:check nging status vip=10.10.10.250 contact='root@localhost' notify() { mailsubject="`hostname` to be $1:$vip floating" mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` change to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact } case "$1" in master) notify master exit 0 ;; backup) notify backup exit 0 ;; fault) notify fault exit 0 ;; *) echo "Usage:`basename $0` {master|backup|fault}" exit 1 ;; esac

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM