docker swarm中容器之间的通信

时间:2019-02-24 11:39:11

标签: node.js docker docker-compose dockerfile 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 节点的更多详细信息:

Env

MASTER_SERVICE_HOST=master
MASTER_PORT=3000

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

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

我真的很困惑,因为当我使用docker-compose(不在群体模式下)运行应用程序时,一切正常。

但是在群体模式下,我都无法达到

通过http

从主机获得的主节点

我也无法到达

工作节点通过Websocket连接建立的主节点

我怀疑我没有正确配置docker网络。

主节点节点上的日志:

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)

1 个答案:

答案 0 :(得分:0)

这是您的问题:

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 对于所有外部容器,尽管您在同一台计算机上运行,​​但它不是本地主机,而是另一台主机。