Redis Sentinel 高可用方案概述
1、概述
Redis 集群旨在通过一组 redis 节点实现高可用性和数据分片(sharding)。集群架构包含 1 个 master 节点和多个 slave 节点。当 master 节点发生故障时,系统需选举出一个 slave 节点晋升为新的 master。然而,Redis 本身及其多数客户端并未内置自动故障检测与主备切换机制,因此必须依赖外部监控方案来实现自动故障恢复。
Redis Sentinel 作为官方推荐的高可用性解决方案,专门用于 Redis 集群的监控与管理。它能够提供节点状态监控、故障通知、自动故障恢复以及客户端的配置发现服务。
2、遇到的问题
1、docker host网络
在使用 Docker 的 host 网络模式时,该模式在 Windows 和 Mac 操作系统上无法生效,且目前未找到替代解决方案。因此,最终决定放弃在 Windows 上部署,转而使用 CentOS 系统进行集群搭建。
2、不使用host网络的情况下sentinel 连接问题
在非 host 网络模式下,虽然可以通过指定主节点端口使 Sentinel 集群正常联通,但在主节点发生故障时,Sentinel 从主节点获取到的 IP 地址是容器内部的虚拟 IP。这导致其他节点无法正常连接,从而引发集群连接失败的问题。
3、搭建过程
1、目录结构
2、sentinel 配置文件
1、sentinel1.conf
#端口号
port 26379
dir /tmp
# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败
sentinel monitor mymaster 2
# 指的是超过5000秒,且没有回复,则判定主节点不可达
sentinel down-after-milliseconds mymaster 5000
# 表示在故障转移的时候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障转移超时时间
sentinel failover-timeout mymaster 5000
2、sentinel2.conf
#端口号
port 26380
dir /tmp
# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败
sentinel monitor mymaster 2
# 指的是超过5000秒,且没有回复,则判定主节点不可达
sentinel down-after-milliseconds mymaster 5000
# 表示在故障转移的时候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障转移超时时间
sentinel failover-timeout mymaster 5000
3、sentinel3.conf
#端口号
port 26381
dir /tmp
# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败
sentinel monitor mymaster 2
# 指的是超过5000秒,且没有回复,则判定主节点不可达
sentinel down-after-milliseconds mymaster 5000
# 表示在故障转移的时候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障转移超时时间
sentinel failover-timeout mymaster 5000
3、docker-compose.yml
version: '2'
services:
master:
image: redis:4.0
restart: always
container_name: redis-master
#使用主机网络
network_mode: "host"
command: redis-server --port 16379
slave1:
image: redis:4.0
restart: always
container_name: redis-slave-1
network_mode: "host"
# 指定端口并指定master ip 端口
command: redis-server --port 16380 --slaveof 16379
slave2:
image: redis:4.0
restart: always
container_name: redis-slave-2
network_mode: "host"
command: redis-server --port 16381 --slaveof 16379
sentinel1:
image: redis:4.0
restart: always
container_name: redis-sentinel-1
network_mode: "host"
# 指定sentinel文件位置
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
# 使用数据卷映射文件到指定sentinel位置
volumes:
- ./sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis:4.0
restart: always
container_name: redis-sentinel-2
network_mode: "host"
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis:4.0
restart: always
container_name: redis-sentinel-3
network_mode: "host"
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
4、使用centos 部署集群测试效果
1、测试通过sentinel1连接集群

2、测试主节点子节点数据同步


3、关闭master查看主备切换

sentinel 正常联通

主节点从16379 切换 至16381

结尾
端午假期后休息了一周,此前曾尝试搭建 sentinel 集群,但因 Docker 网络模型问题,导致主备节点切换后集群无法连接。昨日确认 host 模式在 Windows 上不可行后,将其迁移至 CentOS 进行测试,最终完美解决该问题。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。