Two centos systems are built using vmware locally, and keepalived is installed separately
yum install -y keepalived
Then configure
Add virtaul IP 192.168.2.245 to the machine 192.168.2.241
And set state to MASTER
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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 {
192.168.2.245
}
}
Add virtaul IP 192.168.2.245 to the machine 192.168.2.242
And set the state to BACKUP
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.245
}
}
start up
# service keepalived start
Install nginx on two machines to test whether the failed machine is successfully transferred
Now visit http://192.168.2.245
Then I turned off the 241 machine and then visited http://192.168.2.245
We see that the IP rolls to another machine.
testing successfully.
Of course, what we are simulating is a machine failure. If it is just a problem with nginx, we can also use keepalived to monitor nginx to achieve IP switching.