在配置keepalived vrrp instance时, 有一个nopreempt的选项, 如果配置了nopreempt, 表示进入BACKUP状态的路由器不会在Master还活着的情况下, 抢占MASTER的位置(即使BACKUP路由器的优先级更高)
注意preempt只和当前backup状态有关, 是不是初始backup没有关系.
Preempt_Mode Controls whether a higher priority Backup
router preempts a lower priority Master.
Values are True to allow preemption and
False to not prohibit preemption. Default
is True.
Note: Exception is that the router that owns
the IP address(es) associated with the
virtual router always pre-empts independent
of the setting of this flag.
处于BACKUP状态的路由器 :
6.4.2 Backup
The purpose of the {Backup} state is to monitor the availability and
state of the Master Router.
While in this state, a VRRP router MUST do the following:
- MUST NOT respond to ARP requests for the IP address(s) associated
with the virtual router.
- MUST discard packets with a destination link layer MAC address
equal to the virtual router MAC address.
- MUST NOT accept packets addressed to the IP address(es) associated
with the virtual router.
- If a Shutdown event is received, then:
o Cancel the Master_Down_Timer
o Transition to the {Initialize} state
endif
- If the Master_Down_Timer fires, then:
o Send an ADVERTISEMENT
o Broadcast a gratuitous ARP request containing the virtual
router MAC address for each IP address associated with the
virtual router
o Set the Adver_Timer to Advertisement_Interval
o Transition to the {Master} state
endif
- If an ADVERTISEMENT is received, then:
If the Priority in the ADVERTISEMENT is Zero, then:
o Set the Master_Down_Timer to Skew_Time
else:
If Preempt_Mode is False, or If the Priority in the -- 设置了nopreempt在收到advertisement包后, 相当于默认对方的优先级大于等于本地优先级.
ADVERTISEMENT is greater than or equal to the local
Priority, then:
o Reset the Master_Down_Timer to Master_Down_Interval
else:
o Discard the ADVERTISEMENT
endif
endif
endif
例1 :
route A, priority=100, MASTER, nopreempt
route B, priority=101, BACKUP, nopreempt
1. 先启动A, A进入MASTER状态
2. 再启动B, B进入BACKUP状态, 进入BACKUP后, 不主动竞选MASTER, 所以A还是MASTER.
3. 如果A异常, 则B变成MASTER
4. 如果A恢复, A申请竞选MASTER, 但是优先级低于B, 所以进入BACKUP, 进入BACKUP后, 就不会主动竞选MASTER了(因为设置了nopreempt).
例2 :
route A, priority=101, BACKUP
route B, priority=100, BACKUP, nopreempt
1. 先启动B, B进入MASTER状态
2. 再启动A, A进入BACKUP状态, 进入BACKUP后, 由于A未设置nopreempt, 所以主动竞选MASTER, 优先级高于B, 所以A当选MASTER.
3. 如果A异常, 则B变成MASTER
4. 如果A恢复, A首先变成BACKUP, 然后申请竞选MASTER, 回到MASTER状态, B变成BACKUP.
例3 :
这里要复杂一点, 因为使用了动态优先级 :
route A, priority=100, MASTER, nopreempt, script1+3priovity
route B, priority=99, BACKUP, nopreempt, script1+3priority
1. 先启动route B, 当route B变成master状态后, 执行script1脚本后, 优先级假设+3, 现在优先级是102.
2. 再启动route A. 因为A初始为MASTER, 所以会协商选举, 但是因为当前A的优先级只有100, 低于102, 所以竞选MASTER失败, 转入BACKUP状态, 转入BACKUP状态后, 调用脚本script1, 优先级加3, 优先级变成103, 大于B的102, 但是因为设置了nopreempt, 所以不会主动竞选MASTER, 除非检测到MASTER异常.
[参考]
2. doc/keepalived.conf.SYNOPSIS
nopreempt # Override VRRP RFC preemption default
preempt_delay # Seconds after startup until
# preemption. 0 (default) to 1,000