Redis Sentinel
(哨兵或哨岗)为Redis
提供了高可用解决方案。Sentinel
实例可以监视任意多个主节点以及这些主节点属下的所有从节点,并在被监视的主节点进入下线状态时自动将下线主节点属下的从节点升级为新的主节点,然后由新的主节点替代已经下线的主节点并继续处理后续的命令请求。
这意味着使用Sentinel
可以部署一套Redis
,在没有人为干预的情况下去应付各种各样的失败事件。Redis Sentinel
同时提供了一些其他的功能,比如监控、通知、为客户端提供配置等。
哨兵架构由两部分组成,哨兵节点和数据节点:
Sentinel
有以下功能:
本文主要为了演示,所以主从和哨兵服务都在同一台机器。推荐还是放在不同的物理机上,否则可能会因为错误的配置而导致服务宕机,从而无法继续提供正常的服务。
首先参照官网教程安装redis
。
创建如下目录:
$ mkdir -p redis-master/conf redis-slave1/conf redis-slave2/conf
下载redis.conf
至redis-master/conf
、redis-slave1/conf
和redis-slave2/conf
目录下
进入redis-maste
目录,执行命令,启动master
节点:
$ redis-server conf/redis.conf
slave
节点配置文件加入如下配置:
# slave1 port改为6380,slave1 port改为6381
port 6380
replicaof 127.0.0.1 6379
# 如果主节点有密码
masterauth password
进入redis-slave1
和redis-slave2
目录,执行命令,启动slave
节点:
$ redis-server conf/redis.conf
创建如下目录:
$ mkdir -p redis-sentinel/conf
下载sentinel.conf
至redis-sentinel/conf
目录下,文件名分别为sentinel-26379.conf
、sentinel-26380.conf
、sentinel-26381.conf
, 修改如下配置:
# pid文件对应26379~26381
pidfile "/var/run/redis/redis-sentinel-26379.pid"
sentinel monitor mymaster 127.0.0.1 6379 2
如果主节点设置了密码,还需加入如下配置:
sentinel auth-pass mymaster <password>
启动Sentinel
服务:
$ redis-sentinel conf/sentinel-26379.conf
$ redis-sentinel conf/sentinel-26380.conf
$ redis-sentinel conf/sentinel-26381.conf
Sentinel
关于主节点的状态检查主节点的监控是否正常:
$ docker exec -it redis-sentinel-26379 /bin/bash
root@ef8018273b93:/data# redis-cli -p 26379
127.0.0.1:26379> SENTINEL master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.118.128"
5) "port"
6) "6379"
...
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
# 出现如下信息,说明当前1主2从3哨兵,所有节点状态正常
master0:name=mymaster,status=ok,address=192.168.118.128:6379,slaves=2,sentinels=3
出现如上信息,说明监控主节点正常。
直接杀死master
服务,检查Sentinel
的日志,可以看到:
26638:X 11 Dec 2024 16:37:15.456 * +sentinel-address-switch master mymaster 172.17.0.1 6379 ip 192.168.118.128 port 26381 for 924c83e89cb4f40c643bfd9029200bb3f9b71406
26638:X 11 Dec 2024 16:37:15.460 * Sentinel new configuration saved on disk
26638:X 11 Dec 2024 16:37:36.070 # +sdown slave 172.22.0.1:6379 172.22.0.1 6379 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.086 # +sdown master mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.139 # +odown master mymaster 172.17.0.1 6379 #quorum 2/2
26638:X 11 Dec 2024 16:37:43.140 # +new-epoch 2
26638:X 11 Dec 2024 16:37:43.140 # +try-failover master mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.142 * Sentinel new configuration saved on disk
26638:X 11 Dec 2024 16:37:43.142 # +vote-for-leader 77d629ca66128d00796c715bf5efb92c7205c856 2
26638:X 11 Dec 2024 16:37:43.146 * 9f09a3df829515f43e56acc183ef8614bf24993b voted for 77d629ca66128d00796c715bf5efb92c7205c856 2
26638:X 11 Dec 2024 16:37:43.147 * 924c83e89cb4f40c643bfd9029200bb3f9b71406 voted for 77d629ca66128d00796c715bf5efb92c7205c856 2
26638:X 11 Dec 2024 16:37:43.198 # +elected-leader master mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.198 # +failover-state-select-slave master mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.259 # +selected-slave slave 192.168.118.128:6380 192.168.118.128 6380 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.259 * +failover-state-send-slaveof-noone slave 192.168.118.128:6380 192.168.118.128 6380 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.343 * +failover-state-wait-promotion slave 192.168.118.128:6380 192.168.118.128 6380 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.353 * Sentinel new configuration saved on disk
26638:X 11 Dec 2024 16:37:43.353 # +promoted-slave slave 192.168.118.128:6380 192.168.118.128 6380 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.353 # +failover-state-reconf-slaves master mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:43.397 * +slave-reconf-sent slave 192.168.118.128:6381 192.168.118.128 6381 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:44.310 # -odown master mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:44.367 * +slave-reconf-inprog slave 192.168.118.128:6381 192.168.118.128 6381 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:44.367 * +slave-reconf-done slave 192.168.118.128:6381 192.168.118.128 6381 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:44.419 * +slave-reconf-sent slave 172.22.0.1:6379 172.22.0.1 6379 @ mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:44.419 # +failover-end master mymaster 172.17.0.1 6379
26638:X 11 Dec 2024 16:37:44.419 # +switch-master mymaster 172.17.0.1 6379 192.168.118.128 6380
26638:X 11 Dec 2024 16:37:44.419 * +slave slave 192.168.118.128:6381 192.168.118.128 6381 @ mymaster 192.168.118.128 6380
26638:X 11 Dec 2024 16:37:44.419 * +slave slave 172.22.0.1:6379 172.22.0.1 6379 @ mymaster 192.168.118.128 6380
26638:X 11 Dec 2024 16:37:44.419 * +slave slave 172.17.0.1:6379 172.17.0.1 6379 @ mymaster 192.168.118.128 6380
26638:X 11 Dec 2024 16:37:44.421 * Sentinel new configuration saved on disk
26638:X 11 Dec 2024 16:38:14.483 # +sdown slave 172.17.0.1:6379 172.17.0.1 6379 @ mymaster 192.168.118.128 6380
26638:X 11 Dec 2024 16:38:14.483 # +sdown slave 172.22.0.1:6379 172.22.0.1 6379 @ mymaster 192.168.118.128 6380
127.0.0.1:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "192.168.118.128"
5) "port"
6) "6380"
7) "runid"
8) "b3a7596afc1cd2825423d0f43e45138f7457de6c"
...