keepalived Transition & Entering {MASTER|BACKUP|FAULT} STATE & first priority affect by track interface|script

本例主要说明1个问题, 当使用脚本或接口的动态优先级调整时, 什么时候优先级会生效.

路由器进入状态后, 动态优先级才会被调整, 没有进入状态前的track都不会影响优先级.

用两个例子来说明问题.

并且初始状态为MASTER和BACKUP, track的先后是有差别的.

如果初始状态时MASTER, 那么是先执行track(此时的脚本调用结果不影响路由器优先级), 然后Transition to MASTER STATE. 

如果初始状态是BACKUP, 那么是先Entering to BACKUP STATE, 然后执行脚本(此时的脚本调用结果会影响路由器优先级).

例子1 :

route A : BACKUP, priority=99, track_script+3 weight, !nopreempt
route B : MASTER, priority=100, track_script+3 weight, !nopreempt

先启动A, 

keepalived -f /opt/keepalived/etc/keepalived/keepalived.conf -D

日志 :

Aug 25 15:21:35 192_168_173_203 Keepalived[25763]: Starting Keepalived v1.2.13 (08/19,2014)
Aug 25 15:21:35 192_168_173_203 Keepalived[25764]: Starting Healthcheck child process, pid=25765
Aug 25 15:21:35 192_168_173_203 Keepalived[25764]: Starting VRRP child process, pid=25766
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Netlink reflector reports IP 192.168.173.203 added
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Netlink reflector reports IP 192.168.173.156 added
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Registering Kernel netlink reflector
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Netlink reflector reports IP 192.168.173.203 added
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Netlink reflector reports IP 192.168.173.156 added
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Registering Kernel netlink command channel
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Registering gratuitous ARP shared channel
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Registering Kernel netlink reflector
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Registering Kernel netlink command channel
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Configuration is using : 7942 Bytes
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Configuration is using : 69324 Bytes
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: Using LinkWatch kernel netlink reflector...
注意用词Entering, 是进入BACKUP状态, 而不是向BACKUP状态转变.
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Instance(vi_1) Entering BACKUP STATE
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: VRRP sockpool: [ifindex(2), proto(112), unicast(1), fd(10,11)]
Aug 25 15:21:35 192_168_173_203 Keepalived_healthcheckers[25765]: Using LinkWatch kernel netlink reflector...
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Script(nag) succeeded
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Script(zero) succeeded
Aug 25 15:21:35 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Script(pos) succeeded
优先级99+3=102
Aug 25 15:21:39 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Instance(vi_1) Transition to MASTER STATE
Aug 25 15:21:40 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Instance(vi_1) Entering MASTER STATE
Aug 25 15:21:40 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Instance(vi_1) setting protocol VIPs.
Aug 25 15:21:40 192_168_173_203 Keepalived_vrrp[25766]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100
Aug 25 15:21:40 192_168_173_203 Keepalived_healthcheckers[25765]: Netlink reflector reports IP 172.16.173.100 added

再启动B, 

keepalived -f /opt/keepalived/etc/keepalived/keepalived.conf -D

日志 :

Aug 25 15:26:27 192_168_173_204 Keepalived[8192]: Starting Keepalived v1.2.13 (08/19,2014)
Aug 25 15:26:27 192_168_173_204 Keepalived[8193]: Starting Healthcheck child process, pid=8194
Aug 25 15:26:27 192_168_173_204 Keepalived[8193]: Starting VRRP child process, pid=8196
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Netlink reflector reports IP 192.168.173.204 added
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Netlink reflector reports IP fe80::f6ce:46ff:fe85:15fc added
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Registering Kernel netlink reflector
Aug 25 15:26:27 192_168_173_204 Keepalived_healthcheckers[8194]: Netlink reflector reports IP 192.168.173.204 added
Aug 25 15:26:27 192_168_173_204 Keepalived_healthcheckers[8194]: Netlink reflector reports IP fe80::f6ce:46ff:fe85:15fc added
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Registering Kernel netlink command channel
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Registering gratuitous ARP shared channel
Aug 25 15:26:27 192_168_173_204 Keepalived_healthcheckers[8194]: Registering Kernel netlink reflector
Aug 25 15:26:27 192_168_173_204 Keepalived_healthcheckers[8194]: Registering Kernel netlink command channel
Aug 25 15:26:27 192_168_173_204 Keepalived_healthcheckers[8194]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:26:27 192_168_173_204 Keepalived_healthcheckers[8194]: Configuration is using : 7944 Bytes
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Configuration is using : 69326 Bytes
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: Using LinkWatch kernel netlink reflector...
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: VRRP sockpool: [ifindex(2), proto(112), unicast(1), fd(10,11)]
Aug 25 15:26:27 192_168_173_204 Keepalived_healthcheckers[8194]: Using LinkWatch kernel netlink reflector...
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Script(pos) succeeded
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Script(zero) succeeded
Aug 25 15:26:27 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Script(nag) succeeded
注意用词Transition, 表示现在还不是master, 像master角色转换而已. 转换过程要干些什么, 参考rfc2338.
Aug 25 15:26:28 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) Transition to MASTER STATE
Aug 25 15:26:28 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) Received higher prio advert
第一次竞选MASTER失败, 说明现在track script虽然执行成功了, 但是没有动态变更优先级, 目前优先级还是100
Aug 25 15:26:28 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) Entering BACKUP STATE
第二次竞选成功, 说明已动态变更优先级. 目前优先级100+3=103
Aug 25 15:26:29 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) forcing a new MASTER election
Aug 25 15:26:29 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) forcing a new MASTER election
Aug 25 15:26:30 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) Transition to MASTER STATE
Aug 25 15:26:31 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) Entering MASTER STATE
Aug 25 15:26:31 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) setting protocol VIPs.
Aug 25 15:26:31 192_168_173_204 Keepalived_vrrp[8196]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100
Aug 25 15:26:31 192_168_173_204 Keepalived_healthcheckers[8194]: Netlink reflector reports IP 172.16.173.100 added

例子2 : 

route A : BACKUP, priority=100, track_script+3 weight, !nopreempt
route B : MASTER, priority=99, track_script+3 weight, !nopreempt

先启动B, 

keepalived -f /opt/keepalived/etc/keepalived/keepalived.conf -D

日志如下 :

Aug 25 15:31:12 192_168_173_204 Keepalived[10659]: Starting Keepalived v1.2.13 (08/19,2014)
Aug 25 15:31:12 192_168_173_204 Keepalived[10660]: Starting Healthcheck child process, pid=10661
Aug 25 15:31:12 192_168_173_204 Keepalived[10660]: Starting VRRP child process, pid=10663
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Netlink reflector reports IP 192.168.173.204 added
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Netlink reflector reports IP fe80::f6ce:46ff:fe85:15fc added
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Registering Kernel netlink reflector
Aug 25 15:31:12 192_168_173_204 Keepalived_healthcheckers[10661]: Netlink reflector reports IP 192.168.173.204 added
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Registering Kernel netlink command channel
Aug 25 15:31:12 192_168_173_204 Keepalived_healthcheckers[10661]: Netlink reflector reports IP fe80::f6ce:46ff:fe85:15fc added
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Registering gratuitous ARP shared channel
Aug 25 15:31:12 192_168_173_204 Keepalived_healthcheckers[10661]: Registering Kernel netlink reflector
Aug 25 15:31:12 192_168_173_204 Keepalived_healthcheckers[10661]: Registering Kernel netlink command channel
Aug 25 15:31:12 192_168_173_204 Keepalived_healthcheckers[10661]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:31:12 192_168_173_204 Keepalived_healthcheckers[10661]: Configuration is using : 7942 Bytes
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Configuration is using : 69324 Bytes
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: Using LinkWatch kernel netlink reflector...
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: VRRP sockpool: [ifindex(2), proto(112), unicast(1), fd(10,11)]
Aug 25 15:31:12 192_168_173_204 Keepalived_healthcheckers[10661]: Using LinkWatch kernel netlink reflector...
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: VRRP_Script(pos) succeeded
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: VRRP_Script(nag) succeeded
Aug 25 15:31:12 192_168_173_204 Keepalived_vrrp[10663]: VRRP_Script(zero) succeeded
现在的优先级是99
Aug 25 15:31:13 192_168_173_204 Keepalived_vrrp[10663]: VRRP_Instance(vi_1) Transition to MASTER STATE
Aug 25 15:31:14 192_168_173_204 Keepalived_vrrp[10663]: VRRP_Instance(vi_1) Entering MASTER STATE
现在的优先级是99+3=102
Aug 25 15:31:14 192_168_173_204 Keepalived_vrrp[10663]: VRRP_Instance(vi_1) setting protocol VIPs.
Aug 25 15:31:14 192_168_173_204 Keepalived_vrrp[10663]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100
Aug 25 15:31:14 192_168_173_204 Keepalived_healthcheckers[10661]: Netlink reflector reports IP 172.16.173.100 added

然后启动A, 

keepalived -f /opt/keepalived/etc/keepalived/keepalived.conf -D

日志如下 :

Aug 25 15:32:11 192_168_173_203 Keepalived[8306]: Starting Keepalived v1.2.13 (08/19,2014)
Aug 25 15:32:11 192_168_173_203 Keepalived[8307]: Starting Healthcheck child process, pid=8308
Aug 25 15:32:11 192_168_173_203 Keepalived[8307]: Starting VRRP child process, pid=8309
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Netlink reflector reports IP 192.168.173.203 added
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Netlink reflector reports IP 192.168.173.156 added
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Registering Kernel netlink reflector
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Netlink reflector reports IP 192.168.173.203 added
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Registering Kernel netlink command channel
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Netlink reflector reports IP 192.168.173.156 added
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Registering gratuitous ARP shared channel
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Registering Kernel netlink reflector
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Registering Kernel netlink command channel
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Configuration is using : 7944 Bytes
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Configuration is using : 69326 Bytes
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: Using LinkWatch kernel netlink reflector...
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Instance(vi_1) Entering BACKUP STATE
注意现在的状态是进入BACKUP STATE,
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: VRRP sockpool: [ifindex(2), proto(112), unicast(1), fd(10,11)]
Aug 25 15:32:11 192_168_173_203 Keepalived_healthcheckers[8308]: Using LinkWatch kernel netlink reflector...
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Script(pos) succeeded
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Script(nag) succeeded
Aug 25 15:32:11 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Script(zero) succeeded
第一次竞选MASTER成功, 说明Entering后, 脚本执行完后, 动态调整了priority. 现在的优先级是100+3=103
Aug 25 15:32:12 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Instance(vi_1) forcing a new MASTER election
Aug 25 15:32:12 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Instance(vi_1) forcing a new MASTER election
Aug 25 15:32:13 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Instance(vi_1) Transition to MASTER STATE
Aug 25 15:32:14 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Instance(vi_1) Entering MASTER STATE
Aug 25 15:32:14 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Instance(vi_1) setting protocol VIPs.
Aug 25 15:32:14 192_168_173_203 Keepalived_healthcheckers[8308]: Netlink reflector reports IP 172.16.173.100 added
Aug 25 15:32:14 192_168_173_203 Keepalived_vrrp[8309]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100

路由器进入状态后, 动态优先级才会被调整. 没有进入状态前的track都不会影响优先级.

[参考]
1. keepalived src

2. http://www.ietf.org/rfc/rfc2338.txt

时间: 2024-09-16 13:49:49

keepalived Transition & Entering {MASTER|BACKUP|FAULT} STATE & first priority affect by track interface|script的相关文章

【HBase】start master 与 start master --backup 的区别

   我们都知道hbase 集群中通常有一个master 节点,多个region server节点,为了防止HBase 集群中master的单点故障,可以向集群中添加一个slave master,这些新添加的master 会分布在不同的物理机器上,因此以最坏的情形来看,当前运行master的那台物理机down了,整个系统可以由backup的 master 来接管!    在介绍如何添加master 节点之前,我们先了解一下在hbase集群中master是如何产生的.master 进程使用Zook

keepalived which time exec track script , notify script when vrrp transition

keepalived什么时候调用track script, track interface, notify_* script, notify; 搞清楚这些对设计HA很有必要. 以下这张图大概的画出了跟踪脚本, 跟踪接口, 状态转换, 以及notify脚本的先后关系 :  当然实际的程序设计和这个没有关系, keepalived是3个进程组成的, 主进程, vrrp进程和health_check进程. 这里不作体现.   keepalived.conf配置项 :  跟踪脚本或跟踪接口配置 : tr

keepalived modify notify.c & vrrp.c to enforce waiting notify script execute success

我们在设计数据库HA的时候, 为了防止主备同时读写一份数据, 或者主备同时对外提供服务. 备机在切换成主机前, 需要有一个fence操作, 需先将主节点fence掉, 然后再激活备库为主库. 例如我们这边用的一个HA脚本 :  https://raw.githubusercontent.com/digoal/sky_postgresql_cluster/master/INSTALL.txt 或者RHEL的HA套件, 都有类似的FENCE操作. 但是keepalived目前没有切换前的自定义脚本,

Keepalived双主模型中vrrp_script中的权重改变故障排查

故障重现 keepalived配置如下 # vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from admin@lnmmp.com smtp_connect_timeout 3 smtp_server 127.0.0.1 router_id LVS_DEVEL

2004 version keepalived.conf introduction

注意由于1.5.3的man keepalived.conf还是用的2004版, 所以少了很多新功能的介绍, 后面再补充. 最新的配置的请参考源代码里面的doc下面的keepalived.conf.SYNOPSIS. NOTE_vrrp_vmac.txt. 截取man keepalived.conf, 简单的描述一下配置文件的使用. keepalived.conf分成3个组成部分 : GLOBAL CONFIGURATION contains subblocks of Global definit

备胎的养成记KeepAlived实现热备负载

在  入坑系列之HAProxy负载均衡 中已经详细讲过了怎么将高并发的请求按均衡算法分发到几台服务器上做均衡防止单机崩溃. 但这样的话有没有发现所有请求都经过了HAproxy代理,自然当并发量越来越高时HAproxy就成了瓶颈,突发时HAproxy就会挂掉,或HAproxy服务器出现故障,这时外部唯一的请求入口没有了,怎么办?重启是个办法,但还是需要抢修时间,这期间内客户都无法访问损失的锅又得程序猿来背. 这种情况下备胎出现了, Keepalived登场,他可以解决服务宕机自动无缝切换. Kee

主备-keepalived 备机启动自动从backup切换成master

问题描述 keepalived 备机启动自动从backup切换成master 按照网上的教程搭建,两台服务器,主机ip:10.1.21.211,备机ip:10.1.21.212,虚ip:10.1.21.213.搭完后备用机一**启动keepalived就自动从BACKUP切换到MASTER(另一机并未关keepalived服务)**,正常主机没有down之前备机应该是backup状态才对啊,用ip a查看时发现备用机的IP也包含虚拟IP. 主机配置: ! Configuration File f

keepalived vrrp script|interface weight when positive,nagtive,zero & vrrp's status transition

本文以实例的形式来展示一下脚本的weight对vrrp的影响(状态或优先级). 注意优先级的变化可能导致vrrp master重新选举, 从而导致状态的变化. 而直接导致状态的变化则是当script weight=0时, 可以直接导致状态的变化.(当然最后还是选举产生的, 除非backup配置了不降级, 那么就不会主动让位.) 首先来看一张图 :  这张图表明了track interface或track script的weight在大于0, 小于0, 等于0这三种状态下, 对VRRP的影响. (

Keepalived 配置和使用

Keepalived 配置和使用 keepalived主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现.keepalived主要目的在于,其自身启动一个服务,能够实现工作在双节点或多个节点上,并且可以在内核生效的ipvs规则其中当前持有资源的节点被称为活跃节点,另外的节点被称为备节点被称为 Master/Backup.   VRRP(如果有学习过TCP\IP,这一块很好理解): 虚拟路由器冗余协议(VRRP)是一种选择协议,它可