前面我在写keepalived优先级动态调整时, 写到关于初始优先级设置为255的instance不被动态调整的内容, 原因参考本文末尾部分. 因为
if (vrrp->base_priority == VRRP_PRIO_OWNER) {
/* we will not run a PRIO_OWNER into a non-PRIO_OWNER */
vrrp->effective_priority = VRRP_PRIO_OWNER;
本文主要就是实际验证一下.
用的例子参考
把192.168.173.204的初始优先级调整为255.
把192.168.173.203的初始优先级调整为254.
keepalived启动后, 192.168.173.204是MASTER.
当我把192.168.173.204的nag.var调成1, nag.sh脚本检查失败, 日志如下
但是不影响 192.168.173.204的优先级, 所以它还是master.
[参考]
Aug 21 13:59:03 192_168_173_203 Keepalived[421]: Starting Keepalived v1.2.13 (08/19,2014)
Aug 21 13:59:03 192_168_173_203 Keepalived[422]: Starting Healthcheck child process, pid=423
Aug 21 13:59:03 192_168_173_203 Keepalived[422]: Starting VRRP child process, pid=424
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Netlink reflector reports IP 192.168.173.203 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Netlink reflector reports IP 192.168.173.156 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Registering Kernel netlink reflector
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 192.168.173.203 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Registering Kernel netlink command channel
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 192.168.173.156 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Registering gratuitous ARP shared channel
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Registering Kernel netlink reflector
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Registering Kernel netlink command channel
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Configuration is using : 7937 Bytes
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Configuration is using : 69311 Bytes
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Using LinkWatch kernel netlink reflector...
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP sockpool: [ifindex(2), proto(112), unicast(1), fd(10,11)]
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Using LinkWatch kernel netlink reflector...
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP_Script(nag) succeeded
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP_Script(zero) succeeded
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP_Script(pos) succeeded
Aug 21 13:59:04 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Transition to MASTER STATE
Aug 21 13:59:05 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Entering MASTER STATE
Aug 21 13:59:05 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) setting protocol VIPs.
Aug 21 13:59:05 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100
Aug 21 13:59:05 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 172.16.173.100 added
Aug 21 13:59:06 192_168_173_203 rgmanager[652]: [script] Executing /usr/local/bin/pgctl.sh status
Aug 21 13:59:10 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100
Aug 21 13:59:26 192_168_173_203 rgmanager[1041]: [script] Executing /usr/local/bin/pgctl.sh status
Aug 21 13:59:29 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Received higher prio advert
Aug 21 13:59:29 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Entering BACKUP STATE
Aug 21 13:59:29 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) removing protocol VIPs.
Aug 21 13:59:29 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 172.16.173.100 removed
Aug 21 13:59:03 192_168_173_203 Keepalived[421]: Starting Keepalived v1.2.13 (08/19,2014)
Aug 21 13:59:03 192_168_173_203 Keepalived[422]: Starting Healthcheck child process, pid=423
Aug 21 13:59:03 192_168_173_203 Keepalived[422]: Starting VRRP child process, pid=424
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Netlink reflector reports IP 192.168.173.203 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Netlink reflector reports IP 192.168.173.156 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Registering Kernel netlink reflector
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 192.168.173.203 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Registering Kernel netlink command channel
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 192.168.173.156 added
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Registering gratuitous ARP shared channel
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP fe80::f6ce:46ff:fe86:4234 added
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Registering Kernel netlink reflector
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Registering Kernel netlink command channel
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Opening file '/opt/keepalived/etc/keepalived/keepalived.conf'.
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Configuration is using : 7937 Bytes
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Configuration is using : 69311 Bytes
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: Using LinkWatch kernel netlink reflector...
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP sockpool: [ifindex(2), proto(112), unicast(1), fd(10,11)]
Aug 21 13:59:03 192_168_173_203 Keepalived_healthcheckers[423]: Using LinkWatch kernel netlink reflector...
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP_Script(nag) succeeded
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP_Script(zero) succeeded
Aug 21 13:59:03 192_168_173_203 Keepalived_vrrp[424]: VRRP_Script(pos) succeeded
Aug 21 13:59:04 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Transition to MASTER STATE
Aug 21 13:59:05 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Entering MASTER STATE
Aug 21 13:59:05 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) setting protocol VIPs.
Aug 21 13:59:05 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100
Aug 21 13:59:05 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 172.16.173.100 added
Aug 21 13:59:10 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Sending gratuitous ARPs on eth0 for 172.16.173.100
Aug 21 13:59:29 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Received higher prio advert
Aug 21 13:59:29 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) Entering BACKUP STATE
Aug 21 13:59:29 192_168_173_203 Keepalived_vrrp[424]: VRRP_Instance(vi_1) removing protocol VIPs.
Aug 21 13:59:29 192_168_173_203 Keepalived_healthcheckers[423]: Netlink reflector reports IP 172.16.173.100 removed
当我把192.168.173.204的nag.var调成1, nag.sh脚本检查失败, 日志如下
Aug 21 13:59:43 192_168_173_204 Keepalived_vrrp[13887]: VRRP_Script(nag) failed
但是不影响 192.168.173.204的优先级, 所以它还是master.
[root@192_168_173_204 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr F4:CE:46:85:15:FC
inet addr:192.168.173.204 Bcast:192.168.173.255 Mask:255.255.255.0
inet6 addr: fe80::f6ce:46ff:fe85:15fc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10360595 errors:0 dropped:0 overruns:0 frame:0
TX packets:4714379 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1469576774 (1.3 GiB) TX bytes:661681617 (631.0 MiB)
eth0:1 Link encap:Ethernet HWaddr F4:CE:46:85:15:FC
inet addr:172.16.173.100 Bcast:172.16.173.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:163476 errors:0 dropped:0 overruns:0 frame:0
TX packets:163476 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8835522 (8.4 MiB) TX bytes:8835522 (8.4 MiB)
[参考]
动态调整优先级的范围 :
如果配置的instance的初始优先级是255的话, 则不受track weight的影响. 见vrrp_update_priority函数的内容.
换句话说, 只有初始优先级配置<255的instance, 才可以动态调整优先级. 并且动态调整后的优先级范围是1到254.
if (vrrp->base_priority == VRRP_PRIO_OWNER) {
/* we will not run a PRIO_OWNER into a non-PRIO_OWNER */
vrrp->effective_priority = VRRP_PRIO_OWNER;
} else {
/* WARNING! we must compute new_prio on a signed int in order
to detect overflows and avoid wrapping. */
new_prio = vrrp->base_priority + prio_offset;
if (new_prio < 1)
new_prio = 1;
else if (new_prio > 254)
new_prio = 254;
vrrp->effective_priority = new_prio;
}
keepalived / include / vrrp.h
#define VRRP_PRIO_OWNER 255 /* priority of the ip owner -- rfc2338.5.3.4 */
换句话说, 只有初始优先级配置<255的instance, 才可以动态调整优先级. 并且动态调整后的优先级范围是1到254.
priority <INTEGER-0..255> # VRRP PRIO