找回密码
 立即注册
首页 业界区 安全 Redis Sentinel高可用实战:主从自动故障转移 ...

Redis Sentinel高可用实战:主从自动故障转移

捡嫌 4 小时前
前言

Redis Sentinel(哨兵)是 Redis 官方的高可用方案,实现主从自动故障转移。本文带你从零搭建一套生产级的 Redis 高可用集群。
一、Sentinel 核心概念

监控:持续检查主从节点是否正常
通知:节点异常时通知管理员或其他程序
自动故障转移:主节点下线时,自动选举新的主节点
配置提供者:客户端通过 Sentinel 获取当前主节点地址
二、环境准备

准备三台服务器(或本地三个端口):
  1. 主节点:192.168.1.100:6379
  2. 从节点:192.168.1.101:6379
  3. 从节点:192.168.1.102:6379
  4. Sentinel:三个实例,分别部署在三台服务器
复制代码
三、主从复制配置

从节点配置(redis.conf):
  1. replicaof 192.168.1.100 6379
  2. masterauth yourpassword
  3. replica-read-only yes
复制代码
启动主从节点后验证:
  1. redis-cli info replication
  2. # 输出:role:master / role:slave
复制代码
四、Sentinel 配置详解

创建 sentinel.conf:
  1. port 26379
  2. sentinel monitor mymaster 192.168.1.100 6379 2
  3. sentinel auth-pass mymaster yourpassword
  4. sentinel down-after-milliseconds mymaster 30000
  5. sentinel parallel-syncs mymaster 1
  6. sentinel failover-timeout mymaster 180000
复制代码
配置说明:
• monitor:监控名为 mymaster 的主节点,2 表示需要 2 个 Sentinel 同意才能判定主节点下线
• down-after-milliseconds:30秒无响应则判定下线
• failover-timeout:故障转移超时时间
启动 Sentinel:
  1. redis-sentinel /etc/redis/sentinel.conf
复制代码
五、故障转移测试

模拟主节点宕机:
  1. redis-cli shutdown
复制代码
观察 Sentinel 日志:
  1. +sdown master mymaster 192.168.1.100 6379
  2. +odown master mymaster 192.168.1.100 6379 #quorum 2/2
  3. +switch-master mymaster 192.168.1.100 6379 192.168.1.101 6379
复制代码
六、Spring Boot 整合 Sentinel

application.yml 配置:
  1. spring:
  2.   redis:
  3.     sentinel:
  4.       master: mymaster
  5.       nodes:
  6.         - 192.168.1.100:26379
  7.         - 192.168.1.101:26379
  8.         - 192.168.1.102:26379
  9.     password: yourpassword
复制代码
七、生产环境最佳实践

• 至少部署 3 个 Sentinel 实例(奇数个)
• Sentinel 与 Redis 节点物理隔离部署
• 开启 AOF 持久化:appendonly yes
• 设置强密码:requirepass 和 masterauth
八、总结

Redis Sentinel 提供了开箱即用的高可用方案,核心要点:
• 至少 3 个 Sentinel 实例保证可用性
• 合理配置 down-after 和 failover-timeout
• 客户端必须通过 Sentinel 获取主节点地址
• 生产环境务必开启持久化和监控告警
掌握 Sentinel,让你的 Redis 集群更加健壮!

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册
发帖

0

粉丝关注

23

主题发布

板块介绍填写区域,请于后台编辑