Learn about Swarm cluster management

  Swarm cluster management

Introduction

Docker Swarm is Docker's cluster management tool. It turns a pool of Docker hosts into a single virtual Docker host. Docker Swarm provides the standard Docker API, and any tool that already communicates with the Docker daemon can easily scale to multiple hosts using Swarm.

Supported tools include but are not limited to the following:

  • Dokku
  • Docker Compose
  • Docker Machine
  • Jenkins

principle

As shown in the figure below, a swarm cluster consists of a manager node and a work node.

  • swarm mananger : Responsible for the management of the entire cluster, including cluster configuration, service management, and all cluster-related tasks.
  • work node : the available node in the figure, which is mainly responsible for running corresponding services to perform tasks.


use

The following examples are all introduced with Docker Machine and virtualbox, make sure your host has installed virtualbox.

1. Create a swarm cluster management node (manager)

Create a docker machine:

$ docker-machine create -d virtualbox swarm-manager

Initialize the swarm cluster, and the machine for initialization is the management node of the cluster.

$ docker-machine ssh swarm-manager
$ docker swarm init --advertise-addr 192.168.99.107 #这里的 IP 为创建机器时分配的 ip。

The above output proves that the initialization has been successful. You need to copy the following line, which will be used when adding working nodes:

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

2. Create a swarm cluster working node (worker)

Here two machines are created directly, swarm-worker1 and swarm-worker2.

Enter the two machines respectively, 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 shows that it has been added successfully.

In the figure above, because the content copied in the previous step is relatively long, it will be automatically truncated. In fact, the commands run in the figure are as follows:

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

3. View cluster information

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

$ docker info

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

4. Deploy the service to the cluster

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

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

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

5. Check the service deployment status

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

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

View the specific information of helloworld deployment:

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

6. Expand cluster services

We extend the above 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.

7. Delete service

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

Check to see if it has been deleted:

8. Rolling upgrade service

In the following example, we will introduce how the redis version is rolled to a higher version.

Create a redis version 3.0.6.

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

You can see from the picture that the redis version has been upgraded from 3.0.6 to 3.0.7, indicating that the service has been upgraded successfully.

9. Stop a node from receiving new tasks

View all nodes:

docker@swarm-manager:~$ docker node ls

1631274865963004.png

It can be seen that all nodes are currently Active and can receive new task assignments.

Stop node swarm-worker1:

Note : swarm-worker1 status changes to Drain. It will not affect the services of the cluster, but the swarm-worker1 node will no longer receive new tasks, and the load capacity of the cluster will decrease.

Nodes can be reactivated with the following command:

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

Guess you like

Origin blog.csdn.net/2301_77700816/article/details/132150178