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

keepalived+nginx高可用配置

时间:2015-05-29 06:21:32      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:keepalived   nginx   webseven   

1.下载:
    wget  http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

2.解压安装:
    yum install ipvsadm
    tar -zxvf keepalived-1.2.15.tar.gz
    cd keepalived-1.2.15
    ./configure --prefix=/usr/local/keepalived-1.2.15  --disable-fwmark --sysconfdir=/etc
    make && make install
    cd /usr/local;
    ln -s keepalived-1.2.15 keepalived
3.配置keepalive:
    [root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

    global_defs {
        lvs_id LVS_nginx
    notification_email {
            test@139.com
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    }
    vrrp_script chk_nginx {
        script "/etc/keepalived/scripts/nginx_check.sh"
        interval 2
    }
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 61
        nopreempt
        priority 101
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass nginx
        }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.16.13/24 dev eth0 label eth0:1
    }
    notify_master "/etc/keepalived/scripts/nginx_script.sh master"
    notify_backup "/etc/keepalived/scripts/nginx_script.sh backup"
    notify_fault  "/etc/keepalived/scripts/nginx_script.sh master"
    }

    [root@localhost keepalived]# cat /etc/keepalived/scripts/nginx_script.sh
    #!/bin/bash
    #auth desheng

    NGINXCMD=/usr/local/nginx/sbin/nginx;
    HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk ‘{print $2}‘`;
    MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
    WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
    VIP_CNT=`ifconfig |grep eth0:1|wc -l`;

    LOGFILE=/var/log/keepalived_nginx_state.log;

    if [ "$HTTP_STATE" == "200" -a "$MASTER_CNT"  -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
        echo "[$1]sucess...">>$LOGFILE
        exit 0;
    else
       sleep 1;
        HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk ‘{print $2}‘`;
        MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
        WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
        VIP_CNT=`ifconfig |grep eth0:1|wc -l`;
        if [ "$HTTP_STATE" == "200" -a "$MASTER_CNT"  -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
            echo "[$1]sucess...">>$LOGFILE
            exit 0;
        elif [ "$VIP_CNT"  -eq "0"  ] ;then
            ps aux|grep ‘nginx: worker process‘|grep -v grep|awk ‘{print "kill -9 "$2}‘ | sh
                ps aux|grep ‘nginx: master process‘|grep -v grep|awk ‘{print "kill -9 "$2}‘ | sh
                $NGINXCMD
                echo "[$1]restart...">>$LOGFILE;
        fi;
    fi;

    [root@localhost keepalived]# cat /etc/keepalived/scripts/nginx_check.sh
    #!/bin/bash
    #auth desheng

    NGINXCMD=/usr/local/nginx/sbin/nginx;
    HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk ‘{print $2}‘`;
    MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
    WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
    VIP_CNT=`ifconfig |grep eth0:1|wc -l`;

    LOGFILE=/var/log/keepalived_nginx_state.log;

    if [ "$HTTP_STATE" -eq "200" -a "$MASTER_CNT"  -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
        echo "[check]sucess...">>$LOGFILE
        exit 0;
    else
        sleep 1;
        HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk ‘{print $2}‘`;
        MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
        WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
        if [  "$HTTP_STATE" -eq "200" -a "$MASTER_CNT"  -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
            echo "[check]sucess...">>$LOGFILE
            exit 0;
        else
            echo "[check]failed...">>$LOGFILE
            exit 1;
        fi;
    fi;
4.测试:
    service keepalived start
    ifconfig
    pkill -9 nginx
    ifconfig
    ps aux|grep nginx

本文出自 “webseven” 博客,请务必保留此出处http://webseven.blog.51cto.com/4388012/1656142

keepalived+nginx高可用配置

标签:keepalived   nginx   webseven   

原文地址:http://webseven.blog.51cto.com/4388012/1656142

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