Easy Tutorial
❮ Centos Docker Install Docker Container Connection ❯

Swarm Cluster Management

Introduction

Docker Swarm is Docker's cluster management tool. It transforms a pool of Docker hosts into a single virtual Docker host. Docker Swarm provides the standard Docker API, so any tools that already communicate with the Docker daemon can use Swarm to transparently scale to multiple hosts.

Supported tools include but are not limited to the following:

Principles

As shown in the diagram below, a swarm cluster consists of manager nodes and worker nodes.

Image Link


Usage

The following examples are introduced using Docker Machine and VirtualBox, ensuring your host has VirtualBox installed.

1. Create a Swarm Cluster Manager Node

Create a Docker machine:

$ docker-machine create -d virtualbox swarm-manager

Image Link

Initialize the swarm cluster. The machine that initializes the cluster becomes the manager node.

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

Image Link

The above output indicates successful initialization. Copy the following line, which will be used when adding worker nodes:

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

2. Create Swarm Cluster Worker Nodes

Create two machines directly: swarm-worker1 and swarm-worker2.

Image Link

Enter each machine and specify adding to the cluster created in the previous step. The content copied earlier will be used here.

Image Link

The above data output indicates successful addition.

In the diagram, the content copied earlier is quite long and may be truncated. The actual command run is as follows:

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

3. View Cluster Information

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

$ docker info

Image Link

By looking at the circled areas, you can see that there are three nodes in the currently running cluster, with one being the manager node.

4. Deploy Services to the Cluster

Note: Any operations related to cluster management are performed on the manager node.

The following example creates a service named helloworld on a worker node. It is assigned to a worker node randomly:

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

Image Link

5. Check Service Deployment Status

Check which node the helloworld service is running on. It is currently on the swarm-worker1 node:

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

Image Link

Check the specific information of the helloworld deployment:

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

Image Link

6. Scale Cluster Services

We will scale the above helloworld service to two nodes.

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

Image Link

It can be seen that the service has been scaled from one node to two nodes.

Image Link

7. Remove Service

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

Image Link

Check if it has been removed:

Image Link

8. Rolling Update Service

The following example demonstrates how to roll Redis version updates to a higher version.

Create a Redis with version 3.0.6.

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

Image Link

Roll update Redis.

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

Image Link

The diagram shows that the Redis version has been upgraded from 3.0.6 to 3.0.7, indicating that the service has been successfully upgraded.

9. Stop a Node from Receiving New Tasks

View all nodes:

docker@swarm-manager:~$ docker node ls

Image Link

All nodes are currently Active and can receive new task assignments.

Stop the swarm-worker1 node:

Image Link

Note: The status of swarm-worker1 changes to Drain. This does not affect the cluster's services, but the swarm-worker1 node will no longer receive new tasks, reducing the cluster's load capacity.

You can reactivate the node with the following command:

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

Image Link

❮ Centos Docker Install Docker Container Connection ❯