keepalived 单播模式

keepalived在组播模式下所有的信息都会向224.0.0.18的组播地址发送,产生众多的无用信息,并且会产生干扰和冲突,所以需要将其组播的模式改为单拨。这是一种安全的方法,避免局域网内有大量的keepalived造成虚拟路由id的冲突。
单薄模式需要关闭vrrp_strict,严格遵守vrrp协议这个选项
单薄需要在VIP实例配置段加入单播的源地址和目标地址

keepalived 单薄的实现

准备主机2台

server hostname ip
keepalived s1 172.20.27.10
keepalived s2 172.20.27.11

S1 节点配置

1.修改keepalived配置

[root@s1 ~]# vim /etc/keepalived/keepalived.conf 
   notification_email {
        root@mylinuxops.com
   }    
   smtp_server 127.0.0.1
! Configuration File for keepalived
global_defs {
   notification_email {
   notification_email_from root@mylinuxops.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id s1.mylinuxops.com
   vrrp_skip_check_adv_addr
   #vrrp_strict                 #将严格遵守vrrp协议这一项关闭,否则会因为不是组播而无法启动keepalived
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}  

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 27
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }   
    unicast_src_ip 172.20.27.10         #配置单薄的源地址
    unicast_peer { 
    172.20.27.11                        #配置单薄的目标地址
    }
    virtual_ipaddress {
        172.20.27.100 dev ens33 label ens33:0
    }   
}   

重启服务

[root@s1 ~]# systemctl restart keepalived

s2 节点配置

修改同s1节点相同的配置
修改haproxy配置文件

[root@s2 ~]# vim /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
        root@mylinuxops.com
   }
   notification_email_from root@mylinuxops.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id s2.mylinuxops.com
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 27
    priority 80
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    unicast_src_ip 172.20.27.11         #此处配置时候需要注意
    unicast_peer {                      #地址要和s1节点上
    172.20.27.10                        #的地址进行互换
    }
    virtual_ipaddress {
        172.20.27.100 dev ens33 label ens33:0
    }
}

重启服务

[root@s2 ~]# systemctl restart keepalived

测试

在s1上抓包查看心跳信息是否发送给了s2

[root@s1 ~]# tcpdump -i ens33 host -nn  172.20.27.11
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
20:28:45.733836 IP 172.20.27.10 > 172.20.27.11: VRRPv2, Advertisement, vrid 27, prio 100, authtype simple, intvl 2s, length 20
20:28:47.737157 IP 172.20.27.10 > 172.20.27.11: VRRPv2, Advertisement, vrid 27, prio 100, authtype simple, intvl 2s, length 20

将s1上的keepalive服务停止

[root@s1 ~]# systemctl stop keepalived

在s2主机上查看地址是否已经漂移过去

[root@s2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.11  netmask 255.255.0.0  broadcast 172.20.255.255
        inet6 fe80::20c:29ff:fe4d:1ce3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4d:1c:e3  txqueuelen 1000  (Ethernet)
        RX packets 95675  bytes 8093610 (7.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2172  bytes 183327 (179.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.27.100  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:4d:1c:e3  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6  bytes 482 (482.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 482 (482.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0