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。