How to setup multi-host networking with docker swarm on multiple remote machines

99封情书 提交于 2019-12-10 10:28:26

问题


Before asking this question I have read quiet of articles and stackoverflow questions but I couldn't get the right answer for my setup(perhaps it is already answered). Here is the architecture I have been struggling to get it to work.

  1. I have three physical machines and I would like to setup the Docker swarm with multi-host networking so that I can run docker-compose.

For example:

  1. Machine 1(Docker Swarm Manager and Contains Consoul)(192.168.5.11)
  2. Machine 2(Docker Swarm Node)(192.168.5.12)
  3. Machine 3 (Docker Swarm Node)(192.168.5.13)

And I need to run docker-compose from any other separate machine.

I have tried Docker article but in that article it is all setup under the same physical machine using docker-machine and virtual box. How can I achieve above in three remote machines. Any help appreciated.


回答1:


The latest version of Docker has Swarm Mode built in, so you don't need Consul.

To set up on your boxes, make sure they all have docker version of 1.12 or higher and then you just need to initialise the swarm and join it.

On Machine 1 run:

docker swarm init --advertise-addr 192.168.5.11

The output from that will tell you the command to run on Machine 2 and 3 to join them to the swarm. You'll have a unique swarm token, and the command is something like:

docker swarm join \
--token SWMTKN-1-49nj1... \
192.168.5.11:2377

Now you have a 3-node swarm. Back on Machine 1 you can create a multi-host overlay network:

docker network create -d overlay my-app

And then you run workloads in the network by deploying services. If you want to use Compose with Swarm Mode, you need to use distributed application bundles - which are currently only in the experimental build of Docker.




回答2:


I figured this needs an update, as docker compose files are supported in docker swarm

  1. Initialize the swarm on Machine 1 using

    docker swarm init --advertise-addr 192.168.5.11
    
  2. Join the swarm from Machine 2 & 3 using

    docker swarm join \
    --token <swarm token from previous step> 192.168.5.11:2377 \
    --advertise-addr eth0
    
    • eth0 is the network interface on machines 2 & 3, & could be different based on your config. I found that without the --advertise-addr option, containers couldn't talk to each other across hosts.
  3. To list all the nodes in the swarm & see their status

    docker node ls
    
  4. After this, deploy the stack (group of services or containers) from a compose file

    docker stack deploy -c <compose-file> my-app
    
    • This will create all the containers across multiple hosts
    • To list services (containers) on the swarm run docker service ls

See docker docs Getting started with swarm mode



来源:https://stackoverflow.com/questions/39844880/how-to-setup-multi-host-networking-with-docker-swarm-on-multiple-remote-machines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!