keepalived 学习笔记(二)切换原理(非代码级别分析)

一、切换原理 

    由于 网络不稳定造成的切换,主要存在以下网络变化场景:

(1)  主机网口上的网线松动掉了;

(2)  主机上重启了网卡服务;

(3)  网络通信慢,或带宽被占满,导致 vrrp 包没有被备机收到;

(4)  主机的 keepalived 服务被重启;

(5)  主机服务器被重启(正常重启);

(6)  主机服务器被断电重启(非正常重启);

 

主备机的 keepalived 在网络不稳定场景下的包交互流程如下图:

 

 (1)  网卡松动场景下(以下所有示例的 advert_int 均设置为了 30s)

    此时 主机的 keepalived 日志为:

Jun 21 15:22:21 ha-slave Keepalived_vrrp[1093]: Netlink reports eno1 down
Jun 21 15:22:21 ha-slave Keepalived_vrrp[1093]: (product) Entering FAULT STATE
Jun 21 15:22:21 ha-slave Keepalived_vrrp[1093]: (product) sent 0 priority
Jun 21 15:22:21 ha-slave Keepalived_vrrp[1093]: (product) removing VIPs.
Jun 21 15:22:26 ha-slave Keepalived_vrrp[1093]: Netlink reports eno1 up
Jun 21 15:22:26 ha-slave Keepalived_vrrp[1093]: (product) Entering BACKUP STATE

   备机的 keepalived 日志为:

Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: (product) Receive advertisement timeout
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: (product) Entering MASTER STATE
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: (product) setting VIPs.
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:51 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:56 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:56 ha-master Keepalived_vrrp[126758]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 15:22:56 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:56 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:56 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:22:56 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172

 可以看到流程如下:

 a)  主机上 keepalived 服务检测到服务网卡  down;

 b)  keepalived vrrp 实例 自主进入 FAULT 状态;

 c)  keepalived 发送 优先级为 0 的 vrrp包  (但实际网卡上的网线已经松动了,连不上网,所以此时其实这个包发不出去)

 d) keepalived 移除 vip;

 e) 网线插好之后, keepalived 会自动进入 backup 状态;

 f)  备机的 keepalived 在 主机  a) 流程开始其实已经接收不到 vrrp包了,只要接收不到 vrrp 包的时间超过 3倍的 advert_int 就会自主转为 master ,这里是超过 30s ,在 keepalived 日志中可以看到  “Receive advertisement timeout

以上的转变过程了,为了准确看 f) 流程,可以在主备机上  抓 vrrp 包查看,命令为 :tcpdump -i 网卡  vrrp -vvv -n

(2) 重启网卡服务的场景下

   此时主机上的 keepalived 日志为:

Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: Netlink reflector reports IP 10.19.125.172 removed from eno1
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: (product) Entering BACKUP STATE
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: (product) sent 0 priority
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: (product) removing VIPs.
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: Netlink reports eno1 down
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: (product) Entering FAULT STATE
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: Deassigned address fe80::ea61:ff:fe00:ca1 from interface eno1
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: Netlink reports eno1 up
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: (product) Entering BACKUP STATE
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: product: sending gratuitous ARP for 10.19.125.28
Jun 21 15:24:58 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.28
Jun 21 15:25:00 ha-master Keepalived_vrrp[126758]: Assigned address fe80::ea61:ff:fe00:ca1 for interface eno1

 备机上的 keepalived 日志为:

Jun 21 15:24:58 ha-slave Keepalived_vrrp[1093]: (product) Backup received priority 0 advertisement
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: (product) Receive advertisement timeout
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: (product) Entering MASTER STATE
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: (product) setting VIPs.
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:24:59 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:25:04 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172

 可以看到流程如下:

 a)  主机 keepalived 首先将 172 这个 VIP 从网卡上移除;

 b)  主机 进入 backup 状态,并发送优先级为 0 的 vrrp 包;

 c)  备机 keepalived 可以收到这个优先级为 0 的 vrrp 包(此时主机的网卡还没有 down,所以这个包还是可以发送出去的); 并立刻进入 master 状态;

 d)  原先主机 网卡 down状态,keepalived  进入 FAULT 状态,网卡服务启动好之后,网卡 up 状态,keepalived 进入 BACKUP 状态;接收来自 新 主机的 vrrp 包;

 

在这里有个极端例子,若是网卡重启网卡服务,网卡恢复慢,会造成 重启网卡的 keepalived 从 MASTER --> BACKUP  ---> FAULT --> BACKUP ---> MASTER  --> BACKUP 的状态不断变化,而原先的备机状态会从  BACKUP ---> MASTER,可以看出原先主机频繁变化,本案例产生这样的效果的配置是:  主备均是物理机非虚拟机,且 advert_int 为 1s,小IP是主机,大IP是备机

主机(28)上的 keepalived 日志为:

Jun 21 15:11:29 ha-master Keepalived_vrrp[126758]: Netlink reflector reports IP 10.19.125.172 removed from eno1
Jun 21 15:11:29 ha-master Keepalived_vrrp[126758]: (product) Entering BACKUP STATE
Jun 21 15:11:29 ha-master Keepalived_vrrp[126758]: (product) sent 0 priority
Jun 21 15:11:29 ha-master Keepalived_vrrp[126758]: (product) removing VIPs.
Jun 21 15:11:29 ha-master Keepalived_vrrp[126758]: Netlink reports eno1 down
Jun 21 15:11:29 ha-master Keepalived_vrrp[126758]: (product) Entering FAULT STATE
Jun 21 15:11:29 ha-master Keepalived_vrrp[126758]: Deassigned address fe80::ea61:ff:fe00:ca1 from interface eno1
Jun 21 15:11:30 ha-master Keepalived_vrrp[126758]: Netlink reports eno1 up
Jun 21 15:11:30 ha-master Keepalived_vrrp[126758]: (product) Entering BACKUP STATE
Jun 21 15:11:30 ha-master Keepalived_vrrp[126758]: product: sending gratuitous ARP for 10.19.125.28
Jun 21 15:11:30 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.28
Jun 21 15:11:31 ha-master Keepalived_vrrp[126758]: Assigned address fe80::ea61:ff:fe00:ca1 for interface eno1
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: (product) Receive advertisement timeout
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: (product) Entering MASTER STATE
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: (product) setting VIPs.
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:33 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:34 ha-master Keepalived_vrrp[126758]: (product) Master received advert from 10.19.125.173 with same priority 100 but higher IP address than ours
Jun 21 15:11:34 ha-master Keepalived_vrrp[126758]: (product) Entering BACKUP STATE
Jun 21 15:11:34 ha-master Keepalived_vrrp[126758]: (product) removing VIPs.

 备机(173)上的 keepalived 日志为:

Jun 21 15:11:29 ha-slave Keepalived_vrrp[1093]: (product) Backup received priority 0 advertisement
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: (product) Receive advertisement timeout
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: (product) Entering MASTER STATE
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: (product) setting VIPs.
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:30 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:34 ha-slave Keepalived_vrrp[1093]: (product) Received advert from 10.19.125.28 with lower priority 100, ours 100, forcing new election
Jun 21 15:11:34 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:34 ha-slave Keepalived_vrrp[1093]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 15:11:34 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:11:34 ha-slave Keepalived_vrrp[1093]: Sending gratuitous ARP on eno1 for 10.19.125.172

 通过在备机(173)上 使用命令  tcpdump -i eno1 vrrp -vvv -n  去抓捕整个过程中的 vrrp 包 ,见如下:

15:11:28.412259 IP (tos 0xc0, ttl 255, id 280, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.28 > 10.19.125.173: vrrp 10.19.125.28 > 10.19.125.173: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:29.412319 IP (tos 0xc0, ttl 255, id 281, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.28 > 10.19.125.173: vrrp 10.19.125.28 > 10.19.125.173: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:29.666703 IP (tos 0xc0, ttl 255, id 282, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.28 > 10.19.125.173: vrrp 10.19.125.28 > 10.19.125.173: VRRPv2, Advertisement, vrid 172, prio 0, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:30.276291 IP (tos 0xc0, ttl 255, id 232, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:31.276328 IP (tos 0xc0, ttl 255, id 233, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:32.276370 IP (tos 0xc0, ttl 255, id 234, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:33.276408 IP (tos 0xc0, ttl 255, id 235, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:34.276446 IP (tos 0xc0, ttl 255, id 236, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:34.684150 IP (tos 0xc0, ttl 255, id 283, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.28 > 10.19.125.173: vrrp 10.19.125.28 > 10.19.125.173: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:34.684219 IP (tos 0xc0, ttl 255, id 237, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:35.684284 IP (tos 0xc0, ttl 255, id 238, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:36.684319 IP (tos 0xc0, ttl 255, id 239, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:37.684349 IP (tos 0xc0, ttl 255, id 240, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"
15:11:38.684382 IP (tos 0xc0, ttl 255, id 241, offset 0, flags [none], proto VRRP (112), length 40)
    10.19.125.173 > 10.19.125.28: vrrp 10.19.125.173 > 10.19.125.28: VRRPv2, Advertisement, vrid 172, prio 100, authtype simple, intvl 1s, length 20, addrs: 10.19.125.172 auth "123456^@^@"

 分析 上面三份资料,可以得到如下流程:

a)  15:11:29.412319    备机收到主机发送的 优先级为 100 的  vrrp 包,此前都是每秒收到一次;

b)   Jun 21 15:11:29   28 重启了网卡,进入 backup 状态,同时发送了 优先级为 0的 vrrp 包;

c)  备机 (173) 收到了 这个 vrrp 包,时间为    5:11:29.666703  ,并立即在 15:11:30.276291 向 28 发送了优先级为 100 的 vrrp 包; 

d)  备机 (173) 在 c) 发送 vrrp 包的同时,进入 master 状态;后续每隔 1s 都会向 28 发送 优先级为 100 的 vrrp 包;

e)  28 服务器 在网卡服务启动之后,Jun 21 15:11:30 进入BACUKUP 状态,但是可能一直过了 3s ,网卡都没有准备好且收到 来自 173 的 vrrp 包,因此 在 Jun 21 15:11:33  转为了 master 状态;

f)现在可以知道 28 和 173 都是 master 了,因此双方都向对端发送了优先级为 100 的 vrrp 包,28 收到 来自 173 的 vrpp 包之后,判断 优先级相同情况下,对端 IP  大于自己,因此转为 BACKUP 状态;

 

由上可以看出在网卡启动恢复过程中若时间超过了 3s ,也就是 超过 3倍 的 advert_int 指定的时间,就会自动转为 MASTER 状态,因此 advert_int 还是要设置稍长点,比如 10s.

在脑裂的情况下,及都是 master 的时候,若 vrrp 包优先级相同,则会根据 IP 来决策谁是 MASTER。规则为:IP谁大,谁是 MASTER;

 (3) 主机的 keepalived 服务被重启的场景下

     主机 为 173 ,备机为 28,advert_int 为 10,173 和 28 都配置成了 初始状态为 BACKUP,优先级为 100 ,非抢占式。

 此时主机 keepalived 的日志为:

Jun 21 15:26:35 ha-slave Keepalived[1092]: Stopping
Jun 21 15:26:35 ha-slave Keepalived_vrrp[1093]: (product) sent 0 priority
Jun 21 15:26:35 ha-slave Keepalived_vrrp[1093]: (product) removing VIPs.
Jun 21 15:26:36 ha-slave Keepalived_vrrp[1093]: Stopped - used 0.662641 user time, 4.267257 system time
Jun 21 15:26:36 ha-slave Keepalived[1092]: Stopped Keepalived v2.0.20 (01/22,2020)
Jun 21 15:26:36 ha-slave Keepalived[78393]: Starting Keepalived v2.0.20 (01/22,2020)
Jun 21 15:26:36 ha-slave Keepalived[78393]: Running on Linux 3.10.0-1127.18.2.el7.x86_64 #1 SMP Tue Sep 1 11:13:23 EDT 2020 (built for Linux 3.10.0)
Jun 21 15:26:36 ha-slave Keepalived[78393]: Command line: '/usr/local/keepalived/keepalived/sbin/keepalived' '-D' '-d' '-S' '0'
Jun 21 15:26:36 ha-slave Keepalived[78393]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 21 15:26:36 ha-slave Keepalived[78393]: Opening file './conf/keepalived-product.conf'.
Jun 21 15:26:36 ha-slave Keepalived[78394]: Starting VRRP child process, pid=78395
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Registering Kernel netlink reflector
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Registering Kernel netlink command channel
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Opening file './conf/keepalived-product.conf'.
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Unsafe permissions found for script '/etc/keepalived/product/bin/health_check.sh'.
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: SECURITY VIOLATION - scripts are being executed but script_security not enabled. There are insecure scripts.
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Assigned address 10.19.125.173 for interface eno1
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Assigned address fe80::ea61:ff:fe00:cad for interface eno1
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Registering gratuitous ARP shared channel
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: (product) removing VIPs.
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: ------< Global definitions >------
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: Network namespace = (default)
。。。。。。。。。。。。。。
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]:   Reset promote_secondaries counter 0
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]:   Tracking VRRP instances = 0
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: (product) Entering BACKUP STATE (init)
Jun 21 15:26:36 ha-slave Keepalived_vrrp[78395]: VRRP sockpool: [ifindex(2), family(IPv4), proto(112), unicast(1), fd(11,12)]

 备机的 keepalived 的日志为:

Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: (product) Backup received priority 0 advertisement
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: (product) Receive advertisement timeout
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: (product) Entering MASTER STATE
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: (product) setting VIPs.
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 15:26:35 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172

 变化流程如下:

a) 主机 keepalived 停止时,在  Jun 21 15:26:35  发送了优先级为 0 的 vrrp 包,并移除了 VIP

b)  备机在  Jun 21 15:26:35  接收到优先级为0的 vrrp 包,立刻转为了 MASTER 状态

c)  主机 keepalived 启动成功之后直接转为 BACKUP状态

上面主备的状态都比较稳定,没有发生频繁切换的现象。

 (4)   主机服务器被重启(reboot)场景下

   主机 为 173 ,备机为 28,advert_int 为 10,173 和 28 都配置成了 初始状态为 BACKUP,优先级为 100 ,非抢占式。

此时主机 keepalived 的日志为:

Jun 21 16:54:09 ha-slave Keepalived[945]: Starting Keepalived v2.0.20 (01/22,2020)
Jun 21 16:54:09 ha-slave Keepalived[945]: Running on Linux 3.10.0-1127.18.2.el7.x86_64 #1 SMP Tue Sep 1 11:13:23 EDT 2020 (built for Linux 3.10.0)
Jun 21 16:54:09 ha-slave Keepalived[945]: Command line: '/usr/local/keepalived/keepalived/sbin/keepalived' '-D' '-d' '-S' '0'
Jun 21 16:54:09 ha-slave Keepalived[945]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 21 16:54:09 ha-slave Keepalived[945]: Opening file './conf/keepalived-product.conf'.
Jun 21 16:54:09 ha-slave Keepalived[1141]: Starting VRRP child process, pid=1142
Jun 21 16:54:09 ha-slave Keepalived_vrrp[1142]: Registering Kernel netlink reflector
。。。。。。。。。。。。。。。。。。。。。。。。
Jun 21 16:54:09 ha-slave Keepalived_vrrp[1142]:   Tracking VRRP instances = 0
Jun 21 16:54:09 ha-slave Keepalived_vrrp[1142]: (product) Entering BACKUP STATE (init)
Jun 21 16:54:09 ha-slave Keepalived_vrrp[1142]: VRRP sockpool: [ifindex(2), family(IPv4), proto(112), unicast(1), fd(11,12)]
Jun 21 16:54:09 ha-slave Keepalived_vrrp[1142]: VRRP_Script(productHealthCheck) succeeded
Jun 21 16:54:13 ha-slave Keepalived_vrrp[1142]: Assigned address 10.19.125.173 for interface eno1

 备机的 keepalived 的日志为:

Jun 21 16:51:25 ha-master Keepalived_vrrp[126758]: (product) Backup received priority 0 advertisement
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: (product) Receive advertisement timeout
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: (product) Entering MASTER STATE
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: (product) setting VIPs.
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:26 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:31 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:31 ha-master Keepalived_vrrp[126758]: (product) Sending/queueing gratuitous ARPs on eno1 for 10.19.125.172
Jun 21 16:51:31 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:31 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:31 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172
Jun 21 16:51:31 ha-master Keepalived_vrrp[126758]: Sending gratuitous ARP on eno1 for 10.19.125.172

 变化流程如下:

a) 备机在 Jun 21 16:51:25   就收到来自 主机发送的 优先级为 0 的 vrrp 包,因此直接转为 MASTER状态,并开始对外发送 vrrp包;

b) 主机在 启动之后直接进入 BACKUP 状态,由于在进入该状态之后 30s 可以收到 vrrp 包,因此保持BACKUP状态,未作切换了;

 

posted @ 2021-06-21 20:17  夏之夜  阅读(3796)  评论(0编辑  收藏  举报