Anshuman Bardhan
Anshuman Bardhan

Reputation: 2298

How to create redis-cluster in docker based environment

I want to create Redis cluster in my docker based environment, Any docker base image that supports replication and allow me to create cluster using docker-compose would be helpful.

Upvotes: 6

Views: 17149

Answers (3)

Nima Sarayan
Nima Sarayan

Reputation: 325

you can use this to create replica with master and slave 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

or you can use this with 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

Upvotes: 1

Anshuman Bardhan
Anshuman Bardhan

Reputation: 2298

Here is my working .yml file

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

Upvotes: 7

Adiii
Adiii

Reputation: 60074

You can use 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

Scale the number of replicas using:

$ 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

Upvotes: 6

Related Questions