1、规划 A主机 :192.168.1.00 B主机:192.168.1.101 VIP:192.168.0.250

2、设置A主机为:MASTER 主机B为:BACKUP

3、安装keepalived

yum install  keepalived.x86_64

4、配置A主机的 keepalived配置文件

vim /etc/keepalived/keepalived.conf

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.250/24
    }
}

5、配置B主机的 keepalived配置文件

vim /etc/keepalived/keepalived.conf

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

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.0.250/24
    }
}

6、重启keepalived服务,并配置开机自启动

systemctl restart keepalived.service 
systemctl enable  keepalived.service 

7、测试是否成功

在A主机上运行

ip add show  (查看是否可以看到VIP地址)

在B主机运行

ip add show  (正常情况下无法获取到VIP地址)

8、模拟故障下VIP是否会漂移

将A主机关机 查看B主机正确获取到VIP地址

ip  add  show(查看是否可以看到VIP地址)