0%

keepalived通过检测端口决定vip漂移到哪台机器

安装

yum install keepalived -y

编写检查端口是否通的脚本

#!/bin/bash
CHK_PORT=$1
if [ -n "$CHK_PORT" ];then
        PORT_PROCESS=`ss -lnt|grep $CHK_PORT|wc -l`
        if [ $PORT_PROCESS -eq 0 ];then
                echo "Port $CHK_PORT Is Not Used,End."
                exit 1
        fi
else
        echo "Check Port Cant Be Empty!"
fi

测试:./check_port.sh 8080检测8080端口是否开通.

keepalived主节点配置

! Configuration File for keepalived

global_defs {
   router_id 20.153
}

vrrp_script chk_port {
    script "/etc/keepalived/check_port.sh 8080"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
         chk_port
    }
    virtual_ipaddress {
        172.16.20.99
    }
}

keepalived备节点配置

! Configuration File for keepalived

global_defs {
   router_id 20.154
}

vrrp_script chk_port {
    script "/etc/keepalived/check_port.sh 8080"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
         chk_port
    }
    virtual_ipaddress {
        172.16.20.99
    }
}