概述目标:部署Sentinel监控主从实例并在主故障时自动选举新主,客户端依据通知重连,减少宕机影响。适用:需要高可用的缓存集群场景。核心与实战Sentinel配置(`sentinel.conf`):port 26379 sentinel monitor mymaster 10.0.0.10 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 启动与查看:redis-server /etc/redis/redis.conf --daemonize yes redis-sentinel /etc/redis/sentinel.conf --daemonize yes 主从复制:redis-cli -h 10.0.0.11 -p 6379 replicaof 10.0.0.10 6379 示例故障切换演练:redis-cli -h 10.0.0.10 -p 6379 DEBUG SEGFAULT # 模拟主故障(谨慎) redis-cli -h 10.0.0.12 -p 26379 sentinel get-master-addr-by-name mymaster 客户端连接策略:-- 客户端定期查询Sentinel以获取当前主地址,故障后重连新主 验证与监控Sentinel状态:redis-cli -h 10.0.0.12 -p 26379 sentinel masters redis-cli -h 10.0.0.12 -p 26379 sentinel slaves mymaster 日志与告警:监控failover事件与时长;集成告警通知(邮件/钉钉等)。客户端健康:观察重连成功率与失败原因;设置重试与退避策略。常见误区quorum设置过低导致误切换;需根据Sentinel数量设置合理quorum。主从网络隔离导致脑裂;需跨AZ与网络健康检查。客户端未适配Sentinel导致无法自动更新主地址;需使用支持Sentinel的库。结语通过Redis Sentinel的监控与自动故障切换,缓存系统在主故障时可快速恢复,结合客户端策略可进一步提升可用性。

发表评论 取消回复