我有一个使用这个ioredis javascript 客户端库的 Node 应用程序。我的问题是我无法将这个也是 dockerized 的应用程序连接到我的 Redis 集群(端口 7000 到 7005)及其容器。这是我的设置和结果:
我克隆了这个Docker-redis-cluster repo 并按照说明从Dockerfile
.
$ docker build -t gsccheng/redis-cluster .
然而,在构建之前,我唯一的改变是设置我自己的版本,我已经更改为
ARG redis_version=3.2.1
按照我运行的说明
docker run -i -t -p 7000:7000 -p 7001:7001 -p 7002:7002 -p 7003:7003 -p 7004:7004 -p 7005:7005 -p 7006:7006 -p 7007:7007 gsccheng/redis-cluster
到目前为止很棒...
除了一些警告,例如:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
,到目前为止一切都很好。为什么?:
- 这是输出的屏幕截图。
- 我可以使用进入 CLI
redis-cli -c -p 7000
我
docker inspect [container id]
看到它的 NetworkSettings > IPAddress 为 172.17.0.2 并且在 NetworkSettings > Ports 我看到"Ports": { "6379/tcp": null, "7000/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "7000" } ], ... }
接下来,我停止并删除所有容器。我导航到我的 web 应用程序的根目录,其中我有 aDockerfile
和 adocker-compose.yml
在这里:
码头工人-compose.yml
version: '2.1'
services:
web:
build: .
ports:
- '8080:8080'
depends_on:
- redis
redis:
image: gsccheng/redis-cluster
ports:
- '7000:7000'
- '7001:7001'
- '7002:7002'
- '7003:7003'
- '7004:7004'
- '7005:7005'
Dockerfile
FROM node:4.4.7
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "run", "prod" ]
连接.js
import Redis from 'ioredis';
console.log('I am totally in here');
// The commented out host options shows the many different configurations I tried too.
const node = [{
// Just need to connect to at least one of the startup nodes in the cluster. Redis will find the rest of the nodes in the cluster. All nodes only have a single db of 0 and SELECT is disabled.
port: 7000,
// host: '0.0.0.0'
// host: '127.0.0.1'
// host: '172.18.0.4'
host: 'redis'
// host: 'gsccheng/redis-cluster'
}, {
port: 7001,
// host: '0.0.0.0'
// host: '127.0.0.1'
// host: '172.18.0.4'
host: 'redis'
// host: 'gsccheng/redis-cluster'
}]
client = new Redis.Cluster(node, {});
...
然后我跑
$ docker-compose build
和
$ docker-compose up
输出在这个gist中。对于这个例子,我从那里删除了一些不相关的行(用“...”表示)。
如果有什么不同,我的应用程序是从这个 React-redux样板构建的,但它不包含 Redis。
完整性检查
正如您在要点中看到的那样,随着
docker-compose up
运行和错误不断记录,当我执行时,我仍然可以从容器外部连接到集群:$ redis-cli -c -p 7000
$ docker ps
显示此屏幕截图。$ docker exec 4b3ec3e53e32 ps aux | grep redis
显示此屏幕截图。这是 redis 容器的完整输出。
$ docker inspect 4b3ec3e53e32
注意"IPAddress": ""
下NetworkSettings
...
进一步的问题
- ...为什么现在是空白的?这就是我的 Redis 客户端无法连接到集群的原因吗?
- 那个ip和那个ip有什么区别
"HostIp": "0.0.0.0",
?0.0.0.0
绑定在配置中redis.conf
,我认为这意味着服务器只会监听来自该地址的请求。这是否意味着那里列出的 IP 必须与我的其他ctn_web_1
容器的 ip 匹配? - 我们还从输出中看到
docker-compose up
Redis 服务器在172.18.0.2
. 我注意到这在我运行的实例之间也发生了很大变化(例如172.18.0.4
, ) 。为什么?是因为我在再次运行之前还没有完全删除容器吗?但是,我不应该在我的代码中使用这个 IP 地址,因为这个地址在 redis 容器内部(而我的 web 容器感兴趣的接口是 redis 容器的地址)?172.18.0.3
docker-compose
connect.js
更新:
A.$ docker-compose ps
输出在这里。B. 还要注意这个问题。更具体地说,
redis 集群还不支持 docker(除非你做主机网络)->广告的 ip 地址将不同于他们可以通过的地址