好的!下面给你详细讲解 Redis 哨兵模式的概念、作用,以及如何配置 Redis 哨兵(Sentinel)实现高可用。


一、Redis 哨兵模式简介

Redis 哨兵(Sentinel)是一种高可用解决方案,主要功能包括:

  • 监控:持续检测主节点和从节点的状态。
  • 通知:当被监控的 Redis 节点出现问题时,通知系统管理员或应用。
  • 自动故障转移:当主节点挂掉时,自动将某个从节点升级为新的主节点,并通知客户端更新连接信息。
  • 配置提供者:客户端通过哨兵获取当前主节点的地址,实现无缝切换。

二、哨兵模式架构示意

       +------------+      +-------------+
       | Redis主节点 |<---->| Redis从节点1|
       +------------+      +-------------+
             |                  |
             |                  |
        +----------+       +-----------+
        | 哨兵节点1 |       | 哨兵节点2 |
        +----------+       +-----------+
              \               /
               \             /
               +----------+
               | 哨兵节点3 |
               +----------+
  • 建议至少部署3个哨兵节点,保证哨兵集群的仲裁(多数派)机制正常工作。
  • Redis 主节点写,从节点读;哨兵监控主从状态,故障转移时保证系统可用。

三、哨兵配置步骤示例

假设我们有一主两从 Redis 实例:

  • 主节点 IP:192.168.1.100,端口:6379
  • 从节点1 IP:192.168.1.101,端口:6379
  • 从节点2 IP:192.168.1.102,端口:6379

1. Redis 主从配置(简要)

主节点(redis.conf)

bind 0.0.0.0
port 6379
protected-mode no

从节点(redis.conf)

bind 0.0.0.0
port 6379
replicaof 192.168.1.100 6379   # 旧版本写作 slaveof
protected-mode no

启动主从实例。


2. 哨兵配置文件示例(sentinel.conf)

以下是哨兵节点配置示例(例如哨兵节点1):

port 26379                       # 哨兵端口,默认26379
dir /tmp                        # 工作目录,用于持久化哨兵状态文件

# 监控主节点配置
sentinel monitor mymaster 192.168.1.100 6379 2
# 参数说明:
# mymaster - 主节点的名字,客户端和哨兵用此名字识别主节点
# 192.168.1.100 - 主节点 IP
# 6379 - 主节点端口
# 2 - 哨兵判断主节点挂掉所需的最小投票数(至少2个哨兵同意才判定主挂)

sentinel down-after-milliseconds mymaster 5000
# 如果主节点 5秒内没响应,则判定主节点主观下线(S_DOWN)

sentinel failover-timeout mymaster 60000
# 故障转移超时时间,1分钟内完成切换

sentinel parallel-syncs mymaster 1
# 故障转移时,同时有多少个从节点能并行进行同步

# 其他可选配置,如认证、日志等

启动哨兵:

redis-sentinel /path/to/sentinel.conf

为集群配置至少3个哨兵实例,分别指向相同的主节点信息。


3. 客户端连接

客户端连接时,可以连接哨兵,询问当前主节点地址:

  • Redis 官方客户端和很多第三方客户端都支持哨兵模式,自动发现主节点 IP 和端口。

四、常用命令(连接哨兵后执行)

  • 查看当前哨兵监控的主节点:
SENTINEL masters
  • 查看某个主节点的所有从节点:
SENTINEL slaves mymaster
  • 查看主节点当前状态:
SENTINEL get-master-addr-by-name mymaster

五、注意事项

  • 哨兵节点数最好为奇数,防止投票平票。
  • sentinel monitor 中的主节点名字 mymaster 要保持一致。
  • 哨兵配置文件默认会在 dir 目录下生成 *.conf 和状态文件,不能删。
  • 哨兵模式适合中小规模高可用,生产大规模可考虑 Redis Cluster。
  • Redis 6+版本支持 ACL,哨兵也支持认证配置,注意安全。

六、总结

步骤说明
部署主从配置 Redis 主从复制
配置哨兵编写哨兵配置文件,监控主节点
启动哨兵启动多个哨兵节点,组成哨兵集群
客户端使用哨兵连接哨兵获取主节点信息,实现故障切换

好的!下面给你一个 Redis 哨兵模式的完整部署脚本示例,包含主节点、两个从节点和三个哨兵节点的启动配置,方便你一键部署测试。


Redis 哨兵模式完整部署示例(脚本版)

假设服务器 IP 统一是 127.0.0.1(本地测试环境),端口分配如下:

角色端口
主节点6379
从节点16380
从节点26381
哨兵126379
哨兵226380
哨兵326381

1. 准备 Redis 配置文件目录结构

mkdir -p ~/redis-sentinel-demo/{master,slave1,slave2,sentinel1,sentinel2,sentinel3}

2. 主节点配置 master/redis.conf

port 6379
bind 127.0.0.1
daemonize yes
pidfile /tmp/redis-master.pid
logfile "/tmp/redis-master.log"
dir /tmp/redis-master-data
protected-mode no

3. 从节点配置(slave1/redis.conf 和 slave2/redis.conf

两个从节点配置基本相同,端口不同。

slave1/redis.conf

port 6380
bind 127.0.0.1
daemonize yes
pidfile /tmp/redis-slave1.pid
logfile "/tmp/redis-slave1.log"
dir /tmp/redis-slave1-data
replicaof 127.0.0.1 6379
protected-mode no

slave2/redis.conf

port 6381
bind 127.0.0.1
daemonize yes
pidfile /tmp/redis-slave2.pid
logfile "/tmp/redis-slave2.log"
dir /tmp/redis-slave2-data
replicaof 127.0.0.1 6379
protected-mode no

4. 哨兵配置(3个哨兵)示例,修改端口和日志文件即可

s1/sentinel.conf

port 26379
daemonize yes
pidfile /tmp/redis-sentinel1.pid
logfile "/tmp/redis-sentinel1.log"
dir /tmp/redis-sentinel1-data

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1

s2/sentinel.conf

port 26380
daemonize yes
pidfile /tmp/redis-sentinel2.pid
logfile "/tmp/redis-sentinel2.log"
dir /tmp/redis-sentinel2-data

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1

s3/sentinel.conf

port 26381
daemonize yes
pidfile /tmp/redis-sentinel3.pid
logfile "/tmp/redis-sentinel3.log"
dir /tmp/redis-sentinel3-data

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1

5. 启动脚本 start-redis-sentinel.sh

#!/bin/bash

# 启动主节点
redis-server ~/redis-sentinel-demo/master/redis.conf

# 启动从节点
redis-server ~/redis-sentinel-demo/slave1/redis.conf
redis-server ~/redis-sentinel-demo/slave2/redis.conf

# 启动哨兵节点
redis-sentinel ~/redis-sentinel-demo/sentinel1/sentinel.conf
redis-sentinel ~/redis-sentinel-demo/sentinel2/sentinel.conf
redis-sentinel ~/redis-sentinel-demo/sentinel3/sentinel.conf

echo "Redis 主从和哨兵节点已启动"

给脚本执行权限:

chmod +x start-redis-sentinel.sh

执行启动:

./start-redis-sentinel.sh

6. 验证哨兵状态

连接任意哨兵端口:

redis-cli -p 26379

执行:

SENTINEL masters
SENTINEL slaves mymaster
SENTINEL get-master-addr-by-name mymaster

7. 测试主节点故障转移

关闭主节点:

redis-cli -p 6379 shutdown

等待 10-20 秒,哨兵会自动选举新主节点。

再次执行:

redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster

确认新主节点变更。


七、说明

  • 这里示例是本地环境部署,实际生产环境请修改 bind 为对应 IP,注意防火墙和安全。
  • 哨兵节点数量建议为奇数,避免选举平票。
  • 哨兵会自动同步主从关系,确保主节点故障时自动提升从节点。