深刻理解Redis集群(下):Redis 哨兵(Sentinel)模式

背景

现在对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

其他文章

Kafka消息堆积问题排查

基于SpringMVC的API灰度方案

理解到位:灾备和只读数据库

SQL治理经验谈:索引覆盖

Mybatis链路分析:JDK动态代理和责任链模式的应用

大模型安装部署、测试、接入SpringCloud应用体系

Mybatis插件-租户ID的注入&拦截应用

相关推荐
速易达网络4 小时前
Bootstrap 5 响应式网站首页模板
前端·bootstrap·html
呼哧呼哧.4 小时前
Spring的核心思想与注解
数据库·sql·spring
迎風吹頭髮5 小时前
Linux内核架构浅谈49-Linux per-CPU页面缓存:热页与冷页的管理与调度优化
linux·缓存·架构
21号 15 小时前
9.Redis 集群(重在理解)
数据库·redis·算法
爬山算法5 小时前
Redis(73)如何处理Redis分布式锁的死锁问题?
数据库·redis·分布式
嘗_5 小时前
sql特训
数据库·sql
jason.zeng@15022075 小时前
centos中安装redis
linux·redis·centos
码农多耕地呗6 小时前
力扣146.LRU缓存(哈希表缓存.映射+双向链表数据结构手搓.维护使用状况顺序)(java)
数据结构·leetcode·缓存
wan5555cn6 小时前
周末之美:慢下来,拥抱生活的温柔
数据库
yumgpkpm6 小时前
华为鲲鹏 Aarch64 环境下多 Oracle 、mysql数据库汇聚到Cloudera CDP7.3操作指南
大数据·数据库·mysql·华为·oracle·kafka·cloudera