美文网首页
Nginx+keepalived

Nginx+keepalived

作者: 六弦极品 | 来源:发表于2024-05-21 18:05 被阅读0次

    一、keepalived安装

    yum install keepalived -y
    

    二、keepalived配置

    /etc/keepalived/keepalived.conf

    1、主配置文件

    ! Configuration File for keepalived
    
    global_defs {
       router_id tianma157 ## 路由器标识符,每一个实例唯一标识
       script_user root
    }
    
    ## 检查脚本
    vrrp_script health-check {
       script   "/etc/keepalived/script/health_check.sh 10.110.13.158" ## 另一台keepalived的ip
       interval 5 ## 检查脚本的间隔时间,单位是秒
       fall     3
       rise     5
    }
    
    vrrp_instance NG_CONFIG_KEEP {
        state BACKUP
        interface ens192 ##网卡名
        virtual_router_id 230 ## 虚拟路由器标识符,VIP 后一段
        priority 100
        advert_int 3
        nopreempt
        garp_master_delay 30
    
        authentication {
            auth_type PASS
            auth_pass tm_keepalived_230 ## 集群通信密码,后数据为VIP 后一段
        }
    
        track_interface {
            ens192  ## 本机网卡名
        }
    
        virtual_ipaddress {
            10.110.13.230/24 dev ens192  ## VIP和对应的网卡名
        }
    
        track_script {
            health-check
        }
    }
    

    2、从配置文件

    ! Configuration File for keepalived
    
    global_defs {
       router_id tianma158 ## 路由器标识符,每一个实例唯一标识
       script_user root
    }
    
    ## 检查脚本
    vrrp_script health-check {
       script   "/etc/keepalived/script/health_check.sh 10.110.13.157" ## 另一台keepalived的ip
       interval 5 ## 检查脚本的间隔时间,单位是秒
       fall     3
       rise     5
    }
    
    vrrp_instance NG_CONFIG_KEEP {
        state BACKUP
        interface ens192 ##网卡名
        virtual_router_id 230 ## 虚拟路由器标识符,VIP 后一段
        priority 95
        advert_int 3
        nopreempt
        garp_master_delay 30
    
        authentication {
            auth_type PASS
            auth_pass tm_keepalived_230 ## 集群通信密码,后数据为VIP 后一段
        }
    
        track_interface {
            ens192  ## 本机网卡名
        }
    
        virtual_ipaddress {
            10.110.13.230/24 dev ens192  ## VIP和对应的网卡名
        }
    
        track_script {
            health-check
        }
    }
    

    三、检测脚步

    /etc/keepalived/script/health_check.sh

    1、创建脚步目录

    mkdir /etc/keepalived/script
    chmod +x /etc/keepalived/script/health_check.sh
    

    2、检测脚步

    #!/bin/bash
    
    # -------------------------------------------------------
    # $Name:         health_check.sh
    # $Version:      v1.0
    # $Function:     keepalved检测逻辑
    # $Author:       Liuzhousheng
    # $Create Date:  2020-5-28 15:15
    # $Description:  判断在什么情况下VIP在A/B二台主机上漂移
    #                100 master
    #                 95 slave
    # -------------------------------------------------------
    
    
    health_check()
    {
        local datetime=$(date "+%F %T")
        local logfile='/var/log/keepalived-check.log'
        local keep_conf="/etc/keepalived/keepalived.conf"
        local role=$(grep 'priority' $keep_conf |grep -v '#' |awk '{print $2}')
        local nginx_status=$(netstat  -ntlp |grep -w 80|grep nginx |grep -v grep |wc -l)
        local vip=$(cat $keep_conf |grep 'dev' |sed -r 's!\s+([^/]+)/.*!\1!')
        local local_has_vip=$(ip addr show ens192 |grep -c "$vip")
        local gateways=$(ip r l |awk '/^def|via/ {print $3}' |egrep '^(10|192|172)\.'|sort |uniq)
        local another_keep_ip=$1
    
        # 根据权重定义本地keepalived角色
        if [ "$role" == "100" ]; then
            role="master"
        elif [ "$role" == "95" ]; then
            role="slave"
        fi
    
        # 检查server进程是否存活
        ha_msg="$datetime Nginx is shutdown"
        if [ $nginx_status -eq 0 ]; then
            echo -e "${ha_msg}. $local_has_vip" >>$logfile
            exit 1
        fi
    
    
        # 检查本地网络是否异常
        network_msg="$datetime Gateway is unreachable"
        for gateway in $gateways $another_keep_ip; do
            ping -W 1 -c 1 $gateway >/dev/null 2>&1
            if [ $? -ne 0 ]; then
                echo "${network_msg}, ${gateway}. $local_has_vip" >>$logfile
                stats="${stats}1"
            fi
        done
        if [ "$stats" == "11" ]; then
            exit 2
        fi
    
        return 0
    }
    
    health_check $1
    

    相关文章

      网友评论

          本文标题:Nginx+keepalived

          本文链接:https://www.haomeiwen.com/subject/mquafjtx.html