背景
现在对3个节点的sentinel进行配置。sentinel的配置文件在redis的安装目录中已经存在,只需要复制到指定的位置即可。
sentinel是独立进程,有对应的脚本来执行。
基于之前的redis 一主二从的架构,我们继续启动3个sentinel进程。
哨兵模式的架构图如下:
启动哨兵们
- sentinel6379.conf
properties
protected-mode no
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel_26379.pid"
logfile "/usr/local/redis-6.2.14/logs/redis-sentinel_26379.log"
dir "/private/tmp"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel announce-hostnames no
# Generated by CONFIG REWRITE
user default on nopass ~* &* +@all
sentinel myid c384816840b671b2f194ebd417458c6bbd08230c
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel current-epoch 0
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6380
sentinel known-sentinel mymaster 127.0.0.1 26381 789f7ad2247aa6104665ced751c1956e87ec175b
sentinel known-sentinel mymaster 127.0.0.1 26380 e43e1bc4459a782d2049f8ab1dd8938e17c90ced
启动sentinel,查看哨兵信息:
- sentinel6380.conf
properties
protected-mode no
port 26380
daemonize yes
pidfile "/var/run/redis-sentinel_26379.pid"
logfile "/usr/local/redis-6.2.14/logs/redis-sentinel_26379.log"
dir "/private/tmp"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel announce-hostnames no
# Generated by CONFIG REWRITE
user default on nopass ~* &* +@all
sentinel myid c384816840b671b2f194ebd417458c6bbd08230c
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel current-epoch 0
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6380
sentinel known-sentinel mymaster 127.0.0.1 26381 789f7ad2247aa6104665ced751c1956e87ec175b
sentinel known-sentinel mymaster 127.0.0.1 26380 e43e1bc4459a782d2049f8ab1dd8938e17c90ced
启动sentinel,查看哨兵信息:
- sentinel6381.conf
properties
protected-mode no
port 26381
daemonize yes
pidfile "/var/run/redis-sentinel_26379.pid"
logfile "/usr/local/redis-6.2.14/logs/redis-sentinel_26379.log"
dir "/private/tmp"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel announce-hostnames no
# Generated by CONFIG REWRITE
user default on nopass ~* &* +@all
sentinel myid c384816840b671b2f194ebd417458c6bbd08230c
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel current-epoch 0
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6380
sentinel known-sentinel mymaster 127.0.0.1 26381 789f7ad2247aa6104665ced751c1956e87ec175b
sentinel known-sentinel mymaster 127.0.0.1 26380 e43e1bc4459a782d2049f8ab1dd8938e17c90ced
启动sentinel,查看哨兵信息:
故障转移测试
redis日志
- 我们kill掉原Master进程,即6379的redis-server,看日志
- 看下6380的redis-server日志
06 Sep 2024 15:49:18.869:开始断开和Master进程的连接,接着持续报错说复制失败,不断地重试
(中间有30s的重试)
06 Sep 2024 15:49:49.205 * MASTER MODE enabled,即6380实例被sentinel选举为Master节点
- 看下6381的redis-server日志
06 Sep 2024 15:49:18.870 # Error condition on socket for SYNC: Connection refused开始断开和Master进程的连接,接着持续报错说复制失败,不断地重试
(中间有30s的重试)
06 Sep 2024 15:49:49.262 * REPLICAOF 127.0.0.1:6380 enabled ,即6380实例被sentinel选举为Master节点后,6381实例也接受现实,开始从6380实例进行数据同步
redis-sentinel日志
让我们进入到之前配置好的文件夹
- redis-sentinel_26379进程日志
- redis-sentinel_26380进程日志
- redis-sentinel_26381进程日志
故障恢复
-
重新启动6379实例
-
通过26379-sentinel,虽然原Master重启了,但目前Master还是6380