09-Sentinel配置详解
本文档详细介绍Redis Sentinel的配置,用于实现Redis集群的自动故障转移。
Sentinel架构
┌─────────────────────────────────────────────────────────────┐ │ Redis Sentinel架构 │ ├─────────────────────────────────────────────────────────────┤ │ │ │ 客户端 │ │ │ │ │ ▼ │ │ ┌───────────┐ ┌───────────┐ ┌───────────┐ │ │ │Sentinel-01│ │Sentinel-02│ │Sentinel-03│ │ │ │172.20.3.31│ │172.20.3.32│ │172.20.3.33│ │ │ └─────┬─────┘ └─────┬─────┘ └─────┬─────┘ │ │ │ │ │ │ │ └──────────────┼──────────────┘ │ │ │ │ │ ▼ │ │ ┌───────────────────────────────────────────────┐ │ │ │ Redis集群 │ │ │ │ ┌───────────┐ ┌────────────┐ ┌────────────┐│ │ │ │ │ Master │◄─┤ Slave │ │ Slave ││ │ │ │ │172.20.3.11│ │ 172.20.3.12│ │ 172.20.3.13││ │ │ │ └───────────┘ └────────────┘ └────────────┘│ │ │ └───────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────┘
Sentinel工作原理
监控:Sentinel持续监控主节点健康状态
通知:发现问题后通知管理员
自动故障转移:选举新主节点,从节点切换
配置更新:通知客户端新主节点地址
完整配置
cat > /opt/cluster-deploy/config/redis/sentinel.conf << 'EOF' bind 0.0.0.0 port 26379 daemonize no supervised no pidfile /var/run/redis-sentinel.pid logfile "" loglevel notice sentinel monitor mymaster 172.20.3.11 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes sentinel auth-pass mymaster 'YourStr0ng!Pass' EOF
配置项详解
1. 基础配置
bind 0.0.0.0 # 监听所有接口 port 26379 # Sentinel端口 daemonize no # 非守护进程(容器需要) supervised no # 不受systemd管理 pidfile /var/run/redis-sentinel.pid # PID文件 logfile "" # 输出到stdout(容器需要) loglevel notice # 日志级别
2. 监控配置
sentinel monitor mymaster 172.20.3.11 6379 2
参数说明:
mymaster:主节点名称(可自定义)172.20.3.11:主节点IP6379:主节点端口2:票数,达到此票数则认为主节点失败
票数规则:
Sentinel总数的一半以上
本项目3个Sentinel,票数设为2
3. 超时配置
sentinel down-after-milliseconds mymaster 5000
5000毫秒内主节点无响应则认为宕机
太短可能误判,太长影响故障恢复时间
4. 并行同步
sentinel parallel-syncs mymaster 1
故障转移后,同时同步的从节点数量
设为1避免同时过多同步造成主节点压力
5. 故障转移超时
sentinel failover-timeout mymaster 180000
180秒内未完成故障转移则超时
超时后可重新发起故障转移
6. 脚本重配置
sentinel deny-scripts-reconfig yes
禁止通过SENTINEL SET命令修改监控参数
提高安全性
7. 认证配置
sentinel auth-pass mymaster 'YourStr0ng!Pass'
Redis密码,与主从密码一致
Docker Compose配置
Sentinel-01 (Node1)
sentinel-01: image: redis:7-alpine container_name: sentinel-01 networks: cache-net: ipv4_address: 172.20.3.31 command: redis-sentinel /etc/redis/sentinel.conf volumes: - ./config/redis/sentinel.conf:/etc/redis/sentinel.conf depends_on: - redis-master restart: unless-stopped healthcheck: test: ["CMD", "redis-cli", "-p", "26379", "ping"] interval: 10s timeout: 5s retries: 3
Sentinel-02 (Node2)
sentinel-02: image: redis:7-alpine container_name: sentinel-02 networks: cache-net: ipv4_address: 172.20.3.32 command: redis-sentinel /etc/redis/sentinel.conf volumes: - ./config/redis/sentinel.conf:/etc/redis/sentinel.conf depends_on: - redis-slave restart: unless-stopped healthcheck: test: ["CMD", "redis-cli", "-p", "26379", "ping"] interval: 10s timeout: 5s retries: 3
Sentinel-03 (Node3)
sentinel-03: image: redis:7-alpine container_name: sentinel-03 networks: cache-net: ipv4_address: 172.20.3.33 command: redis-sentinel /etc/redis/sentinel.conf volumes: - ./config/redis/sentinel.conf:/etc/redis/sentinel.conf depends_on: - redis-slave restart: unless-stopped healthcheck: test: ["CMD", "redis-cli", "-p", "26379", "ping"] interval: 10s timeout: 5s retries: 3
重要排错经验
Sentinel配置文件不能只读挂载
问题:Sentinel运行时需要写入状态信息到配置文件
错误配置:
volumes: - ./config/redis/sentinel.conf:/etc/redis/sentinel.conf:ro # 错误!
正确配置:
volumes: - ./config/redis/sentinel.conf:/etc/redis/sentinel.conf # 正确!
Sentinel会在运行期间修改配置文件,记录:
新主节点信息
从节点信息
投票结果
服务IP分配
| 节点 | Sentinel | IP |
|---|---|---|
| Node1 | Sentinel-01 | 172.20.3.31 |
| Node2 | Sentinel-02 | 172.20.3.32 |
| Node3 | Sentinel-03 | 172.20.3.33 |
自动故障转移流程
1. Sentinel检测到Master不可达 ↓ 2. Sentinel之间进行选举 ↓ 3. 获得多数票的Sentinel成为Leader ↓ 4. Leader选择新Master(优先级最高的Slave) ↓ 5. 将其他Slave指向新Master ↓ 6. 更新Sentinel配置文件 ↓ 7. 通知客户端新Master地址
验证命令
# 查看Sentinel容器 docker ps | grep sentinel # 测试Sentinel连接 docker exec sentinel-01 redis-cli -p 26379 ping docker exec sentinel-02 redis-cli -p 26379 ping docker exec sentinel-03 redis-cli -p 26379 ping # 查看Sentinel主节点信息 docker exec sentinel-01 redis-cli -p 26379 SENTINEL master mymaster # 查看所有Sentinel docker exec sentinel-01 redis-cli -p 26379 SENTINEL sentinels mymaster # 查看从节点 docker exec sentinel-01 redis-cli -p 26379 SENTINEL slaves mymaster # 查看Sentinel认为的当前主节点 docker exec sentinel-01 redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster # 强制故障转移(测试用) docker exec sentinel-01 redis-cli -p 26379 SENTINEL failover mymaster
客户端连接方式
PHP中使用Sentinel
<?php // Sentinel客户端自动发现 $sentinel = new RedisSentinel('mymaster', '172.20.3.31', 26379); $master = $sentinel->getMaster(); // 连接主节点 $redis = new Redis(); $redis->connect($master['ip'], $master['port']); $redis->auth('YourStr0ng!Pass'); ?>redis-cli连接
# 通过Sentinel获取主节点 redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster # 使用获取到的地址连接 redis-cli -h <master-ip> -p 6379 -a YourStr0ng!Pass
常见问题
Q1: Sentinel投票不足无法故障转移
检查Sentinel数量和票数配置
确保至少2个Sentinel正常运行
Q2: 故障转移后客户端无法连接
客户端需要使用Sentinel获取新主节点
不要硬编码主节点地址
Q3: Sentinel日志报错
查看Sentinel日志:
docker logs sentinel-01检查配置文件是否只读
Q4: 原Master恢复后变成Slave
这是正常行为
Sentinel会自动将其降级为新Master的Slave
下一步
10-MySQL配置详解.md - 了解MySQL MGR配置
11-MySQL-MGR初始化.md - 初始化MySQL MGR集群