如何在基于Docker的环境中创建Redis集群

时间:2019-11-29 13:13:06

标签: docker redis-cluster

我想在基于docker的环境中创建Redis集群,任何支持复制并允许我使用docker-compose创建集群的docker基本映像都会有所帮助。

2 个答案:

答案 0 :(得分:2)

您可以使用bitnami-docker-redis

借助Docker Compose,可以使用以下方式设置主/副本模式:

version: '2'

services:
  redis-master:
    image: 'bitnami/redis:latest'
    ports:
      - '6379'
    environment:
      - REDIS_REPLICATION_MODE=master
      - REDIS_PASSWORD=my_master_password
    volumes:
      - '/path/to/redis-persistence:/bitnami'

  redis-replica:
    image: 'bitnami/redis:latest'
    ports:
      - '6379'
    depends_on:
      - redis-master
    environment:
      - REDIS_REPLICATION_MODE=slave
      - REDIS_MASTER_HOST=redis-master
      - REDIS_MASTER_PORT_NUMBER=6379
      - REDIS_MASTER_PASSWORD=my_master_password
      - REDIS_PASSWORD=my_replica_password

使用以下方法缩放副本数量:

$ docker-compose up --detach --scale redis-master=1 --scale redis-secondary=3

  

以上命令将副本数扩展到3。您可以扩展   以相同的方式下降。

     

注意:您不应按比例放大/缩小主节点的数量。总是   只有一个主节点正在运行。

bitnami-docker-redis-cluster

答案 1 :(得分:0)

这是我正在工作的.yml文件

version: '3.7'

services:
  fix-redis-volume-ownership: # This service is to authorise redis-master with ownership permissions
    image: 'bitnami/redis:latest'
    user: root
    command: chown -R 1001:1001 /bitnami
    volumes:
      - ./data/redis:/bitnami
      - ./data/redis/conf/redis.conf:/opt/bitnami/redis/conf/redis.conf

  redis-master: # Setting up master node
    image: 'bitnami/redis:latest'
    ports:
      - '6329:6379' # Port 6329 will be exposed to handle connections from outside server 
    environment:
      - REDIS_REPLICATION_MODE=master # Assigning the node as a master
      - ALLOW_EMPTY_PASSWORD=yes # No password authentication required/ provide password if needed
    volumes:
      - ./data/redis:/bitnami # Redis master data volume
      - ./data/redis/conf/redis.conf:/opt/bitnami/redis/conf/redis.conf # Redis master configuration volume


  redis-replica: # Setting up slave node
    image: 'bitnami/redis:latest'
    ports:
      - '6379' # No port is exposed 
    depends_on:
      - redis-master # will only start after the master has booted completely
    environment:
      - REDIS_REPLICATION_MODE=slave # Assigning the node as slave
      - REDIS_MASTER_HOST=redis-master # Host for the slave node is the redis-master node
      - REDIS_MASTER_PORT_NUMBER=6379 # Port number for local 
      - ALLOW_EMPTY_PASSWORD=yes # No password required to connect to node