YouTip LogoYouTip

Docker Swarm

### 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 a standard Docker API, so 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 diagram below, a swarm cluster consists of manager nodes and worker nodes. * **swarm manager**: Responsible for all management tasks of the entire cluster, including cluster configuration, service management, and all other cluster-related work. * **worker node**: As shown in the diagram as "available node," primarily responsible for running the corresponding services to execute tasks. [!(#)](#) * * * ## Usage The following examples are introduced using Docker Machine and VirtualBox. Ensure your host has VirtualBox installed. ### 1. Create a swarm cluster manager node 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 #The IP here is the one assigned when creating the machine. [!(#)](#) The output above proves the initialization was successful. You need to copy the following line, as it 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 Here, we directly create two machines: swarm-worker1 and swarm-worker2. [!(#)](#) Enter each machine and specify adding it to the cluster created in the previous step. This will use the content copied in the last step. [!(#)](#) The data output above indicates the addition was successful. In the image above, because the content copied in the previous step is long, it gets automatically truncated. The command actually run in the image 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 information about the current cluster. $ docker info [!(#)](#) From the circled area, you can see 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. The following example creates a service named `helloworld` on a worker node. 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 the cluster service We will scale the `helloworld` service from the previous example to two nodes. docker@swarm-manager:~$ docker service scale helloworld=2 [!(#)](#) You can see it has been scaled from one node to two nodes. [!(#)](#) ### 7. Delete a service docker@swarm-manager:~$ docker service rm helloworld [!(#)](#) Check if it has been deleted: [!(#)](#) ### 8. Rolling upgrade of a service The following example introduces how to perform a rolling upgrade of a Redis version to a higher one. Create a Redis service with version 3.0.6. docker@swarm-manager:~$ docker service create --replicas 1 --name redis --update-delay 10s redis:3.0.6 [!(#)](#) Perform a rolling upgrade of Redis. docker@swarm-manager:~$ docker service update --image redis:3.0.7 redis [!(#)](#) From the image, you can see that the Redis version has been upgraded from 3.0.6 to 3.0.7, indicating the service upgrade was successful. ### 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 the node `swarm-worker1`: [!(#)](#) **Note**: The status of `swarm-worker1` changes to Drain. This will not affect the cluster's services; it only means the `swarm-worker1` node will no longer receive new tasks, and the cluster's load-bearing capacity is slightly reduced. You can reactivate the node with the following command: docker@swarm-manager:~$ docker node update --availability active swarm-worker1 [!(#)](#)
← Jsref FilterJavascript Json Parse β†’