Sentinel管理命令

正如用户可以使用命令操纵数据库一样,Redis 也为用户提供了相应的 Sentinel 管理命令,用于对 Sentinel 执行各式各样的管理操作,下面将分别对这些 Sentinel 管理命令进行介绍。

SENTINEL masters:获取所有被监视主服务器的信息

通过向 Sentinel 发送以下命令,用户可以获得 Sentinel 正在监视的所有主服务器的相关信息:

SENTINEL masters

作为例子,以下是向 Sentinel 26379 发送 SENTINEL masters 命令的结果:

127.0.0.1:26379> SENTINEL masters
1) 1) "name"
2) "website_db"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "runid"
8) "bdde01381c8d403877a143f627b24615a5f03394"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "143"
19) "last-ping-reply"
20) "143"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "2849"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "163505"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

因为这个 Sentinel 目前只监视了 127.0.0.1:26379 一个主服务器,所以 SENTINEL masters 命令只返回了一个主服务器的信息。表19-1 列出了命令返回的各个字段的意义。

image 2025 01 06 14 17 19 204
Figure 1. 表19-1 SENTINEL masters命令返回的各个字段及其意义

其他信息

  • 复杂度:O(N),其中 N 为 Sentinel 正在监视的主服务器数量。

  • 版本要求:SENTINEL masters 命令从 Redis 2.8.0 版本开始可用。

SENTINEL master:获取指定被监视主服务器的信息

如果你只想获取特定主服务器的信息而不是全部主服务器的信息,可以使用以下命令代替 SENTINEL masters 命令:

SENTINEL master <master-name>

这个命令只会返回用户指定主服务器的相关信息。

以下代码展示了使用 SENTINEL master 命令获取 website_db 主服务器信息的例子:

127.0.0.1:26379> SENTINEL master website_db
1) "name"
2) "website_db"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "runid"
8) "bdde01381c8d403877a143f627b24615a5f03394"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "57"
19) "last-ping-reply"
20) "57"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "1611"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "212401"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

SENTINEL master 命令返回的所有字段及其意义都与 SENTINEL masters 命令返回的字段一样。

其他信息

  • 复杂度:O(1)。

  • 版本要求:SENTINEL master 命令从 Redis 2.8.0 版本开始可用。

SENTINEL slaves:获取被监视主服务器的从服务器信息

通过使用以下命令,用户可以让 Sentinel 返回指定主服务器属下所有从服务器的相关信息:

SENTINEL slaves <master-name>

举个例子,如果我们想要获取 website_db 主服务器属下所有从服务器的相关信息,那么可以执行以下命令:

127.0.0.1:26379> SENTINEL slaves website_db
1) 1) "name"
2) "127.0.0.1:6380"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6380"
7) "runid"
8) "17f715be931c61f33a52459b20f3577de3bee7e0"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "93"
19) "last-ping-reply"
20) "93"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "8563"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "88988"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "127.0.0.1"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "16209"
2) 1) "name"
2) "127.0.0.1:6381"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6381"
7) "runid"
8) "a8f6a69a5107521606bbd3e40a201303def56daf"
...
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "16209"

在 SENTINEL slaves 命令返回的字段当中,大部分字段都与 SENTINEL masters 命令返回的字段相同,表19-2 介绍了一些之前尚未介绍过的从服务器专有字段。

image 2025 01 06 14 24 37 010
Figure 2. 表19-2 SENTINEL slaves命令返回的各个字段及其意义

其他信息

  • 复杂度:O(N),其中 N 为指定主服务器属下的从服务器数量。

  • 版本要求:SENTINEL slaves 命令从 Redis 2.8.0 版本开始可用。

SENTINEL sentinels:获取其他Sentinel的相关信息

用户可以通过执行以下命令,获取监视同一主服务器的其他所有 Sentinel 的相关信息:

SENTINEL sentinels <master-name>

以下是我们对 Sentinel 26379 发送 SENTINEL sentinels 命令,让它列出正在监视主服务器 website_db 的其他 Sentinel 时的结果:

127.0.0.1:26379> SENTINEL sentinels website_db
1) 1) "name"
2) "939a82925c42ce3b8dd84fc62e840bea04167483"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "26380"
7) "runid"
8) "939a82925c42ce3b8dd84fc62e840bea04167483"
9) "flags"
10) "sentinel"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "1028"
19) "last-ping-reply"
20) "1028"
21) "down-after-milliseconds"
22) "30000"
23) "last-hello-message"
24) "1462"
25) "voted-leader"
26) "?"
27) "voted-leader-epoch"
28) "0"
2) 1) "name"
2) "dc6369084b94ac1bf7dcafc4aa9741d520898de6"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "26381"
...
27) "voted-leader-epoch"
28) "0"

SENTINEL sentinels 命令返回的大部分信息项都与 SENTINEL masters 命令以及 SENTINEL slaves 命令相同,表19-3 列出了其中尚未介绍过的 Sentinel 专有信息项。

image 2025 01 06 14 26 57 082
Figure 3. 表19-3 SENTINEL sentinels命令返回的各个字段及其意义

其他信息

  • 复杂度:O(N),其中 N 为正在监视给定主服务器的 Sentinel 数量。

  • 版本要求:SENTINEL sentinels 命令从 Redis 2.8.0 版本开始可用。

SENTINEL get-master-addr-by-name:获取给定主服务器的IP地址和端口号

用户可以通过执行以下命令,通过给定主服务器的名字来获取该服务器的 IP 地址以及端口号:

SENTINEL get-master-addr-by-name <master-name>

举个例子,如果我们想要获取 website_db 主服务器的 IP 地址和端口号,那么可以执行以下命令:

127.0.0.1:26379> SENTINEL get-master-addr-by-name website_db
1) "127.0.0.1"
2) "6379"

需要注意的是,如果 Sentinel 正在对给定主服务器执行故障转移操作,或者原本的主服务器已经因为故障转移而被新的主服务器替换掉了,那么这个命令将返回新主服务器的 IP 地址和端口号。

其他信息

  • 复杂度:O(1)。

  • 版本要求:SENTINEL get-master-addr-by-name 命令从 Redis 2.8.0 版本开始可用。

SENTINEL reset:重置主服务器状态

SENTINEL reset 命令接受一个 glob 风格的模式作为参数,接收到该命令的 Sentinel 将重置所有与给定模式相匹配的主服务器:

SENTINEL reset <pattern>

命令将返回被重置主服务器的数量作为返回值。

接收到 SENTINEL reset 命令的 Sentinel 除了会清理被匹配主服务器的相关信息之外,还会遗忘被匹配主服务器目前已有的所有从服务器,以及正在监视被匹配主服务器的所有其他 Sentinel。在此之后,这个 Sentinel 将会重新搜索正在监视被匹配主服务器的其他 Sentinel,以及该服务器属下的各个从服务器,并与它们重新建立连接。

作为例子,以下代码展示了重置 website_db 主服务器的方法:

127.0.0.1:26379> SENTINEL reset website_db
(integer) 1 --有一个主服务器被重置了

而以下代码则展示了对所有带有 website_ 前缀的主服务器进行重置的方法:

127.0.0.1:26379> SENTINEL reset website_*
(integer) 1

因为 SENTINEL reset 命令可以让 Sentinel 忘掉主服务器之前的记录,并重新开始对主服务器进行监视,所以它通常只会在 Sentinel 网络或者被监视主从服务器的结构出现重大变化时使用。

其他信息

  • 复杂度:O(N),其中 N 为被重置的主服务器数量。

  • 版本要求:SENTINEL reset 命令从 Redis 2.8.0 版本开始可用。

SENTINEL failover:强制执行故障转移

通过执行以下命令,用户可以强制对指定的主服务器实施故障转移,就好像它已经下线了一样:

SENTINEL failover <master-name>

接收到这一命令的 Sentinel 会直接对主服务器执行故障转移操作,而不会像平时那样,先在 Sentinel 网络中进行投票,然后再根据投票结果决定是否执行故障转移操作。

举个例子,如果我们想要让 Sentinel 26379 对 website_db 主服务器强制执行故障转移操作,那么可以执行以下命令:

127.0.0.1:26379> SENTINEL failover website_db
OK

其他信息

  • 复杂度:O(1)。

  • 版本要求:SENTINEL failover 命令从 Redis 2.8.0 版本开始可用。

SENTINEL ckquorum:检查可用Sentinel的数量

用户可以通过执行以下命令,检查 Sentinel 网络当前可用的 Sentinel 数量是否达到了判断主服务器客观下线并实施故障转移所需的数量:

SENTINEL ckquorum <master-name>

以下是我们对 Sentinel 26379 执行 SENTINEL ckquorum 命令的一个例子:

127.0.0.1:26379> SENTINEL ckquorum website_db
OK 3 usable Sentinels. Quorum and failover authorization can be reached

从结果可以看到,Sentinel 网络目前有 3 个 Sentinel 可用,这已经满足了判断 website_db 客观下线所需的 Sentinel 数量。

SENTINEL ckquorum 命令一般用于检查 Sentinel 网络的部署是否成功。比如,如果我们在部署了 3 个 Sentinel 之后,却发现 SENTINEL ckquorum 只能识别到 2 个可用的 Sentinel,那就说明有什么地方出错了。

其他信息

  • 复杂度:O(1)。

  • 版本要求:SENTINEL ckquorum 命令从 Redis 2.8.0 版本开始可用。

SENTINEL flushconfig:强制写入配置文件

用户可以通过向 Sentinel 发送以下命令,让 Sentinel 将它的配置文件重新写入硬盘中:

SENTINEL flushconfig

因为 Sentinel 在被监视服务器的状态发生变化时就会自动重写配置文件,所以这个命令的作用就是在配置文件基于某些原因或错误而丢失时,立即生成一个新的配置文件。此外,当 Sentinel 的配置选项发生变化时,Sentinel 内部也会使用这个命令创建新的配置文件来替换原有的配置文件。

举个例子,我们可以访问 Sentinel 配置文件所在的文件夹,并将该文件删除:

$ ls -l
total 8
-rw-r--r-- 1 huangz staff 550 1 21 21:30 sentinel.conf
$ rm sentinel.conf
$ ls -l
$
然后在客户端中执行重写命令:
127.0.0.1:26379> SENTINEL flushconfig
OK
这样Sentinel将重新生成一个配置文件:
$ ls -l
total 8
-rw-r--r-- 1 huangz staff 549 1 21 21:44 sentinel.conf

最后要注意的是,只有接收到 SENTINEL flushconfig 命令的 Sentinel 才会重写配置文件,Sentinel 网络中的其他 Sentinel 并不会受到这个命令的影响。

其他信息

  • 复杂度:O(1)。

  • 版本要求:SENTINEL flushconfig 命令从 Redis 2.8.0 版本开始可用。