如何设置具有覆盖网络模式的Docker Swarm集群

时间:2016-11-04 02:25:30

标签: docker docker-compose docker-swarm

我在2台Linux机器上创建了一个docker swarm集群,但是当我使用docker-compose up -d启动容器时,发生了一些错误。

这是我的码头信息:

Containers: 4
 Running: 4
 Paused: 0
 Stopped: 0
Images: 28
Server Version: swarm/1.2.5
Role: primary
Strategy: spread
Filters: health, port, containerslots, dependency, affinity, constraint
Nodes: 2
 ozcluster01: 192.168.168.41:2375
  └ ID: CKCO:JGAA:PIOM:F4PL:6TIH:EQFY:KZ6X:B64Q:HRFH:FSTT:MLJT:BJUY
  └ Status: Healthy
  └ Containers: 2 (2 Running, 0 Paused, 0 Stopped)
  └ Reserved CPUs: 0 / 2
  └ Reserved Memory: 0 B / 3.79 GiB
  └ Labels: executiondriver=native-0.2, kernelversion=3.10.0-    327.13.1.el7.x86_64, operatingsystem=CentOS Linux 7 (Core),     storagedriver=devicemapper
  └ UpdatedAt: 2016-11-04T02:05:08Z
  └ ServerVersion: 1.10.3
 ozcluster02: 192.168.168.42:2375
  └ ID: 73GR:6M7W:GMWD:D3DO:UASW:YHJ2:BTH6:DCO5:NJM6:SXPN:PXTY:3NHI
  └ Status: Healthy
  └ Containers: 2 (2 Running, 0 Paused, 0 Stopped)
  └ Reserved CPUs: 0 / 2
  └ Reserved Memory: 64 MiB / 3.79 GiB
  └ Labels: executiondriver=native-0.2, kernelversion=3.10.0-327.10.1.el7.x86_64, operatingsystem=CentOS Linux 7 (Core), storagedriver=devicemapper
  └ UpdatedAt: 2016-11-04T02:05:06Z
  └ ServerVersion: 1.10.3

这是我的docker-compose.yml

version: '2'
services:
  rabbitmq:
    image: rabbitmq
    ports:
     - "5672:5672"
     - "15672:15672"
    network_mode: "bridge"
  config-service:
    image: ozms/config-service
    ports:
     - "8888:8888"
    volumes:
     - ~/ozms/configs:/var/tmp/
     - ~/ozms/log:/log
    network_mode: "bridge"
    labels:
     - "affinity:image==ozms/config-service"
  eureka-service:
    image: ozms/eureka-service
    ports:
    - "8761:8761"
    volumes:
     - ~/ozms/log:/log
    links:
     - config-service
     - rabbitmq
    environment:
     - SPRING_RABBITMQ_HOST=rabbitmq
    network_mode: "bridge"

执行docker-compose up -d后,服务rabbitmqconfig-service可以启动,但eureka-service导致错误:

[dannil@ozcluster01 ozms]$ docker-compose up -d
Creating ozms_config-service_1
Creating ozms_rabbitmq_1
Creating ozms_eureka-service_1
ERROR: Unable to find a node that satisfies the following conditions 
[port 8761 (Bridge mode)]
[available container slots]
[--link=ozms_config-service_1:config-service --link=ozms_config-service_1:config-service_1 --link=ozms_config-service_1:ozms_config-service_1 --link=ozms_rabbitmq_1:ozms_rabbitmq_1 --link=ozms_rabbitmq_1:rabbitmq --link=ozms_rabbitmq_1:rabbitmq_1]

我执行docker ps

CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                                                                           NAMES
871afc8e1eb6        rabbitmq              "docker-entrypoint.sh"   2 minutes ago       Up 2 minutes        4369/tcp, 192.168.168.41:5672->5672/tcp, 5671/tcp, 25672/tcp, 192.168.168.41:15672->15672/tcp   ozcluster01/ozms_rabbitmq_1
8ef3f666a7b9        ozms/config-service   "java -Djava.security"   2 minutes ago       Up 2 minutes        192.168.168.42:8888->8888/tcp                                                                   ozcluster02/ozms_config-service_1

我发现rabbitmq在计算机ozculster01启动,config-service在计算机ozculster02启动。

当docker-compose start config-service时,没有links,因此可以成功启动。 但是,当我在计算机eureka-service上启动ozculster02时,有一个linksrabbitmq,但服务rabbitmq位于计算机ozculster01上, 发生了错误。

我该如何解决这个问题呢? 在Docker Swarm集群中使用network_mode: "bridge"是否正确?

1 个答案:

答案 0 :(得分:1)

我自己解决了这个问题。

在群集模式下,docker容器无法通过network_mode:bridge与另一个容器联系。

在群集模式下,必须使用network_mode : overlay。如果您使用的是compose-file formate version 2,则默认使用Overlay。

查看更多详情:  Setting up a Docker Swarm with network overlay

使用overlay模式,docker-compose.yml文件不需要配置likns,容器可以与${service_name_in_composeFile}

联系其他容器

示例:

我可以进入容器config-service$ ping eureka-service,它运行正常!

这是我的compose-file.yml:

version: '2'
services:
  rabbitmq:
    image: rabbitmq
    ports:
     - "5672:5672"
     - "15672:15672"
  config-service:
    image: ozms/config-service
    ports:
     - "8888:8888"
    volumes:
     - ~/ozms/configs:/var/tmp/
     - ~/ozms/log:/log
    labels:
     - "affinity:image==ozms/config-service"
  eureka-service:
    image: ozms/eureka-service
    ports:
    - "8761:8761"
    volumes:
     - ~/ozms/log:/log
    #links:    it is no need  in overlay mode
    # - config-service
    # - rabbitmq
    environment:
     - SPRING_RABBITMQ_HOST=rabbitmq