只能访问网络中的一个Redis Docker

问题描述

我正在尝试建立一个Redis集群,该集群在专用容器上运行每个节点,但是在将容器彼此连接时遇到了麻烦。

我正在使用docker-compose进行设置:

version: '3'
services:
  redis-cluster-master_a:
    container_name: redis-cluster-master_a
    build:
      context: .
      dockerfile: Dockerfile.master_a
    volumes:
      - /docker_volumes/redis-cluster_data-master_a:/data
    ports: 
      - 7000:7000
    networks: 
      - redis-network

  redis-cluster-master_b:
    container_name: redis-cluster-master_b
    build:
      context: .
      dockerfile: Dockerfile.master_b
    volumes:
      - /docker_volumes/redis-cluster_data-master_b:/data
    ports: 
      - 7001:7001
    networks: 
      - redis-network

  redis-cluster-master_c:
    container_name: redis-cluster-master_c
    build:
      context: .
      dockerfile: Dockerfile.master_c
    volumes:
      - /docker_volumes/redis-cluster_data-master_c:/data
    ports: 
      - 7002:7002
    networks: 
      - redis-network
    
  redis-cluster-slave_a1:
    container_name: redis-cluster-slave_a1
    build:
      context: .
      dockerfile: Dockerfile.slave_ax
    volumes:
      - /docker_volumes/redis-cluster_data-slave_a1:/data
    ports: 
      - 7010:7010
    networks: 
      - redis-network

  redis-cluster-slave_b1:
    container_name: redis-cluster-slave_b1
    build:
      context: .
      dockerfile: Dockerfile.slave_bx
    volumes:
      - /docker_volumes/redis-cluster_data-slave_b1:/data
    ports: 
      - 7011:7011
    networks: 
      - redis-network
    
  redis-cluster-slave_c1:
    container_name: redis-cluster-slave_c1
    build:
      context: .
      dockerfile: Dockerfile.slave_cx
    volumes:
      - /docker_volumes/redis-cluster_data-slave_c1:/data
    ports: 
      - 7012:7012
    networks: 
      - redis-network
        

networks:
  redis-network:

一个Dockerfile看起来像这样:

FROM redis
COPY ./conf/redis-conf-master_a/master_a.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server","/usr/local/etc/redis/redis.conf" ]

如您所见,redis服务器在每个容器中运行,它们通过专用网络连接。我可以从inspect获取容器的IP地址,并通过redis-cli连接到master_a容器。

redis-cli -h 172.21.0.7 -p 7000

到目前为止,太好了。但是,当我尝试连接时,假设master_b拒绝连接:

redis-cli -h 172.21.0.4 -p 7001
Could not connect to Redis at 172.21.0.4:7001: Connection refused

而且由于我只能连接到一个容器,所以无法设置群集。


编辑1

master_a.conf的内容

# only listen to connections from specified hosts
# bind localhost
# accept connections on this port
port 7000 
# 'no' in order to accept connections from other hosts  
protected-mode no

# enable cluster mode
cluster-enabled yes 
# specifies timeout in ms at which node is considered as failed
cluster-node-timeout 5000 
# define the name of the nods's config file
cluster-config-file nodes-master_a.conf
# define location of logfile
logfile "" 

### PERSISTANCE ###
# enable AOF persistence
appendonly yes 
# define name of AOF file
appendfilename "appendonly.aof"  
# update AOF file after every query; other options are 'everysec' and 'no' (let the OS decide when it wants to flush)
appendfsync always 
# 'yes' to block fsync during incoming write commands in order to increase latency
no-appendfsync-on-rewrite no  
# if AOF file size grows by 100% (with respect to the size after the last write command),create a new one 
auto-aof-rewrite-percentage 100
# specify the minimum AOF file size for the file to be rewritten 
auto-aof-rewrite-min-size 64mb 

# create snapshot after 900s if 1 key changed
save 900 1 
# create snapshot after 300s if 10 keys changed
save 300 10 
save 60 10000
# define name of RDB file
dbfilename dump.rdb 

# 'no' in order to prevent Redis from stopping if an error occurs while trying to write to disk
stop-writes-on-bgsave-error no 
# define the location of the RDB & AOF file
# dir /docker_volumes/redis-cluster_data-master_a

### REPLICATION ###
# 'yes' in order to allow node to send (potential) out-of-sync data in case the connection to master node is lost
replica-serve-stale-data no 
replica-read-only yes 
# defines sync strategy of replicas (via a RDB file written on disk or network)
repl-diskless-sync no 

### SECURITY ###
# set password for master node authentication
masterauth *** 
# required in order to be able authenticate to master node
requirepass ***

编辑2

我检查了无法从外部连接的容器的容器处理过程,例如master_b:

docker container top redis-cluster-master_b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
systemd+            489115              489061              0                   06:38               ?                   00:00:01            redis-server localhost:7001 [cluster]
root                489570              489061              0                   06:42               pts/0               00:00:00            bash

从容器内部,我可以毫无问题地运行redis-cli ...因此,可疑容器中的服务器正在正确运行...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...