Docker搭建Redis哨兵模式
文章目录Docker搭建Redis哨兵模式参考特点工作机制搭建Docker搭建Redis哨兵模式参考哨兵模式的搭建前提需要搭建好主从模式,详细请参考博文《Docker搭建Redis主从模式》特点sentinel模式是建立在主从模式的基础上,如果只有一个Redis节点,sentinel就没有任何意义当master挂了以后,sentinel会在slave中选择一个做为master,并修改它们的配置文件
文章目录
Docker搭建Redis哨兵模式
参考
哨兵模式的搭建前提需要搭建好主从模式,详细请参考博文《Docker搭建Redis主从模式》
特点
- sentinel模式是建立在主从模式的基础上,如果只有一个Redis节点,sentinel就没有任何意义
- 当master挂了以后,sentinel会在slave中选择一个做为master,并修改它们的配置文件,其他slave的配置文件也会被修改,比如slaveof属性会指向新的master
- 当master重新启动后,它将不再是master而是做为slave接收新的master的同步数据
- sentinel因为也是一个进程有挂掉的可能,所以sentinel也会启动多个形成一个sentinel集群
- 多sentinel配置的时候,sentinel之间也会自动监控
- 当主从模式配置密码时,sentinel也会同步将配置信息修改到配置文件中,不需要担心
- 一个sentinel或sentinel集群可以管理多个主从Redis,多个sentinel也可以监控同一个redis
- sentinel最好不要和Redis部署在同一台机器,不然Redis的服务器挂了以后,sentinel也挂了
工作机制
-
每个sentinel以每秒钟一次的频率向它所知的master,slave以及其他sentinel实例发送一个 PING 命令
-
如果一个实例距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被sentinel标记为主观下线。
-
如果一个master被标记为主观下线,则正在监视这个master的所有sentinel要以每秒一次的频率确认master的确进入了主观下线状态
-
当有足够数量的sentinel(大于等于配置文件指定的值)在指定的时间范围内确认master的确进入了主观下线状态, 则master会被标记为客观下线
-
在一般情况下, 每个sentinel会以每 10 秒一次的频率向它已知的所有master,slave发送 INFO 命令
-
当master被sentinel标记为客观下线时,sentinel向下线的master的所有slave发送 INFO 命令的频率会从 10 秒一次改为 1 秒一次
-
若没有足够数量的sentinel同意master已经下线,master的客观下线状态就会被移除; 若master重新向sentinel的 PING 命令返回有效回复,master的主观下线状态就会被移除
-
注意事项:当使用sentinel模式的时候,客户端就不要直接连接Redis,而是连接sentinel的ip和port,由sentinel来提供具体的可提供服务的Redis实现,这样当master节点挂掉以后,sentinel就会感知并将新的master节点提供给使用者。
搭建
- 容器规划
| 容器名称 | 角色 | IP | 容器内部端口 | 宿主机映射端口 |
|---|---|---|---|---|
| redis-sentinel-master | 主节点 | 172.17.0.2 | 6379 | 16379 |
| redis-sentinel-slave-1 | 从节点-1 | 172.17.0.3 | 6379 | 26379 |
| redis-sentinel-slave-2 | 从节点-2 | 172.17.0.4 | 6379 | 36379 |
| redis-sentinel-1 | 哨兵-1 | 172.17.0.5 | 16380 | 16380 |
| redis-sentinel-2 | 哨兵-2 | 172.17.0.6 | 26380 | 26380 |
| redis-sentinel-3 | 哨兵-3 | 172.17.0.7 | 36380 | 36380 |
-
再次检查主从模式下主节点中的配置(redis.conf):
# 在主从模式下,如果主从节点都设置了密码的情况下,需要配置主节点的masterauth,如果不配置,那么在主节点宕机重启之后,原先的主节点# # 会成为子节点,这个时候需要通过masterauth去连接哨兵选举出来的新的主节点 masterauth root -
修改哨兵配置文件(redis-sentinel.conf):
-
哨兵一:
# 默认的端口是26379 port 16380 # 配置哨兵监控的master节点信息 # 默认配置sentinel monitor mymaster 127.0.0.1 6379 2 # mymaster是默认的,也可以自己命名,但是在自己命名之后需要将redis-sentinel.conf文件中所有的mymaster都要替换,否则在启动# # 哨兵时就会报错 # 127.0.0.1是主从模式下主节点的IP # 6379是主从模式下主节点的PORT # 2表示的是至少有两个哨兵认为主节点主观下线之后才会将主节点客观下线之后会重新选举新的主节点,下线后的主节点在重启之后会变成新# # 的子节点 sentinel monitor redis-sentinel-master 172.17.0.2 6379 2 -
哨兵二:
# 默认的端口是26379 port 26380 # 配置哨兵监控的master节点信息 # 默认配置sentinel monitor mymaster 127.0.0.1 6379 2 # mymaster是默认的,也可以自己命名,但是在自己命名之后需要将redis-sentinel.conf文件中所有的mymaster都要替换,否则在启动# # 哨兵时就会报错 # 127.0.0.1是主从模式下主节点的IP # 6379是主从模式下主节点的PORT # 2表示的是至少有两个哨兵认为主节点主观下线之后才会将主节点客观下线之后会重新选举新的主节点,下线后的主节点在重启之后会变成新# # 的子节点 sentinel monitor redis-sentinel-master 172.17.0.2 6379 2 -
哨兵三:
# 默认的端口是26379 port 36380 # 配置哨兵监控的master节点信息 # 默认配置sentinel monitor mymaster 127.0.0.1 6379 2 # mymaster是默认的,也可以自己命名,但是在自己命名之后需要将redis-sentinel.conf文件中所有的mymaster都要替换,否则在启动# # 哨兵时就会报错 # 127.0.0.1是主从模式下主节点的IP # 6379是主从模式下主节点的PORT # 2表示的是至少有两个哨兵认为主节点主观下线之后才会将主节点客观下线之后会重新选举新的主节点,下线后的主节点在重启之后会变成新# # 的子节点 sentinel monitor redis-sentinel-master 172.17.0.2 6379 2
-
-
分别启动三个哨兵(前提是主从模式已经处于正常情况下):
-
哨兵一:
# 启动哨兵一 docker run -p 16380:16380 --name redis-sentinel-1 -v /Users/lyb/Downloads/docker-volume/redis-sentinel/sentinel-1/sentinel-1-16380.conf:/etc/redis/redis-sentinel.conf -d redis redis-sentinel /etc/redis/redis-sentinel.conf # 哨兵一启动日志 1:X 19 Apr 2021 12:18:42.106 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:X 19 Apr 2021 12:18:42.107 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=1, just started 1:X 19 Apr 2021 12:18:42.107 # Configuration loaded 1:X 19 Apr 2021 12:18:42.108 * monotonic clock: POSIX clock_gettime 1:X 19 Apr 2021 12:18:42.109 * Running mode=sentinel, port=16380. 1:X 19 Apr 2021 12:18:42.114 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:42.114 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:42.114 # Sentinel ID is 69d86e70ea7acf591ca85065326141835a029167 1:X 19 Apr 2021 12:18:42.114 # +monitor master redis-sentinel-master 172.17.0.2 6379 quorum 2 1:X 19 Apr 2021 12:18:42.115 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:42.119 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:42.119 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:42.119 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:42.122 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:42.122 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:45.461 * +sentinel sentinel b4aac98c98366353e581e879d01b35d681c12079 172.17.0.6 26380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:45.466 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:45.466 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:46.878 * +sentinel sentinel 0db28591d3674dce6ab2633754be35ae31c47faf 172.17.0.7 36380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:46.882 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:46.882 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy -
哨兵二:
# 启动哨兵二 docker run -p 26380:26380 --name redis-sentinel-2 -v /Users/lyb/Downloads/docker-volume/redis-sentinel/sentinel-2/sentinel-2-26380.conf:/etc/redis/redis-sentinel.conf -d redis redis-sentinel /etc/redis/redis-sentinel.conf # 哨兵二启动日志 1:X 19 Apr 2021 12:18:43.435 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:X 19 Apr 2021 12:18:43.435 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=1, just started 1:X 19 Apr 2021 12:18:43.435 # Configuration loaded 1:X 19 Apr 2021 12:18:43.436 * monotonic clock: POSIX clock_gettime 1:X 19 Apr 2021 12:18:43.437 * Running mode=sentinel, port=26380. 1:X 19 Apr 2021 12:18:43.441 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:43.441 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:43.441 # Sentinel ID is b4aac98c98366353e581e879d01b35d681c12079 1:X 19 Apr 2021 12:18:43.441 # +monitor master redis-sentinel-master 172.17.0.2 6379 quorum 2 1:X 19 Apr 2021 12:18:43.442 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:43.446 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:43.446 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:43.446 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:43.449 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:43.450 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:44.166 * +sentinel sentinel 69d86e70ea7acf591ca85065326141835a029167 172.17.0.5 16380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:44.170 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:44.170 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:46.878 * +sentinel sentinel 0db28591d3674dce6ab2633754be35ae31c47faf 172.17.0.7 36380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:46.882 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:46.882 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy -
哨兵三:
# 启动哨兵三 docker run -p 36380:36380 --name redis-sentinel-3 -v /Users/lyb/Downloads/docker-volume/redis-sentinel/sentinel-3/sentinel-3-36380.conf:/etc/redis/redis-sentinel.conf -d redis redis-sentinel /etc/redis/redis-sentinel.conf # 哨兵三启动日志 1:X 19 Apr 2021 12:18:44.796 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:X 19 Apr 2021 12:18:44.796 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=1, just started 1:X 19 Apr 2021 12:18:44.796 # Configuration loaded 1:X 19 Apr 2021 12:18:44.797 * monotonic clock: POSIX clock_gettime 1:X 19 Apr 2021 12:18:44.798 * Running mode=sentinel, port=36380. 1:X 19 Apr 2021 12:18:44.804 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:44.804 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:44.804 # Sentinel ID is 0db28591d3674dce6ab2633754be35ae31c47faf 1:X 19 Apr 2021 12:18:44.804 # +monitor master redis-sentinel-master 172.17.0.2 6379 quorum 2 1:X 19 Apr 2021 12:18:44.806 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:44.809 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:44.809 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:44.809 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:44.812 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:44.812 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:45.461 * +sentinel sentinel b4aac98c98366353e581e879d01b35d681c12079 172.17.0.6 26380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:45.466 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:45.466 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:18:46.203 * +sentinel sentinel 69d86e70ea7acf591ca85065326141835a029167 172.17.0.5 16380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:18:46.208 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:18:46.208 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
-
-
模拟主节点宕机的情况:
-
在模拟宕机之前我们先检查一下主从模式下的
role情况:-
redis-sentinel-master节点(主节点):lyb@lyb ~ % docker exec -it redis-sentinel-master redis-cli 127.0.0.1:6379> auth root OK 127.0.0.1:6379> role 1) "master" 2) (integer) 306997 3) 1) 1) "172.17.0.3" 2) "6379" 3) "306997" 2) 1) "172.17.0.4" 2) "6379" 3) "306997" 127.0.0.1:6379>可以看到redis-sentinel-master节点下面有两个子节点IP和PORT分别为
172.17.0.3:6379、172.17.0.4:6379。 -
redis-sentinel-slave-1节点(子节点):lyb@lyb ~ % docker exec -it redis-sentinel-slave-1 redis-cli 127.0.0.1:6379> auth root OK 127.0.0.1:6379> role 1) "slave" 2) "172.17.0.2" 3) (integer) 6379 4) "connected" 5) (integer) 344087 127.0.0.1:6379> -
redis-sentinel-slave-2节点(子节点):lyb@lyb ~ % docker exec -it redis-sentinel-slave-2 redis-cli 127.0.0.1:6379> auth root OK 127.0.0.1:6379> role 1) "slave" 2) "172.17.0.2" 3) (integer) 6379 4) "connected" 5) (integer) 356145 127.0.0.1:6379>
-
-
停止
redis-sentinel-master节点:docker stop redis-sentinel-master -
等候一段时间之后分别观察三个哨兵的日志:
-
哨兵一日志:
1:X 19 Apr 2021 12:49:01.680 # +sdown master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.754 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:01.754 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:01.754 # +new-epoch 1 1:X 19 Apr 2021 12:49:01.757 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:01.757 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:01.757 # +vote-for-leader b4aac98c98366353e581e879d01b35d681c12079 1 1:X 19 Apr 2021 12:49:01.764 # +odown master redis-sentinel-master 172.17.0.2 6379 #quorum 2/2 1:X 19 Apr 2021 12:49:01.764 # Next failover delay: I will not start a failover before Mon Apr 19 12:55:02 2021 1:X 19 Apr 2021 12:49:02.730 # +config-update-from sentinel 0db28591d3674dce6ab2633754be35ae31c47faf 172.17.0.7 36380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:02.730 # +switch-master redis-sentinel-master 172.17.0.2 6379 172.17.0.4 6379 1:X 19 Apr 2021 12:49:02.730 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.4 6379 1:X 19 Apr 2021 12:49:02.730 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ redis-sentinel-master 172.17.0.4 6379 1:X 19 Apr 2021 12:49:02.733 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:02.733 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:32.753 # +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ redis-sentinel-master 172.17.0.4 6379 -
哨兵二日志:
1:X 19 Apr 2021 12:49:01.691 # +sdown master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.746 # +odown master redis-sentinel-master 172.17.0.2 6379 #quorum 3/2 1:X 19 Apr 2021 12:49:01.746 # +new-epoch 1 1:X 19 Apr 2021 12:49:01.746 # +try-failover master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.751 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:01.751 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:01.751 # +vote-for-leader b4aac98c98366353e581e879d01b35d681c12079 1 1:X 19 Apr 2021 12:49:01.758 # 69d86e70ea7acf591ca85065326141835a029167 voted for b4aac98c98366353e581e879d01b35d681c12079 1 1:X 19 Apr 2021 12:49:01.758 # 0db28591d3674dce6ab2633754be35ae31c47faf voted for b4aac98c98366353e581e879d01b35d681c12079 1 1:X 19 Apr 2021 12:49:01.817 # +elected-leader master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.817 # +failover-state-select-slave master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.869 # +selected-slave slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.869 * +failover-state-send-slaveof-noone slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.941 * +failover-state-wait-promotion slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:02.672 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:02.672 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:02.672 # +promoted-slave slave 172.17.0.4:6379 172.17.0.4 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:02.672 # +failover-state-reconf-slaves master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:02.717 * +slave-reconf-sent slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:02.903 # -odown master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:03.692 * +slave-reconf-inprog slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:03.692 * +slave-reconf-done slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:03.749 # +failover-end master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:03.749 # +switch-master redis-sentinel-master 172.17.0.2 6379 172.17.0.4 6379 1:X 19 Apr 2021 12:49:03.749 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.4 6379 1:X 19 Apr 2021 12:49:03.749 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ redis-sentinel-master 172.17.0.4 6379 1:X 19 Apr 2021 12:49:03.755 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:03.755 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:33.761 # +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ redis-sentinel-master 172.17.0.4 6379 -
哨兵三日志:
1:X 19 Apr 2021 12:49:01.637 # +sdown master redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:01.754 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:01.754 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:01.754 # +new-epoch 1 1:X 19 Apr 2021 12:49:01.757 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:01.757 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:01.757 # +vote-for-leader b4aac98c98366353e581e879d01b35d681c12079 1 1:X 19 Apr 2021 12:49:02.717 # +config-update-from sentinel b4aac98c98366353e581e879d01b35d681c12079 172.17.0.6 26380 @ redis-sentinel-master 172.17.0.2 6379 1:X 19 Apr 2021 12:49:02.717 # +switch-master redis-sentinel-master 172.17.0.2 6379 172.17.0.4 6379 1:X 19 Apr 2021 12:49:02.718 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ redis-sentinel-master 172.17.0.4 6379 1:X 19 Apr 2021 12:49:02.718 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ redis-sentinel-master 172.17.0.4 6379 1:X 19 Apr 2021 12:49:02.722 # Could not rename tmp config file (Device or resource busy) 1:X 19 Apr 2021 12:49:02.723 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy 1:X 19 Apr 2021 12:49:32.752 # +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ redis-sentinel-master 172.17.0.4 6379
-
-
从以上三个哨兵的日志中,我们大概能够看出,投票是由哨兵二发起且有两个哨兵都同意
redis-sentinel-master客观下线,并且进行了故障转移failover,投票选举出新的主节点为172.17.0.4 6379也就是redis-sentinel-slave-2节点,我们验证一下,此时的redis-sentinel-master节点处于宕机状态,我们只需要检查redis-sentinel-slave-1节点和redis-sentinel-slave-2节点的role情况即可:-
redis-sentinel-slave-1节点:127.0.0.1:6379> auth root OK 127.0.0.1:6379> role 1) "slave" 2) "172.17.0.4" 3) (integer) 6379 4) "connected" 5) (integer) 562746 127.0.0.1:6379>此时已经看到
redis-sentinel-slave-1节点的父节点已经发生了变化。 -
redis-sentinel-slave-2节点:127.0.0.1:6379> auth root OK 127.0.0.1:6379> role 1) "master" 2) (integer) 580470 3) 1) 1) "172.17.0.3" 2) "6379" 3) "580026" 127.0.0.1:6379>此时已经看到
redis-sentinel-slave-2节点由原先的slave节点变成了master节点,说明我们的哨兵已经在工作了。
-
-
最后,让我们重启
redis-sentinel-master节点并检查redis-sentinel-master节点的role:lyb@lyb ~ % docker start redis-sentinel-master redis-sentinel-master lyb@lyb ~ % docker exec -it redis-sentinel-master redis-cli 127.0.0.1:6379> auth root OK 127.0.0.1:6379> role 1) "slave" 2) "172.17.0.4" 3) (integer) 6379 4) "connected" 5) (integer) 641269 127.0.0.1:6379>此时已经可以看到
redis-sentinel-master节点由原先的master节点变成了slave节点。
-
以上就是Docker搭建Redis哨兵模式的全部内容,有不对的地方请指正!
更多推荐



所有评论(0)