docker 群中容器之间的通信
Communication between containers in docker swarm
我想在 docker swarm 模式下通过 WebSocket 连接在主节点和工作节点之间进行通信。
应该已经从工作节点到达主节点。
连接失败。
此外,我想通过 http 从我的主机连接到主节点。连接也失败了。
这是我的docker-compose.yml
version: '3'
services:
master:
image: master
build:
context: .
dockerfile: ./docker/master/Dockerfile
env_file:
- ./config.env
command: ['node', './src/master/']
ports:
- 8080:8080
networks:
- webnet
deploy:
replicas: 1
resources:
limits:
cpus: "0.2"
memory: 200M
restart_policy:
condition: none
worker:
image: worker
build:
context: .
dockerfile: ./docker/worker/Dockerfile
env_file:
- ./config.env
command: ['node', './src/worker/']
deploy:
replicas: 10
resources:
limits:
cpus: "0.1"
memory: 100M
restart_policy:
condition: none
networks:
- webnet
depends_on:
- master
networks:
webnet:
我用以下方法创建群:
docker swarm init
docker-compose build --no-cache
docker stack deploy -c docker-compose.yml ${stack_name}
我尝试通过以下方式到达主节点:
curl http://localhost:8080/result
并获得
curl: (7) Failed to connect to localhost port 8080: Connection refused
有关 master 节点的更多详细信息:
环境:
MASTER_PORT=3000
MASTER_HOST=localhost
MASTER_HTTP_PORT=8080
启动 websocket 服务器的代码:
const wss = new WebSocket.Server({ host: config.masterHost, port: config.masterPort }, (err) => {
if (err != null) {
throw err
}
console.log(`Web Socket server started on address ws://${config.masterHost}:${config.masterPort}`)
});
启动 http 服务器的代码:
server.listen(config.masterHttpPort, config.masterHost, (err) => {
if (err != null) {
throw err
}
console.log(`Http server started on address http://${config.masterHost}:${config.masterHttpPort}`);
});
有关 worker 节点的更多详细信息:
环境
MASTER_SERVICE_HOST=master
MASTER_PORT=3000
websocket客户端连接master节点的代码:
const masterUri = `ws://${config.masterServiceHost}:${config.masterPort}`;
console.log(`Connecting to ${masterUri}`);
const ws = new WebSocket(masterUri, {perMessageDeflate: false});
我真的很困惑,因为当我 运行 使用 docker-compose 的应用程序(不在群模式下)时,一切都运行良好。
但是在集群模式下,我都无法到达
来自主机的主节点通过 http
我也够不到
master节点从worker节点通过websocket连接
我怀疑我 docker 网络配置不正确。
来自 master 节点的日志:
Process started with config: {
"grouperFile": "./src/operations/group.js",
"initialDelay": 10,
"mapperFile": "./src/operations/map.js",
"masterHost": "localhost",
"masterHttpPort": 8080,
"masterPort": 3000,
"masterServiceHost": "master",
"slaveReplicationFactor": 1
}
Web Socket server started on address ws://localhost:3000
Http server started on address http://localhost:8080
此外,来自 worker 节点的一些日志:
Process started with config: {
"grouperFile": "./src/operations/group.js",
"initialDelay": 10,
"mapperFile": "./src/operations/map.js",
"masterHost": "master",
"masterHttpPort": 8080,
"masterPort": 3000,
"slaveReplicationFactor": 1
}
Connecting to ws://master:3000
events.js:174
throw er; // Unhandled 'error' event
^
Error: getaddrinfo EAI_AGAIN master master:3000
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
Emitted 'error' event at:
at ClientRequest.req.on (/app/node_modules/ws/lib/websocket.js:554:10)
at ClientRequest.emit (events.js:189:13)
at Socket.socketErrorListener (_http_client.js:392:9)
at Socket.emit (events.js:189:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
这是你的问题:
Web Socket server started on address ws://localhost:3000
Http server started on address http://localhost:8080
不要绑定到本地主机。绑定到任何 IP 0.0.0.0。
或者为了更安全绑定到 hostname -I
IP
对于容器之外的所有内容,它不是本地主机,它是另一个主机,尽管您 运行 在同一台机器上。
我想在 docker swarm 模式下通过 WebSocket 连接在主节点和工作节点之间进行通信。
应该已经从工作节点到达主节点。 连接失败。
此外,我想通过 http 从我的主机连接到主节点。连接也失败了。
这是我的docker-compose.yml
version: '3'
services:
master:
image: master
build:
context: .
dockerfile: ./docker/master/Dockerfile
env_file:
- ./config.env
command: ['node', './src/master/']
ports:
- 8080:8080
networks:
- webnet
deploy:
replicas: 1
resources:
limits:
cpus: "0.2"
memory: 200M
restart_policy:
condition: none
worker:
image: worker
build:
context: .
dockerfile: ./docker/worker/Dockerfile
env_file:
- ./config.env
command: ['node', './src/worker/']
deploy:
replicas: 10
resources:
limits:
cpus: "0.1"
memory: 100M
restart_policy:
condition: none
networks:
- webnet
depends_on:
- master
networks:
webnet:
我用以下方法创建群:
docker swarm init
docker-compose build --no-cache
docker stack deploy -c docker-compose.yml ${stack_name}
我尝试通过以下方式到达主节点:
curl http://localhost:8080/result
并获得
curl: (7) Failed to connect to localhost port 8080: Connection refused
有关 master 节点的更多详细信息:
环境:
MASTER_PORT=3000
MASTER_HOST=localhost
MASTER_HTTP_PORT=8080
启动 websocket 服务器的代码:
const wss = new WebSocket.Server({ host: config.masterHost, port: config.masterPort }, (err) => {
if (err != null) {
throw err
}
console.log(`Web Socket server started on address ws://${config.masterHost}:${config.masterPort}`)
});
启动 http 服务器的代码:
server.listen(config.masterHttpPort, config.masterHost, (err) => {
if (err != null) {
throw err
}
console.log(`Http server started on address http://${config.masterHost}:${config.masterHttpPort}`);
});
有关 worker 节点的更多详细信息:
环境
MASTER_SERVICE_HOST=master
MASTER_PORT=3000
websocket客户端连接master节点的代码:
const masterUri = `ws://${config.masterServiceHost}:${config.masterPort}`;
console.log(`Connecting to ${masterUri}`);
const ws = new WebSocket(masterUri, {perMessageDeflate: false});
我真的很困惑,因为当我 运行 使用 docker-compose 的应用程序(不在群模式下)时,一切都运行良好。
但是在集群模式下,我都无法到达
来自主机的主节点通过 http
我也够不到
master节点从worker节点通过websocket连接
我怀疑我 docker 网络配置不正确。
来自 master 节点的日志:
Process started with config: {
"grouperFile": "./src/operations/group.js",
"initialDelay": 10,
"mapperFile": "./src/operations/map.js",
"masterHost": "localhost",
"masterHttpPort": 8080,
"masterPort": 3000,
"masterServiceHost": "master",
"slaveReplicationFactor": 1
}
Web Socket server started on address ws://localhost:3000
Http server started on address http://localhost:8080
此外,来自 worker 节点的一些日志:
Process started with config: {
"grouperFile": "./src/operations/group.js",
"initialDelay": 10,
"mapperFile": "./src/operations/map.js",
"masterHost": "master",
"masterHttpPort": 8080,
"masterPort": 3000,
"slaveReplicationFactor": 1
}
Connecting to ws://master:3000
events.js:174
throw er; // Unhandled 'error' event
^
Error: getaddrinfo EAI_AGAIN master master:3000
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
Emitted 'error' event at:
at ClientRequest.req.on (/app/node_modules/ws/lib/websocket.js:554:10)
at ClientRequest.emit (events.js:189:13)
at Socket.socketErrorListener (_http_client.js:392:9)
at Socket.emit (events.js:189:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
这是你的问题:
Web Socket server started on address ws://localhost:3000
Http server started on address http://localhost:8080
不要绑定到本地主机。绑定到任何 IP 0.0.0.0。
或者为了更安全绑定到 hostname -I
IP
对于容器之外的所有内容,它不是本地主机,它是另一个主机,尽管您 运行 在同一台机器上。