1.安装依赖包
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
2.安装keepalived
yum install -y keepalived
3.更新配置
# 配置文件地址:/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两种,通常使用的类型为PASSauth_pass 1111 # 据说AH 使用时有问题。验证密码为明文,同一vrrp 实例MASTER 与BACKUP 使用相同的密码才能正常通信。}virtual_ipaddress {192.168.56.110 # 虚拟ip}
}# 2.备份节点
vrrp_instance VI_1 {state BACKUPinterface enp0s8virtual_router_id 51priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.56.110}
}
4.启动并设置开机自启动
systemctl start keepalived # 启动keepalived
systemctl enable keepalived # 加入开机启动keepalived
systemctl restart keepalived # 重新启动keepalived
systemctl status keepalived # 查看keepalived状态
注意事项:
当某个服务挂了,修复完成后请重启keepalived服务。