时 间 记 忆
<<  < 2021 - >  >>
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
最 新 评 论
专 题 分 类
最 新 日 志
最 新 留 言
搜 索
用 户 登 录
友 情 连 接
博 客 信 息


 
keepalived高可用,vip不被抢占配置
[ 2021-7-5 15:17:00 | By: chenchaofeng ]
 
keepalived的高可用部署
三台主机为例:
  master:192.128.232.128
  slave01:192.128.232.129
  slave02:192.128.232.130

1. 环境配置:
host1:192.128.232.128
host1:192.128.232.129
host1:192.128.232.130
VIP: 192.128.232.131
1.操作步骤
1. 三台主机都安装keepalived

~]# yum -y install keepalived

2. 监控业务脚本进行判断,迁移vip,
###本测试以nginx服务的80端口作为测试触发判断
[root@localhost ~]# cat /etc/keepalived/check_port.sh 
#!/bin/bash
#使用方法:
#在keepalived的配置文件中
#vrrp_*** check_port {#创建一个vrrp_***脚本,检查配置
#    *** "/etc/keepalived/check_port.sh 28089" #配置监听的端口
#    interval 2 #检查脚本的频率,单位(秒)
#}
CHK_PORT=$1
if [ -n "$CHK_PORT" ];then
         PORT_PROCESS=`ss -lnt|awk -F':' '{print $2}'|awk '{print $1}'|grep "^$CHK_PORT$"|wc -l`
        echo $PORT_PROCESS
        if [ $PORT_PROCESS -eq 0 ];then
                echo "Port $CHK_PORT Is Not Used,End."
                systemctl stop keepalived 
        fi
else
        echo "Check Port Cant Be Empty!"
fi

3.keepalived配置文件
a. 主机192.128.232.128的配置
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id 192.128.232.128
}
vrrp_*** chk_nginx {
    *** "/etc/keepalived/check_port.sh 28089"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP    #全部节点设置BACKUP,以优先级高暂时为master。
    interface eth0      #对应主机的网卡名称
    virtual_router_id 251   #id号要一致
    priority 100
    advert_int 1
    nopreempt     #当优先级高的主机宕机后,再次恢复时,不会抢夺vip,防止业务频繁切换。
    unicast_peer {        #写上其他安装keepalived主机的ip
        192.128.232.129       
        192.128.232.130
    }
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_*** {
         chk_nginx
    }
    virtual_ipaddress {
        192.128.232.131/24
    }
}

b. 主机192.128.232.129的配置
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id 192.128.232.129
}
vrrp_*** chk_nginx {
    *** "/etc/keepalived/check_port.sh 28089"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 251
    priority 95
    advert_int 1
    nopreempt
    unicast_peer {
        192.128.232.128
        192.128.232.130
    }
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_*** {
         chk_nginx
    }
    virtual_ipaddress {
        192.128.232.131/24
    }
}

c. 主机192.128.232.130的配置
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id 192.128.232.130
}
vrrp_*** chk_nginx {
    *** "/etc/keepalived/check_port.sh 28089"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 251
    priority 85
    advert_int 1
    nopreempt
    unicast_peer {
        192.128.232.128
        192.128.232.129
    }
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_*** {
         chk_nginx
    }
    virtual_ipaddress {
        192.128.232.131/24
    }
}

4.启动三台主机的nginx,以及keepalived服务,启动主机顺序是128,129,130。
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# systemctl enable keepalived

5.vip漂移测试过程。
a.vip在128主机上




b.128主机停止nginx,vip会迁移到129,因为129的优先级比130要高。


d. 验证128主机nginx跟keepalived服务都正常,是否会抢夺vip。

 
 
发表评论:
天涯博客欢迎您!