1.安装依赖包
bash
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
2.安装keepalived
bash
yum install -y keepalived
3.更新配置
yaml
# 配置文件地址:/etc/keepalived/keepalived.conf
# 1.主节点
vrrp_instance VI_1 {
state MASTER # 状态只有MASTER和BACKUP两种,并且要大写,MASTER为工作状态,BACKUP是备用状
interface enp0s8 # 网卡名称,ip addr命令查看,具体看下图
virtual_router_id 51 # 虚拟路由标识,同一个vrrp_instance的MASTER和BACKUP的
priority 100 # 优先级,同一个vrrp_instance的MASTER优先级必须比BACKUP高
advert_int 1 # MASTER 与BACKUP 负载均衡器之间同步检查的时间间隔,单位为秒
authentication {
auth_type PASS # 验证authentication。包含验证类型和验证密码。类型主要有PASS、AH两种,通常使用的类型为PASS
auth_pass 1111 # 据说AH 使用时有问题。验证密码为明文,同一vrrp 实例MASTER 与BACKUP 使用相同的密码才能正常通信。
}
virtual_ipaddress {
192.168.56.110 # 虚拟ip
}
}
# 2.备份节点
vrrp_instance VI_1 {
state BACKUP
interface enp0s8
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.56.110
}
}
4.启动并设置开机自启动
bash
systemctl start keepalived # 启动keepalived
systemctl enable keepalived # 加入开机启动keepalived
systemctl restart keepalived # 重新启动keepalived
systemctl status keepalived # 查看keepalived状态
注意事项:
当某个服务挂了,修复完成后请重启keepalived服务。