English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Swarm Cluster Management

Introduction

Docker Swarm is a cluster management tool for Docker. It transforms a pool of Docker hosts into a single virtual Docker host. Docker Swarm provides a standard Docker API, and any tool that has already communicated with the Docker daemon can easily extend to multiple hosts using Swarm.

The supported tools include but are not limited to the following items:

  • Dokku

  • Docker Compose

  • Docker Machine

  • Jenkins

Principle

As shown in the figure below, the swarm cluster is composed of management nodes (manager) and work nodes (work node).

  • swarm mananger: responsible for the management of the entire cluster, including cluster configuration, service management, and all work related to the cluster.

  • work node: which is the available node in the diagram, mainly responsible for running the corresponding services to execute tasks (tasks).

Use

The following examples are introduced using Docker Machine and virtualbox, make sure that virtualbox is installed on your host.

1、Create swarm cluster management node (manager)

Create docker machine:

$ docker-machine create -d virtualbox swarm-manager

Initialize the swarm cluster, the machine that performs the initialization is the management node of the cluster.

$ docker-machine ssh swarm-manager
$ docker swarm init --advertise-addr 192.168.99.107 #The IP here is the ip assigned when the machine was created.

The above output proves that it has been initialized successfully. You need to copy the following line and use it when adding a worker node:

docker swarm join --token SWMTKN-1-4oogo9qziq768dma0uh2j0z0m5twlm10iynvz7ixza96k6jh9p-ajkb6w7qd06y1e33yrgko64sk 192.168.99.107:2377

2Create swarm cluster worker (worker)

Here we directly create two machines and swarm-worker1 and swarm-worker2 .

Enter two machines separately, specify to add to the cluster created in the previous step, and the content copied in the previous step will be used here.

The above data output indicates that it has been added successfully.

In the above figure, due to the long content copied in the previous step, it will be automatically truncated. In fact, the command running in the figure is as follows:

docker@swarm-worker1:~$ docker swarm join --token SWMTKN-1-4oogo9qziq768dma0uh2j0z0m5twlm10iynvz7ixza96k6jh9p-ajkb6w7qd06y1e33yrgko64sk 192.168.99.107:2377

3Check Cluster Information

Enter the management node and execute: docker info to view the current cluster information.

$ docker info

By looking at the red circle, you can know that there are three nodes currently running in the cluster, one of which is the management node.

4Deploy Service to Cluster

NoteAny operation related to cluster management is performed on the management node.

In the following example, create a service named helloworld on a worker node, and it is randomly assigned to a worker node here:

docker@swarm-manager:~$ docker service create --replicas 1 --name helloworld alpine ping docker.com

5Check Service Deployment Status

Check which node the helloworld service is running on, you can see that it is currently running on swarm.-worker1 Node:

docker@swarm-manager:~$ docker service ps helloworld

Check the specific information of helloworld deployment:

docker@swarm-manager:~$ docker service inspect --pretty helloworld

6Expand Cluster Service

We will expand the helloworld service to two nodes.

docker@swarm-manager:~$ docker service scale helloworld=2

You can see that it has expanded from one node to two nodes.

7Delete Service

docker@swarm-manager:~$ docker service rm helloworld

Check if it has been deleted:

8Rolling Upgrade Service

In the following example, we will introduce how to roll upgrade the redis version to a higher version.

Create a 3.0.6 version of redis.

docker@swarm-manager:~$ docker service create --replicas 1 --name redis --update-delay 10s redis:3.0.6

Rolling upgrade redis.

docker@swarm-manager:~$ docker service update --image redis:3.0.7 redis

From the picture, you can see that the version of redis has been upgraded from 3.0.6 Upgraded to 3.0.7indicates that the service has been upgraded successfully.

9Stop a node from receiving new tasks

View all nodes:

docker@swarm-manager:~$ docker node ls

You can see that all nodes are currently Active and can receive new task assignments.

Stop the node swarm-worker1:

Note:swarm-worker1 The status changes to Drain. It will not affect the cluster's services, just the swarm-worker1 The node no longer receives new tasks, and the cluster's load capacity has decreased.

You can reactivate the node with the following command:

docker@swarm-manager:~$ docker node update --availability active swarm-worker1