Swarm Cluster Management
Introduction
Docker Swarm is Docker's clustering and management tool. It transforms a pool of Docker hosts into a single virtual Docker host. Docker Swarm provides a standard Docker API, so any tool that already communicates with the Docker daemon can easily scale out 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 manager nodes (manager) and worker nodes (work node).
- swarm mananger: Responsible for the management of the entire cluster, including all cluster-related work such as cluster configuration and service management.
- work node: That is, the available node in the figure, mainly responsible for running the corresponding services to execute tasks.
Use
The following examples are all introduced using Docker Machine and VirtualBox; make sure VirtualBox is installed on your host.
1. Create a swarm cluster manager node (manager)
Create a docker machine:
$ docker-machine create -d virtualbox swarm-manager
Initialize the swarm cluster; the machine performing the initialization becomes the cluster's manager node.
$ docker-machine ssh swarm-manager
$ docker swarm init --advertise-addr 192.168.99.107 #这里的 IP 为创建机器时分配的 ip。
The output above confirms that initialization succeeded. You need to 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 (worker)
Here we directly create two machines, swarm-worker1 and swarm-worker2.
Enter each of the two machines separately and specify adding them to the cluster created in the previous step; here you will use the content copied in the previous step.
The output above indicates that the addition succeeded.
In the figure above, because the content copied in the previous step is fairly long, it is automatically truncated; 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 manager node and run: docker info to view information about the current cluster.
$ docker info
From the areas circled in red, you can tell that the currently running cluster has three nodes, one of which is a manager node.
4. Deploy a service to the cluster
Note: Any operation related to cluster management is performed on the manager 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. View service deployment status
Check which node the helloworld service is running on; you can see it is currently on the swarm-worker1 node:
docker@swarm-manager:~$ docker service ps helloworld
View detailed information about the helloworld deployment:
docker@swarm-manager:~$ docker service inspect --pretty helloworld
6. Scale a cluster service
We will scale the aforementioned helloworld service to two nodes.
docker@swarm-manager:~$ docker service scale helloworld=2
You can see that it has been scaled from one node to two nodes.
7. Delete a service
docker@swarm-manager:~$ docker service rm helloworld
Check whether it has been deleted:
8. Rolling update of a service
In the following example, we will introduce how to roll a redis version up to a higher version.
Create a redis of version 3.0.6.
docker@swarm-manager:~$ docker service create --replicas 1 --name redis --update-delay 10s redis:3.0.6
Rolling update redis.
docker@swarm-manager:~$ docker service update --image redis:3.0.7 redis
As you can see from the figure, the redis version has been upgraded from 3.0.6 to 3.0.7, indicating that the service was 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 state of swarm-worker1 changes to Drain. This does not affect the cluster's services; it only means the swarm-worker1 node no longer receives new tasks, and the cluster's load capacity decreases somewhat.
You can reactivate the node with the following command:
docker@swarm-manager:~$ docker node update --availability active swarm-worker1

















