Bootstrap

三台 Centos7.9 中 Docker 部署 Redis 哨兵模式

1. 环境规划

三台服务器的角色分配如下:

IP Address容器端口角色
192.168.15.128redis-master6379主节点
192.168.15.129redis-slave16379从节点
192.168.15.130redis-slave26379从节点
192.168.15.128sentinel126379哨兵节点
192.168.15.129sentinel226379哨兵节点
192.168.15.130sentinel326379哨兵节点

Redis 主节点和哨兵将在同一台服务器上运行。

2. 配置 Docker Compose

在每台服务器上创建自己的 docker-compose.yml 文件。

192.168.15.128 - docker-compose.yml

version: '3.9'
services:
  redis-master:
    image: redis:6.0
    container_name: redis-master
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - ./data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
    networks:
      - redis-network

  sentinel1:
    image: redis:6.0
    container_name: redis-sentinel1
    restart: always
    ports:
      - "26379:26379"
    networks:
      - redis-network
    volumes:
      - ./sentinel1.conf:/usr/local/etc/sentinel.conf
    command: ["redis-sentinel", "/usr/local/etc/sentinel.conf"]

networks:
  redis-network:
    driver: bridge

192.168.15.129 - docker-compose.yml

version: '3.9'
services:
  redis-slave1:
    image: redis:6.0
    container_name: redis-slave1
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - ./data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
    networks:
      - redis-network

  sentinel2:
    image: redis:6.0
    container_name: redis-sentinel2
    restart: always
    ports:
      - "26379:26379"
    networks:
      - redis-network
    volumes:
      - ./sentinel2.conf:/usr/local/etc/sentinel.conf
    command: ["redis-sentinel", "/usr/local/etc/sentinel.conf"]

networks:
  redis-network:
    driver: bridge

192.168.15.130 - docker-compose.yml

version: '3.9'
services:
  redis-slave2:
    image: redis:6.0
    container_name: redis-slave2
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - ./data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
    networks:
      - redis-network

  sentinel3:
    image: redis:6.0
    container_name: redis-sentinel3
    restart: always
    ports:
      - "26379:26379"
    networks:
      - redis-network
    volumes:
      - ./sentinel3.conf:/usr/local/etc/sentinel.conf
    command: ["redis-sentinel", "/usr/local/etc/sentinel.conf"]

networks:
  redis-network:
    driver: bridge


3. 配置 Redis 密码和持久化

在每个服务器上创建 redis.conf 文件,内容如下。

redis.conf (适用于主节点)

bind 0.0.0.0
protected-mode yes
port 6379
requirepass your_master_password
masterauth your_master_password
appendonly yes
dir /data

redis.conf (适用于从节点)

bind 0.0.0.0
protected-mode yes
port 6379
requirepass your_slave_password
masterauth your_master_password
replicaof 192.168.15.128 6379
appendonly yes
dir /data

requirepass:设置访问实例的密码。
masterauth:从节点用此密码认证主节点。
appendonly:启用数据持久化。

4. 配置哨兵

更新各台服务器的 sentinelX.conf 文件,确保哨兵与 Redis 实例的密码匹配:

sentinelX.conf

port 26379
sentinel monitor mymaster 192.168.15.128 6379 2
sentinel auth-pass mymaster your_master_password
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1

sentinel auth-pass mymaster your_master_password:为哨兵提供与 Redis 主节点通信时使用的密码。

  • 192.168.15.128: Redis 主节点的 IP 地址。
  • 2: 最少票数要求,3 个哨兵中需要至少 2 个同意切换主节点。

5. 启动服务

在每台服务器的对应目录运行:

docker-compose up -d

验证所有容器是否正常运行:

docker ps

6. 验证 Redis 哨兵模式

检查哨兵状态: 登录任意一个哨兵容器:

docker exec -it redis-sentinel1 redis-cli -p 26379

执行命令:

SENTINEL masters

SENTINEL slaves mymaster

模拟故障转移: 在主节点服务器停止主节点容器:


docker stop redis-master

查看日志确认哨兵是否选出新的主节点。

重新加入原主节点: 启动原主节点并手动设置为从节点:

docker start redis-master

docker exec -it redis-master redis-cli

slaveof NEW_MASTER_IP 6379

7. 注意事项

  • 端口防火墙:确保 6379 和 26379 端口在三台服务器之间互通。
  • 哨兵数量:建议使用奇数个哨兵以避免投票僵局。

完成以上步骤后,在 192.168.15.128 - 192.168.15.130 即可完成部署 Redis 哨兵模式。

;