繁体   English   中英

码头工人群中的容器之间的通信

[英]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:

我用以下方法创建了swarm:

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
有关节点的更多详细信息:

ENV:

 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}`); }); 
有关工作节点的更多详细信息:

信封

const masterUri = `ws://${config.masterServiceHost}:${config.masterPort}`;
console.log(`Connecting to ${masterUri}`);
const ws = new WebSocket(masterUri, {perMessageDeflate: false});

将websocket客户端连接到主节点的代码:

 const masterUri = `ws://${config.masterServiceHost}:${config.masterPort}`; console.log(`Connecting to ${masterUri}`); const ws = new WebSocket(masterUri, {perMessageDeflate: false}); 

我真的很困惑,因为当我使用docker-compose运行应用程序时(不是在swarm模式下),一切都很完美。

但在群体模式中,我无法达到

来自主机的主节点通过http

我也达不到

工作节点的主节点通过websocket连接

我怀疑我错误地配置了docker网络。

来自节点的日志:

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)

此外,来自工作节点的一些日志:

 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

不要绑定到localhost。 绑定到任何IP 0.0.0.0。 或者为了更安全地绑定到hostname -I IP尽管你在同一台机器上运行,但对于容器外的所有容器都不是localhost,它是另一个主机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM