拓补结构

以 3 个 Sentinel 节点,1 主 2 从作为示例

image.png

各节点信息如下:

角色 IP PORT 别名
master 0.0.0.0 6379 Redis 主节点
slave-1 0.0.0.0 6380 Redis 从节点1
slave-2 0.0.0.0 6381 Redis 从节点2
sentinel-1 0.0.0.0 16379 Redis Sentinel节点1
sentinel-2 0.0.0.0 16380 Redis Sentinel节点2
sentinel-3 0.0.0.0 16381 Redis Sentinel节点3

部署流程

启动 Redis 节点服务

节点 master slave-1 slave-2
节点信息 0.0.0.0:6379 0.0.0.0:6380 0.0.0.0:6381
配置文件路径
示例
/etc/redis/6379.conf /etc/redis/6380.conf /etc/redis/6381.conf
redis 配置
基础配置,示例
port 6379
daemonize yes
pidfile /tmp/redis-6379.pid
logfile /tmp/redis-6379.log
dir /tmp/
port 6380
daemonize yes
pidfile /tmp/redis-6380.pid
logfile /tmp/redis-6380.log
dir /tmp/
REPLICAOF 0.0.0.0 6379
port 6381
daemonize yes
pidfile /tmp/redis-6381.pid
logfile /tmp/redis-6381.log
dir /tmp/
REPLICAOF 0.0.0.0 6379
启动服务 redis-sever /etc/redis/6379.conf redis-sever /etc/redis/6380.conf redis-sever /etc/redis/6381.conf

配置说明:

  1. port 6380 # 启动端口
  2. daemonize yes # 常驻进程
  3. pidfile /tmp/redis-6380.pid # 进程文件
  4. logfile /tmp/redis-6380.log # 日志文件路径
  5. dir /tmp/ # 启动目录
  6. REPLICAOF 0.0.0.0 6379 # 从机才需要配置,配置主节点信息

启动 Sentinel 服务

节点 sentinel-1 sentinel-2 sentinel-3
节点信息 0.0.0.0:16379 0.0.0.0:16380 0.0.0.0:16371
配置文件路径
示例
/etc/redis/sentinel-16379.conf /etc/redis/sentinel-16380.conf /etc/redis/sentinel-16381.conf
redis 配置
基础配置,示例
port 16379
daemonize yes
pidfile /tmp/redis-sentinel-16379.pid
logfile “16379.log”
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
port 16380
daemonize yes
pidfile /tmp/redis-sentinel-16380.pid
logfile “16380.log”
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
port 16381
daemonize yes
pidfile /tmp/redis-sentinel-16381.pid
logfile “16381.log”
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
启动服务 redis-sentinel sentinel-16379.conf redis-sentinel sentinel-16380.conf redis-sentinel sentinel-16381.conf

Sentinel 主要配置

  1. port 26379 # 节点端口信息
  2. dir /tmp/ # 运行目录
  3. logfile sentinel-26379.log # 日志目录
  4. # 表示当前26379的sentinel节点需要监控127.0.0.1:6379这个主节点。
  5. # 2代表需要至少需要两个sentinel节点去判定 Redis 主节点是否异常
  6. # mymaster 是 Redis 主节点别名
  7. sentinel monitor mymaster 127.0.0.1 6379 2
  8. # 通过 ping 方式判定 Redis 节点正异常,若超过指定时间未得到回复,则判定为异常
  9. sentinel down-after-milliseconds mymaster 30000
  10. sentinel parallel-syncs mymaster 1
  11. sentinel failover-timeout mymaster 180000

Sentinel 启动后,原始的 conf 配置会变更,且会自动发现从机节点配置。如下为 16379.conf 变更配置:

port 16379
daemonize yes
pidfile "/tmp/redis-sentinel-16379.pid"
logfile "16379.log"
dir "/tmp"
sentinel myid 520737090ccee6c645e5d8cae945154e082e99ee
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# Generated by CONFIG REWRITE
user default on nopass ~* +@all
sentinel known-replica mymaster 127.0.0.1 7002
sentinel known-replica mymaster 127.0.0.1 7001
sentinel known-sentinel mymaster 127.0.0.1 16381 630535ab25b0b6a4d8ab2f59f72bd8057c86f681
sentinel known-sentinel mymaster 127.0.0.1 16380 eef13d51413ca86f81c3386339c9562340e53f31
sentinel current-epoch 0

查看 Sentinel 信息: redis-cli -p 16379 info Sentinel ,如下:

# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3 # 2个从机,三个sentinel节点

故障转移

执行 redis-cli -p 6379 shutdown 将 master 节点关机后,发现自动切换了 master 节点至 7002,并将 7001 节点的主节点复制由 6379 变更为 7002。

如下为 7002 节点日志:

48:S 19 Oct 2020 15:58:50.795 # Connection with master lost.
48:S 19 Oct 2020 15:58:50.795 * Caching the disconnected master state.
48:S 19 Oct 2020 15:58:51.027 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:51.027 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:51.027 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:52.046 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:52.046 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:52.046 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:53.056 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:53.056 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:53.056 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:54.067 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:54.067 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:54.067 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:55.084 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:55.085 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:55.085 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:56.098 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:56.099 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:56.099 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:57.114 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:57.114 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:57.114 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:58.126 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:58.126 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:58.126 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:58:59.143 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:58:59.144 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:58:59.144 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:00.161 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:00.162 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:00.162 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:01.176 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:01.176 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:01.176 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:02.187 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:02.188 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:02.188 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:03.200 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:03.200 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:03.200 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:04.210 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:04.211 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:04.211 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:05.228 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:05.229 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:05.229 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:06.242 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:06.243 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:06.243 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:07.255 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:07.256 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:07.256 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:08.270 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:08.270 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:08.270 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:09.289 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:09.289 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:09.289 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:10.304 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:10.304 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:10.305 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:11.322 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:11.322 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:11.322 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:12.344 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:12.344 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:12.344 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:13.362 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:13.362 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:13.362 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:14.377 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:14.377 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:14.377 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:15.385 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:15.386 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:15.386 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:16.400 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:16.400 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:16.400 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:17.412 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:17.412 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:17.412 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:18.427 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:18.428 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:18.428 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:19.444 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:19.444 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:19.445 # Error condition on socket for SYNC: Operation now in progress
48:S 19 Oct 2020 15:59:20.453 * Connecting to MASTER 127.0.0.1:6379
48:S 19 Oct 2020 15:59:20.454 * MASTER <-> REPLICA sync started
48:S 19 Oct 2020 15:59:20.454 # Error condition on socket for SYNC: Operation now in progress
48:M 19 Oct 2020 15:59:21.184 * Discarding previously cached master state.
48:M 19 Oct 2020 15:59:21.184 # Setting secondary replication ID to ce0e8431792660e6580a311f893e4f711c1059bf, valid up to offset: 12407. New replication ID is b271da96dbcd40ba56d4a0be556735f6651baaeb
48:M 19 Oct 2020 15:59:21.184 * MASTER MODE enabled (user request from 'id=9 addr=127.0.0.1:49346 fd=13 name=sentinel-630535ab-cmd age=91 idle=0 flags=x db=0 sub=0 psub=0 multi=4 qbuf=188 qbuf-free=32580 obl=45 oll=0 omem=0 events=r cmd=exec user=default')
48:M 19 Oct 2020 15:59:21.184 # CONFIG REWRITE executed with success.
48:M 19 Oct 2020 15:59:22.483 * Replica 127.0.0.1:7001 asks for synchronization
48:M 19 Oct 2020 15:59:22.483 * Partial resynchronization request from 127.0.0.1:7001 accepted. Sending 555 bytes of backlog starting from offset 12407.
48:M 19 Oct 2020 16:00:37.365 * Replica 127.0.0.1:6379 asks for synchronization
48:M 19 Oct 2020 16:00:37.366 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '61ddd9026e83a9ca5f8a5ba19fe2d8c78992c2f4', my replication IDs are 'b271da96dbcd40ba56d4a0be556735f6651baaeb' and 'ce0e8431792660e6580a311f893e4f711c1059bf')