首先为了测试机器,准备两台服务器:
第一步:在线安装keepalived中间件
sudo yum install -y keepalived
有时候需要先卸载再安装
sudo yum -y remove keepalived
第二步:配置 keepalived
$ vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.28.23.100
}
}
简单的解释下参数:
$ vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.28.23.100
}
}
这两个配置文件差别就在priority 101与priority 100,分别表示主从关系。
第三步: 启动 keepalived 服务
systemctl start keepalived.service
systemctl enable keepalived.service
第四步:查看IP地址信息
ip addr show eth0
得到输出:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:ab:d2:19 brd ff:ff:ff:ff:ff:ff
inet 172.28.23.73/24 brd 172.28.23.255 scope global eth0
valid_lft forever preferred_lft forever
inet 172.28.23.100/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:fecc:0:623::9/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:feab:d219/64 scope link
valid_lft forever preferred_lft forever
其中可以看到 172.28.23.100/32 scope global eth0,说明现在host1是作为虚拟IP的master来运行的。
$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether fa:16:3e:95:87:6b brd ff:ff:ff:ff:ff:ff
inet 172.28.23.96/24 brd 172.28.23.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:fecc:0:623::a/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe95:876b/64 scope link
valid_lft forever preferred_lft forever
此时host2上ip地址信息中不包含虚拟IP “172.28.23.100” 信息。
第五步:验证 IP 地址 Failover
systemctl start keepalived.service
重新查询host1的ip信息
ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:ab:d2:19 brd ff:ff:ff:ff:ff:ff
inet 172.28.23.73/24 brd 172.28.23.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:fecc:0:623::9/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:feab:d219/64 scope link
valid_lft forever preferred_lft forever
此时host1上ip地址信息中不包含虚拟IP “172.28.23.100” 信息。
systemctl stop keepalived.service
systemctl start keepalived.service
# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether fa:16:3e:95:87:6b brd ff:ff:ff:ff:ff:ff
inet 172.28.23.96/24 brd 172.28.23.255 scope global eth0
valid_lft forever preferred_lft forever
inet 172.28.23.100/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:fecc:0:623::a/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe95:876b/64 scope link
valid_lft forever preferred_lft forever
现在可以看到 host2 上 ip 地址信息中已经包含虚拟IP “172.28.23.100” 信息了。
此时如果再把 host1 上的 keepalived 服务启动,会发现虚拟IP “172.28.23.100” 又重新绑定到 host1 上了。
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
2001:fecc:0:622::a/64 #ipv6地址
}
}
systemctl restart keepalived.service
ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:ab:d2:19 brd ff:ff:ff:ff:ff:ff
inet 172.28.23.73/24 brd 172.28.23.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:fecc:0:622::a/64 scope global nodad #虚拟的ipv6地址
valid_lft forever preferred_lft forever
inet6 2001:fecc:0:623::9/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:feab:d219/64 scope link
valid_lft forever preferred_lft forever
其他操作和之前添加vip是一样的。