Minh Giang
Minh Giang

Reputation: 727

Cannot Connect to Redis Cluster inside Docker from Client

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. enter image description here

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: enter image description here

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!

Upvotes: 1

Views: 99

Answers (2)

Minh Giang
Minh Giang

Reputation: 727

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:

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
      - 17001:71001
    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
      - 17002:17002
    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
      - 17003:17003
    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
      - 17004:17004
    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
      - 17005:17005
    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
      - 17006:17006
    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

Upvotes: 0

Ivo Gaydazhiev
Ivo Gaydazhiev

Reputation: 26

while using

cluster-announce-ip to the host's public IP in the redis.conf

try creating the cluster also with host's public IP

redis-cli -p 7001 --cluster create <host-ip>:7001 <host-ip>:7002 <host-ip>:7003 <host-ip>:7004 <host-ip>:7005 <host-ip>:7006 --cluster-replicas 1 --cluster-yes

You can also take a look at the following discussion and example of a working configuration can be found here

Upvotes: 0

Related Questions