2.17.1. Brief introduction ¶
Docker Swarm is the cluster management tool of Docker. It transforms the Docker host pool into a single virtual Docker host. Docker Swarm provides standard Docker API, and any tool that has communicated with the Docker daemon can be easily extended to multiple hosts using Swarm.
Supported tools include, but are not limited to:
Dokku
Docker Compose
Docker Machine
Jenkins
2.17.2. Principle ¶
As shown in the following figure, the swarm cluster consists of a management node (manager) and a work node (work node).
swarm mananger Responsible for the management of the entire cluster, including cluster configuration, service management and other cluster-related work.
work node Available node in the figure is mainly responsible for running the corresponding service to perform the task (task).
` <../wp-content/uploads/2019/11/services-diagram.png>` __ The following examples are introduced in Docker Machine and virtualbox to make sure your host has virtualbox installed.Use ¶
2.17.3. 1. Create a swarm cluster management node (manager) ¶
Create a docker machine:
$ docker-machine create -d virtualbox swarm-manager
` <../wp-content/uploads/2019/11/swarm1.png>` __
Initialize the swarm cluster, and the machine that initializes it is the management node of the cluster.
$ docker-machine ssh swarm-manager
$ docker swarm init --advertise-addr 192.168.99.107 #这里的 IP 为创建机器时分配的 ip。
` <../wp-content/uploads/2019/11/swarm2.png>` __
The above output proves that it has been initialized successfully. The following line needs to be copied, which will be used when adding work nodes:
docker swarm join --token SWMTKN-1-4oogo9qziq768dma0uh3j0z0m5twlm10iynvz7ixza96k6jh9p-ajkb6w7qd06y1e33yrgko64sk 192.168.99.107:2377
2.17.4. 2. Create a swarm cluster work node (worker) ¶
Create two machines directly here, swarm-worker1 and swarm-worker2.
` <../wp-content/uploads/2019/11/swarm3.png>` __
Enter each of the two machines and specify to add to the cluster created in the previous step. Here, the content copied in the previous step will be used.
` <../wp-content/uploads/2019/11/swarm4.png>` __
The above data output indicates that it has been added successfully.
In the figure above, because the content copied in the previous step is relatively long, it will be truncated automatically. In fact, the command running in the figure is as follows:
docker@swarm-worker1:~$ docker swarm join --token SWMTKN-1-4oogo9qziq768dma0uh3j0z0m5twlm10iynvz7ixza96k6jh9p-ajkb6w7qd06y1e33yrgko64sk 192.168.99.107:2377
2.17.5. 3. View cluster information ¶
Go to the management node and execute: docker info can view the information of the current cluster.
$ docker info
` <../wp-content/uploads/2019/11/swarm5.png>` __
By drawing a red circle, you can see that there are three nodes in the currently running cluster, one of which is a management node.
2.17.6. 4. Deploy services to the cluster ¶
注意 Any operation related to cluster management is performed on the management node
The following example creates a service called 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
` <../wp-content/uploads/2019/11/swarm6.png>` __
2.17.7. 5. Check the deployment of services ¶
Looking at the node on which the helloworld service is running, you can see that it is currently on the swarm-worker1 node:
docker@swarm-manager:~$ docker service ps helloworld
` <../wp-content/uploads/2019/11/swarm7.png>` __
View the details of the helloworld deployment:
docker@swarm-manager:~$ docker service inspect --pretty helloworld
` <../wp-content/uploads/2019/11/swarm8.png>` __
2.17.8. 6. Expand the cluster service ¶
We extend the above helloworld service to two nodes.
docker@swarm-manager:~$ docker service scale helloworld=2
` <../wp-content/uploads/2019/11/swarm9.png>` __
You can see that it has expanded from one node to two nodes.
` <../wp-content/uploads/2019/11/swarm10.png>` __
2.17.9. 7. Delete the service ¶
docker@swarm-manager:~$ docker service rm helloworld
` <../wp-content/uploads/2019/11/swarm11.png>` __
Check to see if it has been deleted:
` <../wp-content/uploads/2019/11/swarm12.png>` __
2.17.10. 8. Rolling upgrade service ¶
In the following example, we will show how the redis version is rolled up to a later version.
Create a version 3.0.6 of redis.
docker@swarm-manager:~$ docker service create --replicas 1 --name redis --update-delay 10s redis:3.0.6
` <../wp-content/uploads/2019/11/swarm13.png>` __
Scroll to upgrade redis.
docker@swarm-manager:~$ docker service update --image redis:3.0.7 redis
` <../wp-content/uploads/2019/11/swarm14.png>` __
Looking at the figure, you can see that the version of redis has been upgraded from 3.0.6 to 3.0.7, indicating that the service has been upgraded successfully.
2.17.11. 9. Stop a node from receiving new tasks ¶
View all nodes:
docker@swarm-manager:~$ docker node ls
` <../wp-content/uploads/2019/11/swarm16.png>` __
You can see that all the current nodes are Active and can receive new task assignments.
Stop Node swarm-worker1:
` <../wp-content/uploads/2019/11/swarm17.png>` __
注意 The swarm-worker1 status changes to Drain. The service of the cluster will not be affected, but the swarm-worker1 node will no longer receive new tasks, and the load capacity of the cluster will be reduced.
You can reactivate the node with the following command:
docker@swarm-manager:~$ docker node update --availability active swarm-worker1
` <../wp-content/uploads/2019/11/swarm19.png>` __