非 LVS 负载均衡的配置文件模板
master
global_defs {
router_id nginx
enable_script_security
}
vrrp_script nginx_status {
script /opt/script/nginx_status_check.sh
interval 3
timeout 5
# 注意权重和优先级的大小(一个节点的priority减去weight必须小于另外一个节点的priority)
weight -10
rise 2
fall 2
user nginx nginx #要确保 nginx 用户存在
init_fail
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
# 注意权重和优先级的大小(一个节点的priority减去weight必须小于另外一个节点的priority)
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.150/24 dev ens33 label ens33:0
}
track_script {
nginx_status
}
preempt_delay 300
notify_master /opt/script/to_master.sh
notify_backup /opt/script/to_backup.sh
notify_stop /opt/script/to_stop.sh
}
backup
global_defs {
router_id nginx
enable_script_security
}
vrrp_script nginx_status {
script /opt/script/nginx_status_check.sh
interval 3
timeout 5
# 注意权重和优先级的大小(一个节点的priority减去weight必须小于另外一个节点的priority)
weight -10
rise 2
fall 2
user nginx nginx #要确保 nginx 用户存在
init_fail
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
# 注意权重和优先级的大小(一个节点的priority减去weight必须小于另外一个节点的priority)
priority 95
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.150/24 dev ens33 label ens33:0
}
track_script {
nginx_status
}
notify_master /opt/script/to_master.sh
notify_backup /opt/script/to_backup.sh
notify_stop /opt/script/to_stop.sh
}
实验中相关脚本
实验中的脚本非常简单,只是为了验证 keepalived 的功能。生产环境中肯定不会使用这样的脚本,都是根据自身的环境在状态发生切换时执行相应的命令或者发送邮件等。
nginx 健康检查脚本
#!/bin/bash
#author : panbuhei
#nginx check script in keepalived
NGINX_PIDNUM=`ps -ef | grep nginx | grep -v grep | wc -l`
NGINX_PORTNUM=`ss -antpl | grep nginx | wc -l`
if [ $NGINX_PIDNUM -eq 0 ];then
exit 1
elif [ $NGINX_PORTNUM -eq 0 ];then
exit 1
else
exit 0
fi
to_master.sh
cat << EOF > /opt/script/to_master.sh
#!/bin/bash
#author : panbuhei
#
DATE=`date +"%F %H:%M:%S"`
echo "当前节点于 $DATE 切换为 MASTER" >> /tmp/keepalived_change.log
EOF
to_backup.sh
cat << EOF > /opt/script/to_backup.sh
#!/bin/bash
#author : panbuhei
#
DATE=`date +"%F %H:%M:%S"`
echo "当前节点于 $DATE 切换为 BACKUP" >> /tmp/keepalived_change.log
EOF
to_stop.sh
cat << EOF > /opt/script/to_stop.sh
#!/bin/bash
#author : panbuhei
#
DATE=`date +"%F %H:%M:%S"`
echo "当前节点于 $DATE 关闭" >> /tmp/keepalived_change.log
EOF
实验步骤
1 同步两台节点的时间
ntpdate -s ntp1.aliyun.com
1 启动两台节点的 nginx
......
2 启动 keepalived
注意: 当使用 keepalived -t
检查脚本语法时,关于 SECURITY VIOLATION - scripts are being executed but script_security not enabled
的报错可以忽略,因为服务可以正常启动,而且相关脚本也能正常执行。
keepalived111 启动日志:
May 7 09:20:09 keepalived111 systemd: Starting LVS and VRRP High Availability Monitor...
May 7 09:20:09 keepalived111 Keepalived[39824]: Starting Keepalived v2.0.11 (01/06,2019), git commit v2.0.10-75-g40afb8e+
May 7 09:20:09 keepalived111 Keepalived[39824]: Running on Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 (built for Linux 3.10.0)
May 7 09:20:09 keepalived111 Keepalived[39824]: Command line: '/usr/local/keepalived/sbin/keepalived' '-f'
May 7 09:20:09 keepalived111 Keepalived[39824]: '/usr/local/keepalived/etc/keepalived/keepalived.conf' '-p'
May 7 09:20:09 keepalived111 Keepalived[39824]: '/usr/local/keepalived/temp/keepalived.pid' '-D'
May 7 09:20:09 keepalived111 Keepalived[39824]: Opening file '/usr/local/keepalived/etc/keepalived/keepalived.conf'.
May 7 09:20:09 keepalived111 Keepalived[39825]: Starting VRRP child process, pid=39826
May 7 09:20:09 keepalived111 systemd: Started LVS and VRRP High Availability Monitor.
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: Registering Kernel netlink reflector
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: Registering Kernel netlink command channel
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: Opening file '/usr/local/keepalived/etc/keepalived/keepalived.conf'.
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: Assigned address 192.168.10.146 for interface ens33
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: Assigned address fe80::4f78:60f1:9d7d:3da0 for interface ens33
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: Registering gratuitous ARP shared channel
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: (VI_1) removing VIPs.
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: (VI_1) Entering BACKUP STATE (init) #可以看到 keepalived111 初始化的状态为 BACKUP。
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: VRRP sockpool: [ifindex(2), family(IPv4), proto(112), unicast(0), fd(11,12)]
May 7 09:20:09 keepalived111 Keepalived_vrrp[39826]: VRRP_Script(nginx_status) succeeded
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: (VI_1) Receive advertisement timeout
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: (VI_1) Entering MASTER STATE #由于只启动了 keepalived111 节点,并且它的优先级最高,所以几秒后状态切换为 MASTER
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: (VI_1) setting VIPs.
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150 #这里可以看到 keepalived111 节点获取到了vip。
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.150
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:20:13 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:20:18 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
keepalived222 启动日志:
May 7 09:23:04 keepalived222 systemd: Starting LVS and VRRP High Availability Monitor...
May 7 09:23:04 keepalived222 Keepalived[35229]: Starting Keepalived v2.0.11 (01/06,2019), git commit v2.0.10-75-g40afb8e+
May 7 09:23:04 keepalived222 Keepalived[35229]: Running on Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 (built for Linux 3.10.0)
May 7 09:23:04 keepalived222 Keepalived[35229]: Command line: '/usr/local/keepalived/sbin/keepalived' '-f'
May 7 09:23:04 keepalived222 Keepalived[35229]: '/usr/local/keepalived/etc/keepalived/keepalived.conf' '-p'
May 7 09:23:04 keepalived222 Keepalived[35229]: '/usr/local/keepalived/temp/keepalived.pid' '-D'
May 7 09:23:04 keepalived222 Keepalived[35229]: Opening file '/usr/local/keepalived/etc/keepalived/keepalived.conf'.
May 7 09:23:04 keepalived222 Keepalived[35230]: Starting VRRP child process, pid=35231
May 7 09:23:04 keepalived222 systemd: Started LVS and VRRP High Availability Monitor.
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: Registering Kernel netlink reflector
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: Registering Kernel netlink command channel
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: Opening file '/usr/local/keepalived/etc/keepalived/keepalived.conf'.
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: Assigned address 192.168.10.147 for interface ens33
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: Assigned address fe80::4f78:60f1:9d7d:3da0 for interface ens33
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: Registering gratuitous ARP shared channel
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: (VI_1) removing VIPs.
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: (VI_1) Entering BACKUP STATE (init) #可以看到 keepalived222 初始化的状态为 BACKUP。
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: VRRP sockpool: [ifindex(2), family(IPv4), proto(112), unicast(0), fd(11,12)]
May 7 09:23:04 keepalived222 Keepalived_vrrp[35231]: VRRP_Script(nginx_status) succeeded #这里可以看出 nginx_status 模块定义的脚本是正确执行的。
3 关闭 keepalived111 节点的 nginx
keepalived111 日志:
May 7 09:24:09 keepalived111 Keepalived_vrrp[39826]: Script `nginx_status` now returning 1 #这里可以看到脚本的返回值为1,即表示健康检查失败,我的权重配置的是-10,这是优先级就要减少10。
May 7 09:24:12 keepalived111 Keepalived_vrrp[39826]: VRRP_Script(nginx_status) failed (exited with status 1)
May 7 09:24:12 keepalived111 Keepalived_vrrp[39826]: (VI_1) Changing effective priority from 100 to 90 #keepalived111 的优先级降低至90。
May 7 09:24:16 keepalived111 Keepalived_vrrp[39826]: (VI_1) Master received advert from 192.168.10.147 with higher priority 95, ours 90 #keepalived222 的优先级为95,大于自己的优先级。
May 7 09:24:16 keepalived111 Keepalived_vrrp[39826]: (VI_1) Entering BACKUP STATE #+ 所以 keepalived111 的状态切换为 BACKUP。
May 7 09:24:16 keepalived111 Keepalived_vrrp[39826]: (VI_1) removing VIPs. #+ 并删除vip。
keepalived222 日志:
May 7 09:24:13 keepalived222 Keepalived_vrrp[35231]: (VI_1) received lower priority (90) advert from 192.168.10.146 - discarding #这里收到广播:keepalived111的优先级变为90,比自身低,
May 7 09:24:14 keepalived222 Keepalived_vrrp[35231]: (VI_1) received lower priority (90) advert from 192.168.10.146 - discarding
May 7 09:24:15 keepalived222 Keepalived_vrrp[35231]: (VI_1) received lower priority (90) advert from 192.168.10.146 - discarding
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: (VI_1) Receive advertisement timeout
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: (VI_1) Entering MASTER STATE #由于没有配置延时抢占时间,所以 keepalived222 发现自身的优先级高时,只在几秒内状态切换为 MASTER。
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: (VI_1) setting VIPs.
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150 #+ 并获取到vip。
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.150
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:24:16 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:24:21 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
4 启动 keepalived111 节点的 nginx
keepalived111 日志:
May 7 09:30:45 keepalived111 Keepalived_vrrp[39826]: Script `nginx_status` now returning 0 #脚本的返回值为0,即表示健康检查成功,优先级还原。
May 7 09:30:48 keepalived111 Keepalived_vrrp[39826]: VRRP_Script(nginx_status) succeeded
May 7 09:30:48 keepalived111 Keepalived_vrrp[39826]: (VI_1) Changing effective priority from 90 to 100 #优先级还原为100.
May 7 09:30:49 keepalived111 Keepalived_vrrp[39826]: (VI_1) start preempt delay (30.000000) #由于设置了在 30 秒之后再抢占为 MASTER,所以,这里等待 30 秒。
#+ 这里的延时抢占主要就是为了防止在修复故障时,还未完全修复成功的情况下,多次临时启动程序会导致 vip 的来回切换,影响其他方面的问题。我这里为了试验方便,设置为30秒,可以根据环境适当增大。
May 7 09:31:19 keepalived111 Keepalived_vrrp[39826]: (VI_1) received lower priority (95) advert from 192.168.10.147 - discarding
May 7 09:31:20 keepalived111 Keepalived_vrrp[39826]: (VI_1) received lower priority (95) advert from 192.168.10.147 - discarding
May 7 09:31:21 keepalived111 Keepalived_vrrp[39826]: (VI_1) received lower priority (95) advert from 192.168.10.147 - discarding
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: (VI_1) Receive advertisement timeout
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: (VI_1) Entering MASTER STATE #可以看到在30秒到40秒之间才切换为 MASTER。
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: (VI_1) setting VIPs.
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150 #+ 并获取到vip。
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.150
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:31:22 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:31:27 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
keepalived222 日志:
May 7 09:31:22 keepalived222 Keepalived_vrrp[35231]: (VI_1) Master received advert from 192.168.10.146 with higher priority 100, ours 95
May 7 09:31:22 keepalived222 Keepalived_vrrp[35231]: (VI_1) Entering BACKUP STATE #keepalived222 的状态切换为 BACKUP。
May 7 09:31:22 keepalived222 Keepalived_vrrp[35231]: (VI_1) removing VIPs. #+ 并删除vip。
至此,实验完毕。下面的实验步骤其实完全没有必要。
5 关闭 keepalived222 节点的 nginx
keepalived111 节点无日志
keepalived222 节点的日志:
May 7 09:35:25 keepalived222 Keepalived_vrrp[35231]: Script `nginx_status` now returning 1
May 7 09:35:28 keepalived222 Keepalived_vrrp[35231]: VRRP_Script(nginx_status) failed (exited with status 1)
May 7 09:35:28 keepalived222 Keepalived_vrrp[35231]: (VI_1) Changing effective priority from 95 to 85
因为 keepalived111 的优先级本身就高,所以在 keepalived222 发生异常时,状态不会发生任何改变。
6 再关闭 keepalived111 节点的 nginx
keepalived111 节点的日志:
May 7 09:36:07 keepalived111 Keepalived_vrrp[39826]: Script `nginx_status` now returning 1
May 7 09:36:10 keepalived111 Keepalived_vrrp[39826]: VRRP_Script(nginx_status) failed (exited with status 1)
May 7 09:36:10 keepalived111 Keepalived_vrrp[39826]: (VI_1) Changing effective priority from 100 to 90
keepalived222 节点无日志
这种情况下,状态也不会发生任何改变。因为 keepalived111 的优先级还是高于 keepalived222。
7 先启动 keepalived222 节点的 nginx
keepalived111 节点的日志:
May 7 09:36:53 keepalived111 Keepalived_vrrp[39826]: (VI_1) Master received advert from 192.168.10.147 with higher priority 95, ours 90
May 7 09:36:53 keepalived111 Keepalived_vrrp[39826]: (VI_1) Entering BACKUP STATE
May 7 09:36:53 keepalived111 Keepalived_vrrp[39826]: (VI_1) removing VIPs.
keepalived222 节点的日志:
May 7 09:36:46 keepalived222 Keepalived_vrrp[35231]: Script `nginx_status` now returning 0
May 7 09:36:49 keepalived222 Keepalived_vrrp[35231]: VRRP_Script(nginx_status) succeeded
May 7 09:36:49 keepalived222 Keepalived_vrrp[35231]: (VI_1) Changing effective priority from 85 to 95
May 7 09:36:50 keepalived222 Keepalived_vrrp[35231]: (VI_1) received lower priority (90) advert from 192.168.10.146 - discarding
May 7 09:36:51 keepalived222 Keepalived_vrrp[35231]: (VI_1) received lower priority (90) advert from 192.168.10.146 - discarding
May 7 09:36:52 keepalived222 Keepalived_vrrp[35231]: (VI_1) received lower priority (90) advert from 192.168.10.146 - discarding
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: (VI_1) Receive advertisement timeout
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: (VI_1) Entering MASTER STATE
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: (VI_1) setting VIPs.
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.150
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:36:53 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:36:58 keepalived222 Keepalived_vrrp[35231]: Sending gratuitous ARP on ens33 for 192.168.1.150
先启动 keepalived222 节点的 nginx,优先级还原为 85,大于 keepalived111 故障时的优先级 90,所以 keepalived222 的状态切换为 MASTER。
8 再启动 keepalived111 节点的 nginx
keepalived111 节点的日志:
May 7 09:39:52 keepalived111 Keepalived_vrrp[39826]: Script `nginx_status` now returning 0
May 7 09:39:55 keepalived111 Keepalived_vrrp[39826]: VRRP_Script(nginx_status) succeeded
May 7 09:39:55 keepalived111 Keepalived_vrrp[39826]: (VI_1) Changing effective priority from 90 to 100
May 7 09:39:55 keepalived111 Keepalived_vrrp[39826]: (VI_1) start preempt delay (30.000000) #这里还是等待30秒。
May 7 09:40:25 keepalived111 Keepalived_vrrp[39826]: (VI_1) received lower priority (95) advert from 192.168.10.147 - discarding
May 7 09:40:26 keepalived111 Keepalived_vrrp[39826]: (VI_1) received lower priority (95) advert from 192.168.10.147 - discarding
May 7 09:40:27 keepalived111 Keepalived_vrrp[39826]: (VI_1) received lower priority (95) advert from 192.168.10.147 - discarding
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: (VI_1) Receive advertisement timeout
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: (VI_1) Entering MASTER STATE
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: (VI_1) setting VIPs.
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.150
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:40:28 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:40:33 keepalived111 Keepalived_vrrp[39826]: Sending gratuitous ARP on ens33 for 192.168.1.150
May 7 09:40:33 keepalived111 Keepalived_vrrp[39826]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.150
keepalived222 节点的日志:
May 7 09:40:28 keepalived222 Keepalived_vrrp[35231]: (VI_1) Master received advert from 192.168.10.146 with higher priority 100, ours 95
May 7 09:40:28 keepalived222 Keepalived_vrrp[35231]: (VI_1) Entering BACKUP STATE
May 7 09:40:28 keepalived222 Keepalived_vrrp[35231]: (VI_1) removing VIPs.
keepalived111 还是等待 30 秒后再抢占为 MASTER。
这里没有展示在 keepalived111 和 keepalived222 的 nginx 都关闭的情况下,先启动keepalived111 的 nginx 时的日志。大家稍微想一下肯定就知道,当 keepalived111 和 keepalived222 的 nginx 都关闭时,还是 keepalived111 的优先级高,所以它俩的状态都不会发生变化,vip 也不会变化;先启动 keepalived111 的 nginx 后,还是 keepalived111 的优先级高,所以它俩的状态还是不会发生变化,vip 也不会变化。
最后查看一下 keepalived 状态切换时脚本执行的日志
keepalived111 节点
[root@keepalived111 ~]# cat /tmp/keepalived_change.log
当前节点于 2021-05-07 09:20:09 切换为 BACKUP #刚开始初始化的状态为 BACKUP
当前节点于 2021-05-07 09:20:13 切换为 MASTER #由于只启动了 keepalived111 节点,所以几秒后状态切换为 MASTER
当前节点于 2021-05-07 09:24:16 切换为 BACKUP #关闭 nginx 后,状态切换为 BACKUP
当前节点于 2021-05-07 09:31:22 切换为 MASTER #启动 nginx 后,状态切换为 MASTER
当前节点于 2021-05-07 09:36:53 切换为 BACKUP
当前节点于 2021-05-07 09:40:28 切换为 MASTER
keepalived222 节点
[root@keepalived222 ~]# cat /tmp/keepalived_change.log
当前节点于 2021-05-07 09:23:04 切换为 BACKUP #刚开始初始化的状态为 BACKUP
当前节点于 2021-05-07 09:24:16 切换为 MASTER #keepalived111 节点关闭 nginx 后,状态切换为 MASTER
当前节点于 2021-05-07 09:31:22 切换为 BACKUP #keepalived111 节点启动 nginx 后,状态切换为 BACKUP
当前节点于 2021-05-07 09:36:53 切换为 MASTER
当前节点于 2021-05-07 09:40:28 切换为 BACKUP