Docker容器化高可用架构部署方案(十)
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票数设为23. 超时配置sentinel down-after-milliseconds mymaster 50005000毫秒内主节点无响应则认为宕机太短可能误判太长影响故障恢复时间4. 并行同步sentinel parallel-syncs mymaster 1故障转移后同时同步的从节点数量设为1避免同时过多同步造成主节点压力5. 故障转移超时sentinel failover-timeout mymaster 180000180秒内未完成故障转移则超时超时后可重新发起故障转移6. 脚本重配置sentinel deny-scripts-reconfig yes禁止通过SENTINEL SET命令修改监控参数提高安全性7. 认证配置sentinel auth-pass mymaster YourStr0ng!PassRedis密码与主从密码一致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: 3Sentinel-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: 3Sentinel-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分配节点SentinelIPNode1Sentinel-01172.20.3.31Node2Sentinel-02172.20.3.32Node3Sentinel-03172.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集群