Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Cluster Docker Compose

I'm struggling to create a Docker Compose to create a Redis Cluster. I saw that there is a Redis Cluster image from Bitnami, I tried but it my Spring Boot App cannot connect to it due to the below error: enter image description here

I tried another approach is to create 2 Redis instances master-slave and I can connect to it. Now I'm trying to create 6 Redis Instances and later create a Redis Cluster with 3 master and 3 slaves with the following command:

redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 \
127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 --cluster-replicas 1

But when I executed the command it said that

Could not connect to Redis at 127.0.0.1:6380: Connection refused

Below is my current Docker-compose.yaml:

version: '3.8'
services:
  redis-node-0:
    image: redis:latest
    container_name: redis-0
    ports:
      - "6380:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-0:/redis/data

  redis-node-1:
    image: redis:latest
    container_name: redis-1
    ports:
      - "6381:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]  
    volumes:
      - redis-cluster_data-1:/redis/data

  redis-node-2:
    image: redis:latest
    container_name: redis-2
    ports:
      - "6382:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-2:/redis/data

  redis-node-3:
    image: redis:latest
    container_name: redis-3
    ports:
      - "6383:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-3:/redis/data

  redis-node-4:
    image: redis:latest
    container_name: redis-4
    ports:
      - "6384:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-4:/redis/data

  redis-node-5:
    image: redis:latest
    container_name: redis-5
    ports:
      - "6385:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-5:/redis/data


networks: 
  default:
    name: overlay
volumes:
  redis-cluster_data-0:
    driver: local
  redis-cluster_data-1:
    driver: local
  redis-cluster_data-2:
    driver: local
  redis-cluster_data-3:
    driver: local
  redis-cluster_data-4:
    driver: local
  redis-cluster_data-5:
    driver: local

I'm totally new to Both Docker and Redis, I'm learning so any help would be really appreciated. Thanks in advance.

like image 380
Nguyễn Đức Tâm Avatar asked Sep 01 '26 02:09

Nguyễn Đức Tâm


2 Answers

The way to do this is not obvious, because Redis Cluster doesn't easily work with Docker bridge networking. The simplest way to setup a single-node cluster is to cheat and bind it to 127.0.0.1:

version: '3.8'

services:
  redis-single-node-cluster:
    image: docker.io/bitnami/redis-cluster:7.0
    environment:
      - 'ALLOW_EMPTY_PASSWORD=yes'
      - 'REDIS_CLUSTER_REPLICAS=0'
      - 'REDIS_NODES=127.0.0.1 127.0.0.1 127.0.0.1'
      - 'REDIS_CLUSTER_CREATOR=yes'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1'
    ports:
      - '6379:6379'

You can then connect to it with this Spring Boot config:

spring:
  redis:
    cluster:
      nodes: [localhost:6379]
    ssl: false

If you want to setup multiple nodes, you'll have to create a custom network and assign static IPs. You'll probably also have to set network_mode: host.

like image 68
Michael Böckling Avatar answered Sep 02 '26 17:09

Michael Böckling


I faced the same problem, many thanks to Michael Böckling for the answer, but for me it was not the final solution, besides that I made some changes, maybe it will be useful for someone

version: '3.8'

services:
  app:
    ...
    environment:
      - REDIS_URL=redis://:@redis-cluster:6379/0
    depends_on:
      redis-cluster:
        condition: service_started


  redis-cluster:
    image: docker.io/bitnami/redis-cluster:7.0
    environment:
      - 'ALLOW_EMPTY_PASSWORD=yes'
      - 'REDIS_CLUSTER_REPLICAS=0'
      - 'REDIS_NODES=redis-cluster redis-cluster redis-cluster'
      - 'REDIS_CLUSTER_CREATOR=yes'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=redis-cluster'
    ports:
      - '6379:6379'
like image 23
Денис Викторов Avatar answered Sep 02 '26 17:09

Денис Викторов



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!