码迷,mamicode.com
首页 > 其他好文 > 详细

keepalived+nginx配置文件及检查nginx服务的脚本

时间:2017-07-14 13:35:08      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:val   fan   interval   配置   一个   ++   virt   eth0   etc   

原地址:http://www.cnblogs.com/fansik/p/6248684.html

脚本一启动的速度要快一些哦,因为脚本二要判断两次以后才启动哎

这两个一般配合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
技术分享

keepalived+nginx配置文件及检查nginx服务的脚本

标签:val   fan   interval   配置   一个   ++   virt   eth0   etc   

原文地址:http://www.cnblogs.com/wangmo/p/7169329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!