有没有办法使用服务器实例内的命令行查看当前状态(主/备份)?
干杯
答案1
您可以使用通知命令来写出状态文件。
# for ANY state transition.
# "notify" script is called AFTER the
# notify_* script(s) and is executed
# with 3 arguments provided by keepalived
# (ie don’t include parameters in the notify line).
# arguments
# $1 = "GROUP"|"INSTANCE"
# $2 = name of group or instance
# $3 = target state of transition
# ("MASTER"|"BACKUP"|"FAULT")
notify /path/notify.sh
创建一个通知脚本,例如:
#!/bin/bash
# notify.sh
echo $1 $2 is in $3 state > /var/run/keepalive.$1.$2.state
获取状态脚本如下:
#!/bin/bash
# getstate.sh
cat /var/run/keepalive.*.*.state
答案2
可以通过发送USR2
信号来转储当前状态保持活跃父进程:
kill -USR2 $(cat /var/run/keepalived.pid)
查看文件/tmp/keepalived.stats
。以下是结果示例(例如Keepalived v2.0.7):
掌握实例:
VRRP Instance: Server1 Advertisements: ... Became master: 15 <--- one more than the Released counter Released master: 14 Packet Errors: ...
备份实例:
VRRP Instance: Server2 Advertisements: ... Became master: 33 <--- equal to Released counter Released master: 33 Packet Errors: ...
笔记:如果SELinux用来 (CentOS 7),这可能会阻止写入此文件。你可以用这个来解决这个问题:
touch /tmp/keepalived.stats
chmod go+w /tmp/keepalived.stats
chcon -t keepalived_var_run_t /tmp/keepalived.stats
现在尝试再次发送信号。
答案3
通过 SNMP 读取当前状态对我来说是最可靠的。要启用此功能,您必须启动具有 snmp 支持的 keepalived:
- 将 -x 添加到守护进程选项(请参阅基于 RedHat 的系统上的 /etc/sysconfig/keepalived)
并安装 snmpd。
然后您可以通过以下方式可靠地查询状态
snmpget -Oq -Ov -v2c -cpublic localhost KEEPALIVED-MIB::vrrpInstanceState.1
它也可以通过通知脚本来完成,但这些脚本并不总是触发,导致状态文件与现实不同步。
答案4
数据总线
附有版本1.3.0,Keepalived 添加了 DBus 接口†,用于查询 VRRP 状态、使用信号监视 VRRP 事件,甚至在运行时修改某些 VRRP 配置。截至 2021 年,该接口仅用于 VRRP 功能,不公开与 Keepalived 的 IPVS 子系统相关的任何内容。使用配置文件块enable_dbus
中的选项启用 DBus 支持global_defs
。还可以指定一些其他选项(此处未说明)。
界面
您可以使用任何 DBus 客户端/库(例如、、dbus-send
)访问 DBus 接口。我在这里使用 systemd作为示例,因为它有一个非常好的界面:gdbus
qdbus
busctl
# busctl tree org.keepalived.Vrrp1
└─/org
└─/org/keepalived
└─/org/keepalived/Vrrp1
├─/org/keepalived/Vrrp1/Instance
│ └─/org/keepalived/Vrrp1/Instance/eth0
│ └─/org/keepalived/Vrrp1/Instance/eth0/1
│ └─/org/keepalived/Vrrp1/Instance/eth0/1/IPv4
└─/org/keepalived/Vrrp1/Vrrp
有两个接口可用,全局org.keepalived.Vrrp1.Vrrp
的/org/keepalived/Vrrp1/Vrrp
:
# busctl introspect org.keepalived.Vrrp1 /org/keepalived/Vrrp1/Vrrp org.keepalived.Vrrp1.Vrrp
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
.CreateInstance method ssuu - -
.DestroyInstance method s - -
.PrintData method - - -
.PrintStats method - - -
.ReloadConfig method - - -
.VrrpReloaded signal - - -
.VrrpStarted signal - - -
.VrrpStopped signal - - -
对于每个 VRRP 实例,org.keepalived.Vrrp1.Instance
根据此模板在路径上设置接口。对于 id为的/org/keepalived/Vrrp1/Instance/<interface>/<virtual-router-id>/<ip-family>
IPv4 VRRP 实例,我们得到以下内容:1
eth1
my-instance
# busctl introspect org.keepalived.Vrrp1 /org/keepalived/Vrrp1/Instance/eth0/1/IPv4 org.keepalived.Vrrp1.Instance
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
.SendGarp method - - -
.Name property s "my-instance" emits-change
.State property (us) 2 "Master" emits-change
.VrrpStatusChange signal u - -
如果您使用多个 keepalived 进程并设置该instance
选项,或者您正在使用带有该选项的网络命名空间功能namespace
,则路径还包括实例和/或命名空间。
查询状态
要获取特定实例的状态,我们可以使用以下命令
# busctl get-property org.keepalived.Vrrp1 /org/keepalived/Vrrp1/Instance/eth0/1/IPv4 org.keepalived.Vrrp1.Instance State
(us) 2 "Master"
返回的属性是一个 STRUCT,带有状态代码和人类可读的名称。重要的状态是、、、0 "Init"
。前三个是官方的 RFC 2338 状态,最后一个由 Keepalived 定义。不同版本的 Keepalived 有一些额外的状态。从 2.2.4 版开始,例如还有和。直到 2.0.0 版,例如还有和代码 98 具有不同的语义,因此您的代码应该主要依赖这四个主要状态,并处理新的状态代码。1 "Backup"
2 "Master"
3 "Fault"
97 "Stop"
98 "Deleted"
4 "Goto master"
98 "Goto fault"
监控信号
您还可以使用 DBus 信号监视状态变化。以下输出显示启动、引入故障和停止 Keepalived 实例时的 DBus 信号:
# busctl monitor --match="type='signal',sender='org.keepalived.Vrrp1'
‣ Type=signal Endian=l Flags=1 Version=1 Priority=0 Cookie=5
Sender=:1.80 Path=/org/keepalived/Vrrp1/hades_root/Instance/br_vrrp/67/IPv4 Interface=org.keepalived.Vrrp1.Instance Member=VrrpStatusChange
UniqueName=:1.80
MESSAGE "u" {
UINT32 1;
};
‣ Type=signal Endian=l Flags=1 Version=1 Priority=0 Cookie=6
Sender=:1.80 Path=/org/keepalived/Vrrp1/Instance/eth0/1/IPv4 Interface=org.keepalived.Vrrp1.Instance Member=VrrpStatusChange
UniqueName=:1.80
MESSAGE "u" {
UINT32 2;
};
‣ Type=signal Endian=l Flags=1 Version=1 Priority=0 Cookie=7
Sender=:1.80 Path=/org/keepalived/Vrrp1/hades_root/Instance/br_vrrp/67/IPv4 Interface=org.keepalived.Vrrp1.Instance Member=VrrpStatusChange
UniqueName=:1.80
MESSAGE "u" {
UINT32 3;
};
‣ Type=signal Endian=l Flags=1 Version=1 Priority=0 Cookie=8
Sender=:1.80 Path=/org/keepalived/Vrrp1/hades_root/Instance/br_vrrp/67/IPv4 Interface=org.keepalived.Vrrp1.Instance Member=VrrpStatusChange
UniqueName=:1.80
MESSAGE "u" {
UINT32 1;
};
‣ Type=signal Endian=l Flags=1 Version=1 Priority=0 Cookie=9
Sender=:1.80 Path=/org/keepalived/Vrrp1/hades_root/Instance/br_vrrp/67/IPv4 Interface=org.keepalived.Vrrp1.Instance Member=VrrpStatusChange
UniqueName=:1.80
MESSAGE "u" {
UINT32 2;
};
‣ Type=signal Endian=l Flags=1 Version=1 Priority=0 Cookie=7
Sender=:1.80 Path=/org/keepalived/Vrrp1/Instance/eth0/1/IPv4 Interface=org.keepalived.Vrrp1.Instance Member=VrrpStatusChange
UniqueName=:1.80
MESSAGE "u" {
UINT32 98;
};
‣ Type=signal Endian=l Flags=1 Version=1 Priority=0 Cookie=8
Sender=:1.80 Path=/org/keepalived/Vrrp1/Vrrp Interface=org.keepalived.Vrrp1.Vrrp Member=VrrpStopped
UniqueName=:1.80
MESSAGE "" {
};
我们观察到:
- 当 Keepalived 启动时,不会
INIT
发出任何状态信号。 - 实例转换到
BACKUP
状态(state BACKUP
使用配置)。 - 实例转换到
MASTER
状态。 - 检测到故障并且实例转换到
FAULT
状态。 - 实例通过再次前往来
BACKUP
恢复MASTER
。 - Keepalived 停止并且实例转换到该
STOP
状态。 - Keepalived 还发出全局
VrrpStopped
信号。
†不过,Keepalived 必须使用--enable-dbus
configure
此选项才能正常工作。不过,大多数二进制发行版现在应该已经启用了它。例如,Debian 和 Ubuntu 就是这样做的自软件包版本 1:1.3.9-1 起。