Table of Contents

You always need a local running Redis Cluster if your application has a dependency on the redis cluster. You can follow the below steps to create a redis cluster using docker-compose

redis cluster

Create the Redis Conf File

Move to the folder where you want to create the redis cluster. First, create a folder named redis-cluster

Bash
mkdir redis-cluter

Create a new folder inside the redis-cluster folder that will hold the configuration for all the redis cluster nodes

Bash
cd redis-cluster
mkdir redis
touch redis.conf

Put the below contents in the redis.conf file

Bash
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

move out of the redis folder and come to the redis-cluster directory

Bash
cd ..

Create the docker-compose file for Redis cluster

Bash
touch redis-cluster-docker-compose.yml

Put the below contents in the docker-compose file

Bash
version: '3'

networks:
  redis-cluster-compose:
    driver: bridge

services:
  redis-cluster-creator:
    command:
      - redis-cli
      - --cluster
      - create
      - localhost:7001
      - localhost:7002
      - localhost:7003
      - localhost:7004
      - localhost:7005
      - localhost:7006
      - --cluster-yes
      - --cluster-replicas
      - '1'
    depends_on:
      redis-node-1:
        condition: service_healthy
      redis-node-2:
        condition: service_healthy
      redis-node-3:
        condition: service_healthy
      redis-node-4:
        condition: service_healthy
      redis-node-5:
        condition: service_healthy
      redis-node-6:
        condition: service_healthy
    image: docker.io/redis:latest
    network_mode: host
  redis-node-1:
    command:
      - redis-server
      - /redis/redis.conf
      - --port
      - '7001'
    healthcheck:
      interval: 10s
      retries: '3'
      test:
        - CMD
        - redis-cli
        - -p
        - '7001'
        - -c
        - ping
      timeout: 5s
    image: docker.io/redis:latest
    network_mode: host
    ports:
      - 7001:7000
    volumes:
      - redis-data-1:/data
      - ./redis:/redis
  redis-node-2:
    command:
      - redis-server
      - /redis/redis.conf
      - --port
      - '7002'
    healthcheck:
      interval: 10s
      retries: '3'
      test:
        - CMD
        - redis-cli
        - -p
        - '7002'
        - -c
        - ping
      timeout: 5s
    image: docker.io/redis:latest
    network_mode: host
    ports:
      - 7002:7000
    volumes:
      - redis-data-2:/data
      - ./redis:/redis
  redis-node-3:
    command:
      - redis-server
      - /redis/redis.conf
      - --port
      - '7003'
    healthcheck:
      interval: 10s
      retries: '3'
      test:
        - CMD
        - redis-cli
        - -p
        - '7003'
        - -c
        - ping
      timeout: 5s
    image: docker.io/redis:latest
    network_mode: host
    ports:
      - 7003:7000
    volumes:
      - redis-data-3:/data
      - ./redis:/redis
  redis-node-4:
    command:
      - redis-server
      - /redis/redis.conf
      - --port
      - '7004'
    healthcheck:
      interval: 10s
      retries: '3'
      test:
        - CMD
        - redis-cli
        - -p
        - '7004'
        - -c
        - ping
      timeout: 5s
    image: docker.io/redis:latest
    network_mode: host
    ports:
      - 7004:7000
    volumes:
      - redis-data-4:/data
      - ./redis:/redis
  redis-node-5:
    command:
      - redis-server
      - /redis/redis.conf
      - --port
      - '7005'
    healthcheck:
      interval: 10s
      retries: '3'
      test:
        - CMD
        - redis-cli
        - -p
        - '7005'
        - -c
        - ping
      timeout: 5s
    image: docker.io/redis:latest
    network_mode: host
    ports:
      - 7005:7000
    volumes:
      - redis-data-5:/data
      - ./redis:/redis
  redis-node-6:
    command:
      - redis-server
      - /redis/redis.conf
      - --port
      - '7006'
    healthcheck:
      interval: 10s
      retries: '3'
      test:
        - CMD
        - redis-cli
        - -p
        - '7006'
        - -c
        - ping
      timeout: 5s
    image: docker.io/redis:latest
    network_mode: host
    ports:
      - 7006:7000
    volumes:
      - redis-data-6:/data
      - ./redis:/redis
volumes:
  redis-data-1: {}
  redis-data-2: {}
  redis-data-3: {}
  redis-data-4: {}
  redis-data-5: {}
  redis-data-6: {}

In the above configuration we will be creating six Redis Server nodes. There will be three master nodes and each master node will have one slave

Run the docker containers for Redis Cluster

Bash
 docker-compose -p "" -f redis-cluster-docker-compose.yml up

this will run the redis-cluster on your local machine

  • Redis containers are running on ports 7001, 7002, 7003, 7004, 7005, 7006

If you check the running containers you will get the below output

Bash
  docker-compose docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                   PORTS     NAMES
a6e5b1393497   redis:latest   "docker-entrypoint.s…"   11 minutes ago   Up 4 minutes (healthy)             docker-compose-redis-node-5-1
ed3f7bb7fbb8   redis:latest   "docker-entrypoint.s…"   11 minutes ago   Up 4 minutes (healthy)             docker-compose-redis-node-3-1
9ce7e124b13f   redis:latest   "docker-entrypoint.s…"   11 minutes ago   Up 4 minutes (healthy)             docker-compose-redis-node-4-1
39312cfeef00   redis:latest   "docker-entrypoint.s…"   11 minutes ago   Up 4 minutes (healthy)             docker-compose-redis-node-1-1
e14592f57170   redis:latest   "docker-entrypoint.s…"   11 minutes ago   Up 4 minutes (healthy)             docker-compose-redis-node-6-1
104be25c7404   redis:latest   "docker-entrypoint.s…"   11 minutes ago   Up 4 minutes (healthy)             docker-compose-redis-node-2-1

References

Leave a Reply

Your email address will not be published. Required fields are marked *