前言

当前操作系统:Windows 10,Redis 版本:5.0.10 for Windows

哨兵模式简单来说是在主从模式下,添加哨兵进程以选举从节点替换宕机主节点的模式

主从模式

在了解哨兵模式前,得先将机器配置主从关系,以如下两台机器为例:

  • 192.168.67.130
  • 192.168.67.1

接下来我打算将 192.168.67.130 配置为主节点(Master),而 192.168.67.1 从节点(Replica),一下我们以 M1 和 R1 分别代表两台机器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 配置 M1 的 bind 为 192.168.67.130 以确保能被其他的客户端连接
bind 192.168.67.130
# 同样的,配置 R1 的 bind 为 192.168.67.1 以确保能被其他的客户端连接
bind 192.168.67.1
# 在 R1 的配置中设置 M1 为主从关系
slaveof 192.168.67.130 6379
# 当然也可以使用客户端连接 R1 后通过 SLAVEOF 指令配置主从关系
192.168.67.1:6379> SLAVEOF 192.168.67.130 6379
OK
# 关联成功后 M1 和 R1 的 Redis 会将日志打印出来
# M1 打印的日志如下:
[7648] 01 Sep 17:04:39.883 * Starting BGSAVE for SYNC with target: disk
[7648] 01 Sep 17:04:39.898 * Background saving started by pid 8684
[7648] 01 Sep 17:04:40.040 # fork operation complete
[7648] 01 Sep 17:04:40.040 * Background saving terminated with success
[7648] 01 Sep 17:04:40.055 * Synchronization with replica 192.168.67.134:6379 succeeded
# R1 打印的日志如下:
[3468] 05 Sep 17:12:28.131 * Connecting to MASTER 192.168.67.130:6379
[3468] 05 Sep 17:12:28.131 * MASTER <-> REPLICA sync started
[3468] 05 Sep 17:12:28.131 * Non blocking connect for SYNC fired the event.
[3468] 05 Sep 17:12:28.135 * Master replied to PING, replication can continue...
[3468] 05 Sep 17:12:28.138 * Trying a partial resynchronization (request f3f4c3ebe082784cb0ad8b58703478619c2fe3bb:572504).
[3468] 05 Sep 17:12:28.245 * Full resync from master: fd9a55223a42dcc57c8cbab087dcf66a4db20d52:0
[3468] 05 Sep 17:12:28.245 * Discarding previously cached master state.
[3468] 05 Sep 17:12:28.485 * MASTER <-> REPLICA sync: receiving 48086 bytes from master
[3468] 05 Sep 17:12:28.487 * MASTER <-> REPLICA sync: Flushing old data
[3468] 05 Sep 17:12:28.488 * MASTER <-> REPLICA sync: Loading DB in memory
[3468] 05 Sep 17:12:28.496 * MASTER <-> REPLICA sync: Finished with success
# 看到类似成功提示的日志,说明主备已经关联。
# 如果你还不放心,你可以通关客户端分别连接到 M1 和 R1 并输入指令 info replication 来查看主备情况。
# M1 显示如下:
role:master
connected_slaves:1
# R1 显示如下:
role:slave
master_host:192.168.67.130
master_port:6379
master_link_status:up
# M1 的 connected_slaves:1 和 R1 的 master_link_status:up 足以说明主从关系已经建立成功

值得注意的是 Redis 5 之后的版本中有了一个新的指令 REPLICAOF 来迭代 SLAVEOF 指令,用于复制数据和建立主从关系。

哨兵模式

在官方提供的版本中默认有一个 sentinel.conf 配置文件,其中有基础的两个参数是需要注意的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# port <sentinel-port>
# The port that this sentinel instance will run on
# 译:哨兵进程将通过此端口运行
port 26379

# sentinel monitor <master-name> <ip> <redis-port> <quorum>
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
# 译:通知 Sentinel 监视指定的 Master,并且只有在至少 <quorum> 哨兵同意时才考虑它处于 O_DOWN(客观关闭)状态。
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Replicas are auto-discovered, so you don't need to specify replicas in
# any way. Sentinel itself will rewrite this configuration file adding
# the replicas using additional configuration options.
# Also note that the configuration file is rewritten when a
# replica is promoted to master.
#
# Note: master name should not include special characters or spaces.
# <master-name> 为自定义项,但是不能包含特殊字符或空格。
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster 192.168.67.130 6379 1

1.哨兵进程端口一般以 Redis 端口前面加个 2 作为进程端口,当然得先确保 26379 为空闲端口。

2.只需要配置监听主节点的端口,从节点会被自动发现。

根据上面的配置新建一个 sentinel.conf 文件并写入,然后通过 redis-server.exe 运行哨兵进程

1
2
3
4
5
6
# redis-server.exe <configure-path> --sentinel
redis-server.exe sentinel.conf --sentinel
# 运行后也会像运行 Redis 一样,打印出相关的内容以及日志:
[8540] 06 Sep 09:20:02.646 # Sentinel ID is 6b2c189b490d72a61c73735e08702980635c13aa
[8540] 06 Sep 09:20:02.646 # +monitor master mymaster 192.168.67.130 6379 quorum 1
[8540] 06 Sep 09:20:02.648 * +slave slave 192.168.67.1:6379 192.168.67.1 6379 @ mymaster 192.168.67.130 6379

这里我们可以看到 Sentinel 给自己建了一个 ID,之后 +monitor 表示主节点被发现,随后 +slave 表示一个从节点被发现。

其他