如何在基于 docker 的环境中创建 redis-cluster

How to create redis-cluster in docker based environment

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

您可以使用 bitnami-docker-redis.

With Docker Compose the master/replica mode can be setup using:

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

The above command scales up the number of replicas to 3. You can scale down in the same way.

Note: You should not scale up/down the number of master nodes. Always have only one master node running.

bitnami-docker-redis-cluster

这是我的工作 .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

您可以使用它来创建具有主从节点的副本

version: '3'

services:
  redis:
    image: redis:5.0.0
    container_name: master
    ports:
      - "6379:6379"
    networks:
      - redis-replication

  redis-slave:
    image: redis:5.0.0
    container_name: slave
    ports:
      - "6380:6379"
    command: redis-server --slaveof master 6379
    depends_on:
      - redis
    networks:
      - redis-replication

networks:
  redis-replication:
    driver: bridge

或者您可以将此与 redislabs/redismod 一起使用:

redis:
  image: redislabs/redismod:latest
ports:
  - "6329:6329"
command:
  [
    "--loadmodule",
    "/usr/lib/redis/modules/redisai.so",
    "--loadmodule",
    "/usr/lib/redis/modules/redisearch.so",
    "--loadmodule",
    "/usr/lib/redis/modules/redisgraph.so",
    "--loadmodule",
    "/usr/lib/redis/modules/redistimeseries.so",
    "--loadmodule",
    "/usr/lib/redis/modules/rejson.so",
    "--loadmodule",
    "/usr/lib/redis/modules/redisbloom.so",
    "--loadmodule",
    "/usr/lib/redis/modules/redisgears.so",
    "Plugin",
    "/var/opt/redislabs/modules/rg/plugin/gears_python.so",
    --port 6329,
  ]
redis-slave:
  image: redislabs/redismod:latest
  ports:
    - "6380:6379"
  command:
    [
      "--loadmodule",
      "/usr/lib/redis/modules/redisai.so",
      "--loadmodule",
      "/usr/lib/redis/modules/redisearch.so",
      "--loadmodule",
      "/usr/lib/redis/modules/redisgraph.so",
      "--loadmodule",
      "/usr/lib/redis/modules/redistimeseries.so",
      "--loadmodule",
      "/usr/lib/redis/modules/rejson.so",
      "--loadmodule",
      "/usr/lib/redis/modules/redisbloom.so",
      "--loadmodule",
      "/usr/lib/redis/modules/redisgears.so",
      "Plugin",
      "/var/opt/redislabs/modules/rg/plugin/gears_python.so",
      --REPLICAOF redis 6329,
  ]
depends_on:
  - redis