Docker Compose 安装Redis哨兵集群

redis主从配置

进入/opt/docker-compose/redis,编辑docker-compose.yml,配置如下:

version: '3.1'

services:
  redis-master:
    container_name: redis-master
    image: redis
    restart: always
    hostname: redis-master
    ports:
      - 6380:6379
    volumes:
      - "/opt/redis/redis-master/data:/data"
    environment:
      REDIS_PASSWORD: 您的密码
      TZ: Asia/Shanghai
    command: ["sh", "-c", "redis-server --requirepass $${REDIS_PASSWORD}  --masterauth $${REDIS_PASSWORD}"]

  redis-slave:
    container_name: redis-slave
    image: redis
    restart: always
    hostname: redis-slave
    ports:
      - 6381:6379
    volumes:
      - "/opt/redis/redis-slave/data:/data"
    environment:
      REDIS_PASSWORD: 您的密码
      TZ: Asia/Shanghai
    command: ["sh", "-c", "redis-server --slaveof redis-master 6379 --requirepass $${REDIS_PASSWORD}  --masterauth $${REDIS_PASSWORD}"]

执行docker-compose up -d,启动主从服务。

redis哨兵配置

进入/opt/docker-compose/redis-sentinel,编辑docker-compose.yml,配置如下:

version: '3.1'

services:
  redis-sentinel-1:
    container_name: redis-sentinel-1
    image: redis
    restart: always
    hostname: redis-sentinel-1
    ports:
      - 26380:26379
    volumes:
      - "/opt/redis-sentinel/redis-sentinel-1/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf"
    environment:
      TZ: Asia/Shanghai
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf

  redis-sentinel-2:
    container_name: redis-sentinel-2
    image: redis
    restart: always
    hostname: redis-sentinel-2
    ports:
      - 26381:26379
    volumes:
      - "/opt/redis-sentinel/redis-sentinel-2/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf"
    environment:
      TZ: Asia/Shanghai
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf

  redis-sentinel-3:
    container_name: redis-sentinel-3
    image: redis
    restart: always
    hostname: redis-sentinel-3
    ports:
      - 26382:26379
    volumes:
      - "/opt/redis-sentinel/redis-sentinel-3/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf"
    environment:
      TZ: Asia/Shanghai
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf

networks:
  default:
    external:
      name: redis_default

编辑sentinel.conf,配置如下:

port 26379
dir /tmp
sentinel monitor mymaster redis-master 6379 2
sentinel auth-pass mymaster 您的密码
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

将sentinel.conf,分别复制到/opt/redis-sentinel/redis-sentinel-1/conf、/opt/redis-sentinel/redis-sentinel-2/conf和/opt/redis-sentinel/redis-sentinel-3/conf目录下。

进入/opt/docker-compose/redis-sentinel目录,执行docker-compose up -d,启动哨兵服务。

未经允许不得转载:君子如兰 » Docker Compose 安装Redis哨兵集群

赞 (0)