I’m trying to set up a Redis Cluster inside Docker using the following docker-compose.yml
file:
version: '3.9'
services:
redis-stack-node-1:
image: redis:latest
container_name: redis-stack-node-1
command: redis-server /redis/redis.conf
volumes:
- ./node1:/redis
ports:
- 7001:7001
networks:
- redis-cluster
redis-stack-node-2:
image: redis:latest
container_name: redis-stack-node-2
command: redis-server /redis/redis.conf
volumes:
- ./node2:/redis
ports:
- 7002:7002
networks:
- redis-cluster
redis-stack-node-3:
image: redis:latest
container_name: redis-stack-node-3
command: redis-server /redis/redis.conf
volumes:
- ./node3:/redis
ports:
- 7003:7003
networks:
- redis-cluster
redis-stack-node-4:
image: redis:latest
container_name: redis-stack-node-4
command: redis-server /redis/redis.conf
volumes:
- ./node4:/redis
ports:
- 7004:7004
networks:
- redis-cluster
redis-stack-node-5:
image: redis:latest
container_name: redis-stack-node-5
command: redis-server /redis/redis.conf
volumes:
- ./node5:/redis
ports:
- 7005:7005
networks:
- redis-cluster
redis-stack-node-6:
image: redis:latest
container_name: redis-stack-node-6
command: redis-server /redis/redis.conf
volumes:
- ./node6:/redis
ports:
- 7006:7006
networks:
- redis-cluster
redis-cluster-creator:
image: redis:latest
container_name: redis-cluster-creator
command: redis-cli -p 7001 --cluster create redis-stack-node-1:7001 redis-stack-node-2:7002 redis-stack-node-3:7003 redis-stack-node-4:7004 redis-stack-node-5:7005 redis-stack-node-6:7006 --cluster-replicas 1 --cluster-yes
depends_on:
- redis-stack-node-1
- redis-stack-node-2
- redis-stack-node-3
- redis-stack-node-4
- redis-stack-node-5
- redis-stack-node-6
networks:
- redis-cluster
redis-insight:
image: redislabs/redisinsight
container_name: redis-insight
ports:
- 5540:5540
networks:
- redis-cluster
volumes:
- ./redisinsight:/db
depends_on:
- redis-cluster-creator
networks:
redis-cluster:
driver: bridge
Each Redis node uses the following redis.conf
file (with port
changing from 7001 to 7006 and cluster-announce-bus-port
changing accordingly):
port 7001
bind 0.0.0.0
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
cluster-announce-port 7001
cluster-announce-bus-port 17001
The cluster runs fine inside Docker. I can connect between nodes using redis-cli from within the containers, and everything seems to work. The Docker host is running Ubuntu and has a public IP address.
Problem
I cannot connect to the cluster from an external client. Using tools like RESP.app
or RedisInsight
, the initial connection works, but the client cannot interact with the cluster.
It seems the client detects the cluster and redirects to individual nodes. However, the nodes return their internal Docker IPs instead of the host’s public IP, causing the connection to fail.
Here’s a screenshot of the error message from RESP.app:
What I’ve Tried
I set the cluster-announce-ip
to the host’s public IP in the redis.conf
. However, when I do this, the nodes fail to join the cluster.
Has anyone encountered this issue before or knows a solution? Any help would be greatly appreciated. Thank you!
2
Answers
I managed to solve the problem myself. When
cluster-announce-ip
is set to the host server's IP address, the nodes communicate with each other using the server's IP. Therefore, it is necessary to open the internal communication ports as well. I updated my Docker Compose file as follows:while using
try creating the cluster also with host’s public IP
You can also take a look at the following discussion
and example of a working configuration can be found here