Docker ~ Swarm builds a highly available docker cluster and swarmdocker

Source: Internet
Author: User
Tags docker swarm

Docker ~ Swarm builds a highly available docker cluster and swarmdocker

Back to directory

Swarm Concept

Swarm is a Docker company launched to manage docker clusters. It turns a group of Docker hosts into a single, virtual host. Swarm uses standard Docker API interfaces as its front-end access portals. in other words, various forms of Docker clients (such as docker Client in Go, docker_py, and docker) can communicate directly with Swarm. Almost all Swarm is developed using the go language. Swarm0.2 is released. Compared with version 0.1, version 0.2 adds a new policy to schedule containers in the cluster so that they can be spread on available nodes, and supports more Docker commands and cluster drivers.
Swarm deamon is only a sched and a router. Swarm does not run containers by itself. It only accepts requests sent from the docker client and schedules suitable nodes to run containers, this means that even if Swarm fails for some reason, the nodes in the cluster will run as usual. After Swarm resumes running, it will collect and reconstruct the cluster information.

Swarm Structure

Basic Swarm commands

Docker node is used to display the cluster nodes. By default, only one node is created, which of course cannot be highly available. You can use docker node -- help to view all node parameters.

Cluster initialization docker swarm init

After initialization, you cannot re-execute this operation. Use docker node ls to view the created Cluster.

Manage nodes and work nodes in the Cluster


ADD management node docker swarm join

Some options need to be added to the Docker Swarm command: * join: indicates that a new node will be added to Swarm * –Manager: indicate the nature of the node (manager vs worker) * --Listen-addr: Allow a newly added node to access other nodes in Swarm * The last parameter is the address of the first management node (that is, the node to which this command will be sent) Note: Because the –auto-accept manager option is provided during the Swarm initialization process, the second management node is automatically accepted. Without this option, the second management node needs to be manually accepted by the first management node. $ MANAGER2_IP = $ (docker-machine ip manager2) docker-machine ssh manager2 docker swarm join --manager --listen-addr $ MANAGER2_IP: 2377 $ MANAGER1_IP: 2377

The script used for Swarn deployment, from the network

The following is a Shell script used to create a Docker host and deploy Swarm. Of course, the number of management/work nodes can be changed at will.
Note: creating two management nodes and two working nodes is only used for demonstration. In industrial production, we may need to build three management nodes and five work nodes in the cluster.

# Define the number of managers/workers

# Create the Docker hosts
for i in $(seq 1 $MANAGER); do docker-machine create --driver virtualbox manager$i; done
for i in $(seq 1 $WORKER); do docker-machine create --driver virtualbox worker$i; done

# Init the swarm
docker-machine ssh manager1 docker swarm init --auto-accept manager --auto-accept worker --listen-addr $(docker-machine ip manager1):2377

# Add additional manager(s)
for i in $(seq 2 $MANAGER); do docker-machine ssh manager$i docker swarm join --manager --listen-addr $(docker-machine ip manager$i):2377 $(docker-machine ip manager1):2377; done

# Add workers
for i in $(seq 1 $WORKER); do docker-machine ssh worker$i docker swarm join --listen-addr $(docker-machine ip worker$i):2377 $(docker-machine ip manager1):2377; done

For the cluster mentioned in the above article, we didn't talk about how to use it. After creating a cluster, we can use

Docker stack deploy-c test. yml test

Below is a self-Written service, version 3

version: "3"

    image: logger.api
      context: ./src/Logger.Api
      dockerfile: Dockerfile
      - "5000:80"
      - ingress

    image: logger.manager
      context: ./src/Logger.Manager
      dockerfile: Dockerfile
      - "5050:80"
      - ingress


Note that the name of a service must be different. For example, logger. manager is incorrect!

To create a service. You can also use docker service to view the running service!

Back to directory


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.