docker 1分钟 快速搭建 redis 哨兵集群

发布于:2025-08-30 ⋅ 阅读:(22) ⋅ 点赞:(0)

使用 docker-compose 1 分钟搭建好 1主2从3哨兵的 redis 哨兵集群

目录结构

redis-sentinel-cluster
├── check_redis.sh
├── docker-compose.yml
├── redis
│   └── redis.conf
├── sentinel
│   └── sentinel.conf


docker-compose.yml 配置

version: '3.8'

services:
  # --- Redis 主节点 ---
  redis-master:
    image: redis:7.2
    container_name: redis-master
    hostname: redis-master
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - "7000:6379"
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      - redis-master-data:/data
    networks:
      redis-net:
        ipv4_address: 192.168.80.1

  # --- Redis 从节点 1 ---
  redis-slave-1:
    image: redis:7.2
    container_name: redis-slave-1
    hostname: redis-slave-1
    # 启动时,指定它复制 redis-master
    command: redis-server /usr/local/etc/redis/redis.conf --replicaof redis-master 6379
    ports:
      - "7001:6379"
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      - redis-slave-1-data:/data
    networks:
      redis-net:
        ipv4_address: 192.168.80.2
    depends_on:
      - redis-master

  # --- Redis 从节点 2 ---
  redis-slave-2:
    image: redis:7.2
    container_name: redis-slave-2
    hostname: redis-slave-2
    command: redis-server /usr/local/etc/redis/redis.conf --replicaof redis-master 6379
    ports:
      - "7002:6379"
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      - redis-slave-2-data:/data
    networks:
      redis-net:
        ipv4_address: 192.168.80.3
    depends_on:
      - redis-master

  # --- 哨兵节点 1 ---
  redis-sentinel-1:
    image: redis:7.2
    container_name: redis-sentinel-1
    hostname: redis-sentinel-1
    # 使用 sentinel.conf 启动哨兵模式
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    mem_limit: 256m
    ports:
      - "27000:26379"
    volumes:
      - ./sentinel/sentinel.conf:/usr/local/etc/redis/sentinel.conf
    networks:
      redis-net:
        ipv4_address: 192.168.80.4
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2

  # --- 哨兵节点 2 ---
  redis-sentinel-2:
    image: redis:7.2
    container_name: redis-sentinel-2
    hostname: redis-sentinel-2
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    ports:
      - "27001:26379"
    volumes:
      - ./sentinel/sentinel.conf:/usr/local/etc/redis/sentinel.conf
    networks:
      redis-net:
        ipv4_address: 192.168.80.5
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2

  # --- 哨兵节点 3 ---
  redis-sentinel-3:
    image: redis:7.2
    container_name: redis-sentinel-3
    hostname: redis-sentinel-3
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    ports:
      - "27002:26379"
    volumes:
      - ./sentinel/sentinel.conf:/usr/local/etc/redis/sentinel.conf
    networks:
      redis-net:
        ipv4_address: 192.168.80.6
    depends_on:
      - redis-master
      - redis-slave-1
      - redis-slave-2

# 定义网络,让所有容器都在同一个网络下,可以通过服务名互相通信
networks:
  redis-net:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.80.0/20
          gateway: 192.168.80.255

# 定义数据卷,用于持久化 Redis 数据
volumes:
  redis-master-data:
  redis-slave-1-data:
  redis-slave-2-data:

redis/redis.conf 配置

根据实际情况进行密码修改

# 绑定到所有网络接口,以便容器间可以互相访问
bind 0.0.0.0

# 开启保护模式(建议在有密码时关闭,通过防火墙保护)
protected-mode no

# 设置密码,这是生产环境的最佳实践,根据实际安全要求进行密码修改
# 主从和哨兵都需要用这个密码来认证
requirepass "your_redis_password"

# 当本实例作为从节点时,连接主节点所用的密码
masterauth "your_redis_password"

# 开启 AOF 持久化,保证数据安全
appendonly yes

sentinel/sentinel.conf 配置

# 绑定到所有网络接口
bind 0.0.0.0

# 后台启动
#daemonize yes

# 哨兵的工作端口
port 26379

# 监控名为 'mymaster' 的主节点,其地址是 redis-master:6379
# '2' 是 quorum(法定人数),意味着至少需要 2 个哨兵同意,才能判定主节点下线并触发故障转移
# docker-compose 网络会通过服务名 'redis-master' 解析到正确的容器 IP
sentinel monitor mymaster 192.168.80.1 6379 2

# 主节点被判定为主观下线(SDOWN)所需的毫秒数
# 如果一个哨兵在 5000 毫秒内没有收到主节点的有效回复,就认为它主观下线
sentinel down-after-milliseconds mymaster 5000

# 故障转移的超时时间(毫秒)
# 如果一个哨兵在 180000 毫秒内没有完成对该 master 的故障转移,
# 其他哨兵可以接替它继续进行
sentinel failover-timeout mymaster 180000

# 在故障转移后,最多允许多少个从节点同时与新的主节点进行同步
# '1' 是最安全的选择,可以防止新主节点因大量同步请求而过载
sentinel parallel-syncs mymaster 1

# 指定连接 Redis 主从节点所需的密码
sentinel auth-pass mymaster "your_redis_password"
# 哨兵密码,根据实际安全要求进行密码修改
requirepass "your_sentinel_password"

启动并验证

启动
docker-compose up -d

类似如下输出

[+] Running 7/7
 ✔ Network redis-sentinel-cluster_redis-net  Created                                                                                         0.2s 
 ✔ Container redis-master                    Started                                                                                         0.7s 
 ✔ Container redis-slave-2                   Started                                                                                         0.9s 
 ✔ Container redis-slave-1                   Started                                                                                         0.9s 
 ✔ Container redis-sentinel-1                Started                                                                                         1.2s 
 ✔ Container redis-sentinel-2                Started                                                                                         1.2s 
 ✔ Container redis-sentinel-3                Started  
查看 启动状态
docker-compose ps

类似如下输出

NAME               IMAGE       COMMAND                   SERVICE            CREATED         STATUS         PORTS
redis-master       redis:7.2   "docker-entrypoint.s…"   redis-master       5 seconds ago   Up 4 seconds   0.0.0.0:7000->6379/tcp, :::7000->6379/tcp
redis-sentinel-1   redis:7.2   "docker-entrypoint.s…"   redis-sentinel-1   5 seconds ago   Up 3 seconds   6379/tcp, 0.0.0.0:27000->26379/tcp, :::27000->26379/tcp
redis-sentinel-2   redis:7.2   "docker-entrypoint.s…"   redis-sentinel-2   5 seconds ago   Up 3 seconds   6379/tcp, 0.0.0.0:27001->26379/tcp, :::27001->26379/tcp
redis-sentinel-3   redis:7.2   "docker-entrypoint.s…"   redis-sentinel-3   5 seconds ago   Up 3 seconds   6379/tcp, 0.0.0.0:27002->26379/tcp, :::27002->26379/tcp
redis-slave-1      redis:7.2   "docker-entrypoint.s…"   redis-slave-1      5 seconds ago   Up 3 seconds   0.0.0.0:7001->6379/tcp, :::7001->6379/tcp
redis-slave-2      redis:7.2   "docker-entrypoint.s…"   redis-slave-2      5 seconds ago   Up 3 seconds   0.0.0.0:7002->6379/tcp, :::7002->6379/tcp

验证集群状态
./check_redis_sentinel.sh -h 192.168.80.1 -p 6379 -a your_redis_password

类似有如下输出:

成功连接到 Redis (192.168.80.1:6379)

--- 正在检查单机/主从模式 ---
模式判断: 主从复制模式 或 单机模式
----------------------------------------
Redis 版本         : 7.2.10
运行时间         : 0 天
已连接客户端   : 7
使用内存         : 1.29M
当前节点角色   : MASTER
----------------------------------------
节点角色: MASTER
发现 2 个从节点:
  - slave0:192.168.80.2:6379
  - slave1:192.168.80.3:6379

提示: 如果这是一个由 Sentinel 管理的集群, 请使用 -s <sentinel_port> 参数来获取详细的哨兵和主从信息.
./check_redis.sh -h 192.168.80.4 -s 26379 -a your_sentinel_password

类似有如下输出

--- 正在检查哨兵 (Sentinel) 模式 ---
成功连接到 Redis (192.168.80.4:26379)
模式判断: 哨兵 (Sentinel) 模式

监控组: mymaster
----------------------------------------
[Master 节点]
  - 192.168.80.1:6379
[Slave 节点]
  - 192.168.80.3:6379
  - 192.168.80.2:6379
[Sentinel 节点]
  - 192.168.80.6:26379 (284f5f39749c4461097f19df31d6df2b1042f2d9)
  - 192.168.80.5:26379 (522a29a38455cc8d74e11f05626dd01b4b67d3f1)
  - 192.168.80.4:26379 (当前连接的节点)

check_redis_sentinel 脚本地址:https://github.com/zhengmingliang/code-fragment/blob/main/shell/linux/redis/check_redis_sentinel.sh

笔记来源

个人博客 docker 1分钟 快速搭建 redis 哨兵集群 https://alianga.com/articles/docker-redis-sentinel


网站公告

今日签到

点亮在社区的每一天
去签到