Redis Sentinel 是 Redis 提供的高可用解决方案,用于监控 Redis 主从架构中的节点状态,实现自动故障检测、故障转移及配置管理。本镜像基于 Redis 镜像构建,封装了 Sentinel 组件,可快速部署至容器环境,为 Redis 集群提供高可用保障。
bashdocker run -d \ --name redis-sentinel \ -p 26379:26379 \ -v /path/to/sentinel.conf:/etc/redis/sentinel.conf \ redis:alpine \ redis-sentinel /etc/redis/sentinel.conf
说明:需提前准备
sentinel.conf配置文件(见 4.4 配置参数),并通过-v挂载至容器内。
bash# 节点 1 docker run -d \ --name redis-sentinel-1 \ -p 26379:26379 \ -v /path/to/sentinel-1.conf:/etc/redis/sentinel.conf \ --network redis-net \ redis:alpine redis-sentinel /etc/redis/sentinel.conf # 节点 2(端口映射 26380,避免宿主机端口冲突) docker run -d \ --name redis-sentinel-2 \ -p 26380:26379 \ -v /path/to/sentinel-2.conf:/etc/redis/sentinel.conf \ --network redis-net \ redis:alpine redis-sentinel /etc/redis/sentinel.conf # 节点 3(端口映射 26381) docker run -d \ --name redis-sentinel-3 \ -p 26381:26379 \ -v /path/to/sentinel-3.conf:/etc/redis/sentinel.conf \ --network redis-net \ redis:alpine redis-sentinel /etc/redis/sentinel.conf
说明:需创建自定义网络
redis-net(docker network create redis-net),确保 Sentinel 与 Redis 节点在同一网络。
以下示例包含 1 主、2 从、3 Sentinel 节点的完整架构:
yamlversion: '3' networks: redis-net: driver: bridge services: # Redis 主节点 redis-master: image: redis:alpine container_name: redis-master ports: - "6379:6379" networks: - redis-net command: redis-server --appendonly yes # 开启 AOF 持久化 # Redis 从节点 1 redis-slave-1: image: redis:alpine container_name: redis-slave-1 ports: - "6380:6379" networks: - redis-net command: redis-server --slaveof redis-master 6379 # 指向主节点 # Redis 从节点 2 redis-slave-2: image: redis:alpine container_name: redis-slave-2 ports: - "6381:6379" networks: - redis-net command: redis-server --slaveof redis-master 6379 # 指向主节点 # Sentinel 节点 1 sentinel-1: image: redis:alpine container_name: sentinel-1 ports: - "26379:26379" networks: - redis-net volumes: - ./sentinel-1.conf:/etc/redis/sentinel.conf command: redis-sentinel /etc/redis/sentinel.conf # Sentinel 节点 2 sentinel-2: image: redis:alpine container_name: sentinel-2 ports: - "26380:26379" networks: - redis-net volumes: - ./sentinel-2.conf:/etc/redis/sentinel.conf command: redis-sentinel /etc/redis/sentinel.conf # Sentinel 节点 3 sentinel-3: image: redis:alpine container_name: sentinel-3 ports: - "26381:26379" networks: - redis-net volumes: - ./sentinel-3.conf:/etc/redis/sentinel.conf command: redis-sentinel /etc/redis/sentinel.conf
注:所有 Sentinel 配置文件(sentinel-1.conf、sentinel-2.conf、sentinel-3.conf)需包含相同的监控配置(见 4.4 配置参数)。
Sentinel 配置通过 sentinel.conf 文件定义,核心参数如下:
| 参数名 | 格式 | 说明 | 默认值 |
|---|---|---|---|
sentinel monitor <master-name> <ip> <port> <quorum> | 必选 | 监控的主节点名称、IP、端口及“仲裁数”(判断主节点不可用的最小 Sentinel 数量) | - |
sentinel down-after-milliseconds <master-name> <ms> | 可选 | 主节点无响应的超时时间(毫秒),超时则标记为“主观下线” | 30000(30秒) |
sentinel parallel-syncs <master-name> <num> | 可选 | 故障转移后,同时向新主节点同步数据的从节点数量(越小越慢,冲突越少) | 1 |
sentinel failover-timeout <master-name> <ms> | 可选 | 故障转移超时时间(毫秒),超时则重试 | ***(3分钟) |
sentinel notification-script <master-name> <path> | 可选 | 节点状态变化时触发的通知脚本路径(如告警脚本) | - |
sentinel client-reconfig-script <master-name> <path> | 可选 | 故障转移后触发的客户端重配置脚本路径 | - |
示例配置文件(sentinel.conf):
conf# 监控名为 "mymaster" 的主节点(IP:redis-master,端口:6379,仲裁数:2) sentinel monitor mymaster redis-master 6379 2 # 主节点无响应 10 秒后标记为下线 sentinel down-after-milliseconds mymaster *** # 故障转移时允许 1 个从节点同时同步 sentinel parallel-syncs mymaster 1 # 故障转移超时时间 2 分钟 sentinel failover-timeout mymaster ***
为简化配置,可通过环境变量动态注入 Sentinel 参数(无需手动编写 sentinel.conf),常用变量如下:
| 环境变量 | 对应配置参数 | 说明 |
|---|---|---|
SENTINEL_MONITOR_NAME | sentinel monitor <name> | 主节点名称(如 mymaster) |
REDIS_MASTER_HOST | sentinel monitor <ip> | 主节点 IP/主机名 |
REDIS_MASTER_PORT | sentinel monitor <port> | 主节点端口(默认 6379) |
SENTINEL_QUORUM | sentinel monitor <quorum> | 仲裁数(默认 2) |
SENTINEL_DOWN_AFTER | sentinel down-after-milliseconds | 超时时间(毫秒,默认 30000) |
SENTINEL_PARALLEL_SYNCS | sentinel parallel-syncs | 同步从节点数量(默认 1) |
SENTINEL_FAILOVER_TIMEOUT | sentinel failover-timeout | 故障转移超时(毫秒,默认 ***) |
使用环境变量的 Docker Run 示例:
bashdocker run -d \ --name redis-sentinel \ -p 26379:26379 \ --network redis-net \ -e SENTINEL_MONITOR_NAME=mymaster \ -e REDIS_MASTER_HOST=redis-master \ -e REDIS_MASTER_PORT=6379 \ -e SENTINEL_QUORUM=2 \ -e SENTINEL_DOWN_AFTER=*** \ redis:alpine \ redis-sentinel --sentinel
客户端需通过 Sentinel 获取当前主节点地址,而非直接连接主节点。以 Redis CLI 为例:
bash# 连接 Sentinel(端口 26379) redis-cli -h 127.0.0.1 -p 26379 # 获取主节点信息 sentinel get-master-addr-by-name mymaster # 输出示例:1) "redis-master" 2) "6379"(故障转移后将返回新主节点地址)
主流 Redis 客户端(如 Jedis、Lettuce)均内置 Sentinel 支持,配置示例(Java Jedis):
javaSet<String> sentinelSet = new HashSet<>(); sentinelSet.add("127.0.0.1:26379"); sentinelSet.add("127.0.0.1:26380"); sentinelSet.add("127.0.0.1:26381"); JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinelSet); Jedis jedis = pool.getResource();
来自真实用户的反馈,见证轩辕镜像的优质服务
免费版仅支持 Docker Hub 加速,不承诺可用性和速度;专业版支持更多镜像源,保证可用性和稳定速度,提供优先客服响应。
免费版仅支持 docker.io;专业版支持 docker.io、gcr.io、ghcr.io、registry.k8s.io、nvcr.io、quay.io、mcr.microsoft.com、docker.elastic.co 等。
当返回 402 Payment Required 错误时,表示流量已耗尽,需要充值流量包以恢复服务。
通常由 Docker 版本过低导致,需要升级到 20.x 或更高版本以支持 V2 协议。
先检查 Docker 版本,版本过低则升级;版本正常则验证镜像信息是否正确。
使用 docker tag 命令为镜像打上新标签,去掉域名前缀,使镜像名称更简洁。
探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
在 Linux 系统配置镜像加速服务
在 Docker Desktop 配置镜像加速
Docker Compose 项目配置加速
Kubernetes 集群配置 Containerd
在宝塔面板一键配置镜像加速
Synology 群晖 NAS 配置加速
飞牛 fnOS 系统配置镜像加速
极空间 NAS 系统配置加速服务
爱快 iKuai 路由系统配置加速
绿联 NAS 系统配置镜像加速
QNAP 威联通 NAS 配置加速
Podman 容器引擎配置加速
HPC 科学计算容器配置加速
ghcr、Quay、nvcr 等镜像仓库
无需登录使用专属域名加速
需要其他帮助?请查看我们的 常见问题 或 官方QQ群: 13763429