using keepalived to set up a virtual IP environment
Ready to
Prepare two machines with IP address information as follows:
host1:192.168.56.103
host2:192.168.56.104
For testing purposes, install Apache on two machines and start the httpd service so that the following two URLs are accessible
-http://192.168.56.103
-http://192.168.56.104 Installation keepalived
Install keepalived separately on two machines
$ sudo yum install-y keepalived
Configure keepalived
keepalived configuration on host1
$ cat/etc/keepalived/keepalived.conf
vrrp_instance vi_1 {State
MASTER
interface eth2
Virtual_ router_id Wuyi
Priority
Advert_int 1
Authentication {
Auth_type pass
auth_pass 123456
}
virtual_ipaddress {
192.168.56.105
}
}
keepalived configuration on Host2
$ cat/etc/keepalived/keepalived.conf
vrrp_instance vi_1 {State
MASTER
interface eth2
Virtual_ router_id Wuyi
Priority
Advert_int 1
Authentication {
Auth_type pass
auth_pass 123456
}
virtual_ipaddress {
192.168.56.105
}
}
start the keepalived service
$ sudo service keepalived start
$ sudo chkconfig keepalived on
Test
After the service starts, access the following virtual address to access the HTTP server
http://192.168.56.105
View IP address information
IP address information on host1
$ IP Addr Show eth2
3:eth2: <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.56.103/24 brd 192.168.56.255
scope Global eth2
inet 192.168.56.105/32 Scope Global eth2
inet6 fe80::a00:27ff:fe32:8382/64 scope link
valid_lft forever Preferred_lft Forever
You can see the inet 192.168.56.105/32 scope global eth2, which means that host1 is now running as master of the virtual IP.
IP address information on HOST2
$ IP Addr Show eth2
3:eth2: <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.56.104/24 brd 192.168.56.255
scope Global eth2
Inet6 fe80::a00:27ff:feec:907b/64 scope link
valid_lft forever preferred_lft Forever
The IP address information on HOST2 does not contain the virtual IP "192.168.56.105" information.
Verifying IP address Failover
Now manually stop the keepalived service on the host1
$ sudo service keepalived stop
IP address information on host1
$ IP Addr Show eth2
3:eth2: <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.56.103/24 brd 192.168.56.255
scope Global eth2
Inet6 fe80::a00:27ff:fe32:8382/64 scope link
valid_lft forever preferred_lft Forever
The IP address information on Host1 no longer contains the virtual IP "192.168.56.105" information.
IP address information on HOST2
$ IP Addr Show eth2
3:eth2: <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.56.104/24 brd 192.168.56.255
scope Global eth2
inet 192.168.56.105/32 Scope Global eth2
inet6 fe80::a00:27ff:feec:907b/64 scope link
valid_lft forever Preferred_lft Forever
You can now see that the IP address information on the HOST2 already contains the virtual IP "192.168.56.105" information.
At this point if the host1 on the Keepalived service startup, you will find that the virtual IP "192.168.56.105" and rebind to the host1.