5

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.

Adiii
  • 54,482
  • 7
  • 145
  • 148
Anshuman Bardhan
  • 1,986
  • 2
  • 8
  • 10

3 Answers3

7

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

Adiii
  • 54,482
  • 7
  • 145
  • 148
Anshuman Bardhan
  • 1,986
  • 2
  • 8
  • 10
  • Hi, I'm able to create a Redis Cluster within Docker with Bitnami Image. I would like to connect to a cluster from my Spring Boot project via IntelliJ, is it possible to do such a thing? I'm using a Windows machine. Thanks in advance. – Nguyễn Đức Tâm Jul 30 '21 at 08:06
  • I think the comment "will only start after the master has booted completely" is misleading. In Docker Compose, the dependency only means that the depended-upon service gets started, but there's no wait or health check involved. – akauppi Oct 12 '21 at 17:22
  • @NguyễnĐứcTâm were you able to connect to a cluster from spring project via Intellij? I am trying to figure out the same. – sattu Mar 10 '22 at 02:59
6

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

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • $ docker-compose up --detach --scale redis-master=1 --scale redis-secondary=3 command only creates 1 slave node, tried increasing the secondary nodes but no success, 1. can you provide me a command to check or verify if they are connected? 2.Executing the above command changes the port number everytime – Anshuman Bardhan Nov 29 '19 at 15:04
  • 1
    in scaling you can not use the static port, if you want just add `ports: - '6379:6379'` in master – Adiii Nov 29 '19 at 15:11
  • the containers are created but automatically it goes down... 08:01:46.50 INFO ==> ** Starting Redis setup ** 08:01:46.62 INFO ==> Initializing Redis... mkdir: cannot create directory '/bitnami/redis': Permission denied Tried giving permissions but no luck – Anshuman Bardhan Dec 02 '19 at 08:04
0

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
Nima Sarayan
  • 185
  • 1
  • 11