KingOfSocket
KingOfSocket

Reputation: 293

How to set up a Docker Swarm cluster with overlay network mode

I create a docker swarm cluster on 2 Linux machine, but when I use docker-compose up -d to start containers , some error has occurred.

This is my docker info:

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

This is my 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"

after I exec docker-compose up -d , the service rabbitmq and config-service can be started up , but eureka-service caused an error:

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

And I exec 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

I find that rabbitmq is start up on machine ozculster01, config-service is start up on machine ozculster02.

when docker-compose start config-service, there is no links, so it can be start up successfully. but when I start eureka-service on machine ozculster02,there is a links to rabbitmq, but the service rabbitmq is on machine ozculster01, the error occurred.

How could I do to resolve the problem ?
Is that right to use network_mode: "bridge" in Docker Swarm cluster ?

Upvotes: 2

Views: 1020

Answers (1)

KingOfSocket
KingOfSocket

Reputation: 293

I resolved the problem myself.

In swarm mode, docker containers can't contact another container with network_mode:bridge.

In swarm mode, one must use network_mode : overlay. Overlay is used by default if you are using compose-file formate version 2.

see more detail : Setting up a Docker Swarm with network overlay

With overlay mode , docker-compose.yml file do not need the config likns , Containers can contact another container with ${service_name_in_composeFile}

Example:

I can enter into the container config-service , and $ ping eureka-service, and it works fine!

this is my 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

Upvotes: 2

Related Questions