Docker Swarm Cluster Management
Docker Swarm 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 all tools that already communicate with the Docker daemon can be easily scaled to multiple hosts using Swarm.
Supported tools include, but are not limited to, the following.
- Dokku
- Docker Compose
- Docker Machine
- Jenkins
Docker Swarm Principle
As shown in the figure below, a swarm cluster consists of a manager and a work node.
- swarm mananger: responsible for the management of the whole cluster including cluster configuration, service management and all cluster-related work.
- work node: the available node in the diagram, mainly responsible for running the corresponding services to execute tasks.
Docker Swarm Usage
The following examples are introduced with Docker Machine and virtualbox, make sure you have virtualbox installed on your host.
1. Create swarm cluster manager node (manager)
To create a docker machine.
$ docker-machine create -d virtualbox swarm-manager
Initialize the swarm cluster. The machine that does 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 the initialization has been successful. The following line needs to be copied out and will be used when adding working nodes.
docker swarm join --token SWMTKN-1-4oogo9qziq768dma0uh3j0z0m5twlm10iynvz7ixza96k6jh9p-ajkb6w7qd06y1e33yrgko64sk 192.168.99.107:2377
2. Create swarm cluster worker nodes (workers)
Here we create two machines directly, swarm-worker1 and swarm-worker2.
Go into the two machines separately and specify to add the cluster created in the previous step, where the contents copied in the previous step will be used.
The above data output indicates that it has been added successfully.
In the above figure, the content copied in the previous step will be automatically truncated due to its length, and the command actually run in the figure 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 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 the management node.
4. Deploying services to the cluster
Note: Any operation related to cluster management is done on the management node.
In the following example, a service named helloworld is created on a worker node, here it 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
To see which node the helloworld service is running on, you can see that it is currently at the swarm-worker1 node.
docker@swarm-manager:~$ docker service ps helloworld
View specific information about the helloworld deployment at:
docker@swarm-manager:~$ docker service inspect --pretty helloworld
6. Extending the cluster service
We extend the above helloworld service to two nodes.
docker@swarm-manager:~$ docker service scale helloworld=2
You can see that it has been extended from one node, to two nodes.
7. Delete services
docker@swarm-manager:~$ docker service rm helloworld
To see if it has been deleted.
8. Rolling Upgrade Service
In the following example, we will describe how the redis version can be rolled up to a higher 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
Rolling redis upgrade.
docker@swarm-manager:~$ docker service update --image redis:3.0.7 redis
As you can see from the diagram, the version of redis has been upgraded from 3.0.6 to 3.0.7, which means the service has been upgraded successfully.
9. Stop 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 node swarm-worker1.
Note: The swarm-worker1 status changes to Drain. it does not affect the services of the cluster, only that the swarm-worker1 node no longer receives new tasks and the load capacity of the cluster decreases.
The node can be reactivated with the following command.
docker@swarm-manager:~$ docker node update --availability active swarm-worker1