keepalived配置虚拟IP实现高可用

配置了两个虚拟机模拟服务器: host1:172.24.84.86 host2:172.24.94.146 虚拟ip:172.24.84.100(此IP是我随便用的一个IP模拟vip)

安装keepalived

我是直接用yum安装的

yum install keepalived

host1:配置文件如下

keepalived配置虚拟IP实现高可用

host2:配置文件如下

keepalived配置虚拟IP实现高可用

eth0 是网卡,有些是eth33,可以通过ifconfig命令进行查看; priority 是权重,权重高表示主服务器优先选择 virtual_ipaddress表示虚拟ip的地址

还有其它的配置自行搜索

关闭防火墙或者打开keepalived通信端口(好像是112)

systemctl stop firewalld

启动keepalived

systemctl start keepalived.service
systemctl enable keepalived.service

查看IP

ip addr show eth0

测试结果

开启keepalived服务后,vip优先来到了权重高的host1

keepalived配置虚拟IP实现高可用

关闭host1的keepalived服务后,vip则漂移到了host2

keepalived配置虚拟IP实现高可用

又重启host1的keepalived服务后,vip回到了权重高的host1

keepalived配置虚拟IP实现高可用