keepalived设置虚拟IP, 实现IP漂移
使用 keepalived 设置虚拟 IP 环境
准备
准备三个机器,IP地址信息如下:
host1: 192.168.2.206
host2: 192.168.2.205
hots3: 192.168.2.177
安装 keepalived
在两台机器上分别安装 keepalived
$ sudo yum install -y keepalived
1
配置 keepalived
host1 上 keepalived 配置
$ cat /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.2.98
}
}
host2 上 keepalived 配置
$ cat /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.2.98
}
}
Host3 上 keepalived 配置
$ cat /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.2.98
}
}
启动 keepalived 服务
$ sudo service keepalived start
$ sudo chkconfig keepalived on
查看IP地址信息
host1 上 IP 地址信息
$ ip addr show ens160
3: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:32:83:82 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.205/24 brd 192.168.2.255 scope global eth2
inet 192.168.2.98/32 scope global eth2
inet6 fe80::a00:27ff:fe32:8382/64 scope link
valid_lft forever preferred_lft forever
7
其中可以看到 inet 192.168.2.98/32 scope global ens160,说明现在host1是作为虚拟IP的master来运行的。
host2 上 IP 地址信息
$ ip addr show eth0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:ec:90:7b brd ff:ff:ff:ff:ff:ff
inet 192.168.2.206/24 brd 192.168.56.255 scope global eth2
inet6 fe80::a00:27ff:feec:907b/64 scope link
valid_lft forever preferred_lft forever
此时host2上ip地址信息中不包含虚拟IP “192.168.56.105” 信息。
验证 IP 地址 Failover
现在手动停止host1上的 keepalived 服务
$ sudo service keepalived stop
1
host1 上 IP 地址信息
$ ip addr show eth0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:32:83:82 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.205 /24 brd 192.168.56.255 scope global eth2
inet6 fe80::a00:27ff:fe32:8382/64 scope link
valid_lft forever preferred_lft forever
此时 host1 上 ip 地址信息中不再包含虚拟IP “192.168.56.105” 信息。
host2 上 IP 地址信息
$ ip addr show eth0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:ec:90:7b brd ff:ff:ff:ff:ff:ff
inet 192.168.2.206/24 brd 192.168.56.255 scope global eth2
inet 192.168.2.98/32 scope global eth2
inet6 fe80::a00:27ff:feec:907b/64 scope link
valid_lft forever preferred_lft forever
现在可以看到 host2 上 ip 地址信息中已经包含虚拟IP “192.168.2.98” 信息了。
此时如果再把 host1 上的 keepalived 服务启动,会发现虚拟IP “192.168.2.98” 又重新绑定到 host1 上了。
参考链接:https://blog.csdn.net/wexiaoword/java/article/details/81364488