目 录CONTENT

文章目录

2023-02-27 安装keeplived 做vrrp高可用

所念皆星河
2020-05-06 / 0 评论 / 0 点赞 / 20 阅读 / 5879 字

安装keeplived

yum install keepalived -y

配置keeplived防止脑裂脚本(多台)

vi /etc/keepalived/check_port.sh  两个脚本任一都可以,第一个脚本keeplive的配置文件脚本后带端口号,第二个脚本无需。
#!/bin/bash
#vrrp_script check_port {#创建一个vrrp_script脚本,检查配置
#    script "/etc/keepalived/check_port.sh 80" #配置监听的端口
#    interval 2 #检查脚本的频率,单位(秒)
#  
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
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "端口号"|wc -l)
if [ "${counter}" -eq 0 ]; then
    /etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/check_port.sh

配置主机本机一IP:10.4.7.11 vip:10.4.7.10

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {

        router_id 10.4.7.11

}

vrrp_script chk_nginx {
        script "/etc/keepalived/check_port.sh 80"
        interval 2
        weight -20

}

vrrp_instance VI_1 {
        state MASTER
        interface ens32
        virtual_router_id 251     #同一家族要一致,同一个集群id一致
        mcast_src_ip 10.4.7.11    #本地IP
        priority 100     # 优先级决定是主还是备    越大越优先
        advert_int 1     #通讯间隔
        nopreempt

        authentication {
                auth_type PASS
                auth_pass 11111111

        }

        track_script {
                chk_nginx

        }

        virtual_ipaddress {
                10.4.7.10      #虚拟VIP

        }

}

配置主机本机二IP:10.4.7.12 vip:10.4.7.10

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
        router_id 10.4.7.12

}

vrrp_script chk_nginx {
        script "/etc/keepalived/check_port.sh 7443"
        interval 2
        weight -20

}

vrrp_instance VI_1 {
        state BACKUP
        interface ens32
        virtual_router_id 251
        mcast_src_ip 10.4.7.12
        priority 90
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 11111111

        }

        track_script {
                chk_nginx

        }

        virtual_ipaddress {
                10.4.7.10

        }

}

所有主机启动keeplived

启动keeplived

systemctl start keepalived.service
systemctl enable keepalived.service

查看vip 并且测试

lpp6kkbs.png
lpp6ksfv.png

补充篇keeplive和Haproxy高可用检查keeplive配置

安装killall软件

yum install psmisc -y

keeplive配置检查haproxy进程是否存在

vrrp_script check_haproxy {
    script "killall -0 haproxy"
    interval 2
    weight -30 #使用 killall -0 haproxy 命令检查所在节点的 haproxy 进程是否正常。如果异常则将权重减少(-30),从而触发重新选主过程;
    }
    track_script {
    check_haproxy
    }
0

评论区