Keepalibed监控nginx

配置Keepalived监控nginx

--wang

目的:

通过Keepalived实现对nginx的监控,每两秒扫描一次,如果nginx关闭,尝试重启nginx,两秒后检查nginx是否启动,如果还没有启动,就关闭Keepalived

配置文件: 

/etc/keepalived/keepalived.conf

注意事项:

脚本一定要开启执行权限chmod  +x  /root/shell/nginx_check.sh

脚本内容:

[root@CRS_LH_LoadB ~]# vim /root/shell/nginx_check.sh

#!/bin/bash

time=`date '+%Y%m%d %H%M%S'`

#echo "${time}test script">>/root/shell/test.txt #用于判断脚本是否正常执行,可以注释掉

systemctl status nginx>>/dev/null

if [ $? -ne 0 ] ; then

        systemctl start nginx

        sleep 2

        if [ $? -ne 0 ] ; then

                systemctl stop keepalived>>/dev/null

        fi

fi

 

实际配置文件:

主:

global_defs {

        router_id Haproxy_HA_m

}

vrrp_script chk_nginx {

        script "/root/shell/nginx_check.sh"

        interval 2

}

vrrp_instance VI_1 {

    state MASTER

    interface ens160

    virtual_router_id 58

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.100.180

 

}

track_script {     #调用脚本

       chk_nginx

    }

}

备:

global_defs {

        router_id Haproxy_HA_b

}

 

vrrp_script chk_nginx {

        script "/root/shell/nginx_check.sh"

        interval 2

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens160

    virtual_router_id 58

    priority 80

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.100.180

}

track_script {     #调用脚本

       chk_nginx

    }

}

 

posted @ 2019-03-20 11:06  水中鱼2018  阅读(241)  评论(0编辑  收藏  举报