0. 背景
局域网内两台主机
192.168.1.16 主节点
192.168.1.43 备节点
分别在 9439 端口提供相同的服务,现在使用 keepalived
配置 HA。
1. 安装keepalived
分别在两台主机安装 keepalived
及相关依赖包
yum install -y net-tools psmisc
yum install -y net-snmp
yum install -y ipvsadm keepalived
2. 配置 keepalived
(1)配置主节点192.168.1.16
新建文件配置文件/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script check_docker_losa {
# 自身状态检测
script "sh /etc/keepalived/chk_losa.sh"
interval 3
weight 5
}
vrrp_instance VI_1 {
# 初始化状态
state MASTER
# 虚拟 ip 绑定的网卡
interface ens192
# 此 ID 要与 Backup 配置一致
virtual_router_id 80
# 默认启动优先级,要比 Backup 大点,但要控制量,保证自身状态监测生效
priority 100
advert_int 1
virtual_ipaddress {
# 虚拟 ip 地址
192.168.1.2
}
track_script {
check_docker_losa
}
}
另外在编写一个状态检测脚本/etc/keepalived/chk_losa.sh
,检测 docker 中的服务是否正常
#!/bin/bash
RMQ_CONTAINER=$(docker ps -a|awk '/losa-front-nginx/ {print $1}')
if [[ -n "$RMQ_CONTAINER" ]]; then
exit 0
fi
exit 1
(2)配置备节点192.168.1.43
新建文件配置文件/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script check_docker_losa {
# 自身状态检测
script "sh /etc/keepalived/chk_losa.sh"
interval 3
weight 5
}
vrrp_instance VI_1 {
# 初始化状态
state BACKUP
# 虚拟 ip 绑定的网卡
interface ens192
# 此 ID 要与 MASTER 配置一致
virtual_router_id 80
# 默认启动优先级
priority 90
advert_int 1
virtual_ipaddress {
# 虚拟 ip 地址
192.168.1.2
}
track_script {
check_docker_losa
}
}
状态检测脚本类似(1)中主节点配置
3. 启动 keepalived
(1)在主节点上(192.168.1.16
)
# 启动 keepalived 服务
systemctl start keepalived
# 查看启动日志
tail -f /var/log/messages
Oct 29 05:23:18 localhost Keepalived_vrrp[129640]: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 29 05:23:19 localhost Keepalived_vrrp[129640]: VRRP_Instance(VI_1) Changing effective priority from 100 to 105
Oct 29 05:23:19 localhost Keepalived_vrrp[129640]: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 29 05:23:19 localhost Keepalived_vrrp[129640]: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 29 05:23:19 localhost Keepalived_vrrp[129640]: Sending gratuitous ARP on ens192 for 192.168.1.2
...
此时可通过192.168.1.2
访问主节点上的服务
(2)在备节点上(192.168.1.43
)
# 启动 keepalived 服务
systemctl start keepalived
# 查看启动日志
tail -f /var/log/messages
Oct 29 17:32:09 localhost Keepalived_vrrp[15599]: VRRP_Instance(VI_1) Entering BACKUP STATE
...
4. 验证双活效果
(1) 停止主节点上的服务,或者使用systemctl stop keepalived
停止主节点上的 keepalived 服务
(2)这时应观察到备节点192.168.1.43
上的日志:
Oct 29 17:33:05 localhost Keepalived_vrrp[15599]: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 29 17:33:06 localhost Keepalived_vrrp[15599]: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 29 17:33:06 localhost Keepalived_vrrp[15599]: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 29 17:33:06 localhost Keepalived_vrrp[15599]: Sending gratuitous ARP on enp3s0 for 192.168.1.2
...
说明IP192.168.1.2
已经成功漂移到了备节点上,验证访问192.168.1.2
依然能够正常提供服务,此时是备节点提供服务。
(3)恢复主节点上的服务,或者使用systemctl start keepalived
恢复 keepalived 服务
这时观察到备节点上的日志:
Oct 29 17:38:42 localhost Keepalived_vrrp[15599]: VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 29 17:38:42 localhost Keepalived_vrrp[15599]: VRRP_Instance(VI_1) removing protocol VIPs.
...
说明192.168.1.2
又漂移回了主节点上,主节点继续提供服务。