跳转到主要内容

三节点哨兵集群部署

docker-compose文件准备

version: '3.1'

services:
  redis01:
    image: swr.cn-southwest-2.myhuaweicloud.com/vp-guiyang/redis:5.0.0
    command: sh -c "redis-server /usr/local/redis/redis.conf; redis-sentinel /usr/local/redis/sentinel.conf"
    ports:
      - 36379:6379
      - 46379:26379
    volumes:
    - ./redis01/data:/data
    - ./redis01/redis.conf:/usr/local/redis/redis.conf
    - ./redis01/sentinel.conf:/usr/local/redis/sentinel.conf
    restart: always

  redis02:
    image: swr.cn-southwest-2.myhuaweicloud.com/vp-guiyang/redis:5.0.0
    command: sh -c "redis-server /usr/local/redis/redis.conf; redis-sentinel /usr/local/redis/sentinel.conf"
    ports:
      - 36380:6379
      - 46380:26379
    volumes:
    - ./redis02/data:/data
    - ./redis02/redis.conf:/usr/local/redis/redis.conf
    - ./redis02/sentinel.conf:/usr/local/redis/sentinel.conf
    restart: always
    

  redis03:
    image: swr.cn-southwest-2.myhuaweicloud.com/vp-guiyang/redis:5.0.0
    command: sh -c "redis-server /usr/local/redis/redis.conf; redis-sentinel /usr/local/redis/sentinel.conf"
    ports:
      - 36381:6379
      - 46381:26379
    volumes:
    - ./redis03/data:/data
    - ./redis03/redis.conf:/usr/local/redis/redis.conf
    - ./redis03/sentinel.conf:/usr/local/redis/sentinel.conf
    restart: always

redis配置文件需要修改的地方

redis主节点

  1. 将bind 127.0.0.1注释掉
  2. protected-mode yes改为 protected no
  3. logfile "" 改为 logfile "/var/log/redis.log"(方便日后排查redis故障)
  4. databases 16 (可以根据需求该需要的数据库数量)
  5. 如果使用RDB做持久化,可以修改下面的配置 save 900 1 save 300 10 save 60 10000
  6. dbfilename dump.rdb指定rdb在保存的文件名(可以根据需要修改)
  7. dir /data 指定dump.rdb存放的目录,需要与挂载目录保持一致。保证数据能正常持久化到硬盘中
  8. requirepass 配置redis的密码

redis从节点

  1. 在主节点的基础上加入replica-serve-stale-data yes

哨兵的配置修改

  1. logfile "" 改为 logfile "/var/log/redis-sentinel.log"(方便日后排查故障)
  2. sentinel announce-ip 172.18.41.8 (用来告诉其他哨兵我的地址是多少)
  3. sentinel announce-port 46379 (用来告诉其他哨兵我的端口是多少)
  4. sentinel monitor mymaster 172.18.41.8 36379 2(配置监控的redis 名字、master的地址、端口、quorum) quorm计算公式:quorum = (n / 2) + 1 (n表示主机数量)
  5. sentinel auth-pass mymaster 密码 配置主监控的认证密码