Introduction
In this lab, you will learn how to manage nodes within a Docker Swarm using the docker swarm leave command. You will begin by initializing a Docker Swarm and joining a worker node to it. Subsequently, you will practice removing the worker node from the swarm. Finally, you will explore the process of removing a manager node, including the necessity of using the force option.
Initialize a Docker Swarm
In this step, you will learn how to initialize a Docker Swarm. A Docker Swarm is a cluster of Docker hosts that are running in swarm mode. Swarm mode enables you to manage a cluster of Docker nodes as a single virtual system.
Before initializing the swarm, let's check the current Docker version installed on the LabEx VM.
docker version
You should see output similar to this, indicating the Docker version is 20.10.21:
Client: Docker Engine - Community
Version: 20.10.21
API version: 1.41
Go version: go1.18.9
Git commit: baedd2a
Built: Tue Oct 25 17:58:10 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.21
API version: 1.41 (minimum version 1.12)
Go version: go1.18.9
Git commit: 305620d
Built: Tue Oct 25 17:56:51 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.10
GitCommit: b4bd567ea6c98e7b5d78a23676a0a79559d930d5
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
To initialize a Docker Swarm, you use the docker swarm init command. This command turns the current Docker host into a swarm manager.
docker swarm init
After running the command, you will see output indicating that the swarm has been initialized and providing a command to join other nodes as workers. The output will look similar to this:
Swarm initialized: current node (xxxxxxxxxxxx) is now a manager.
To add a worker to this swarm, run the following command on the worker node:
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 172.17.0.2:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
The output shows that the current node is now a swarm manager. It also provides the command to join a worker node to this swarm. We will use this command in the next step.
You can verify the swarm status using the docker info command.
docker info
Look for the "Swarm" section in the output. It should indicate that the swarm is "active".
...
Swarm: active
NodeID: xxxxxxxxxxxx
Is Manager: true
ClusterID: xxxxxxxxxxxx
Managers: 1
Nodes: 1
Orchestration:
TaskHistoryRetentionLimit: 5
Raft:
HeartbeatInterval: 100ms
ElectionTimeout: 3s
SnapshotInterval: 500ms
Dispatcher:
HeartbeatPeriod: 5s
CA configuration:
Expiry duration: 3 months
Force rotate at: 7 weeks
Root Rotation: false
Local Node State: active
Error: none
Remote Manager:
ID: xxxxxxxxxxxx
Addr: 172.17.0.2:2377
Addr: 172.17.0.2:2377
Node Address: 172.17.0.2
Manager Status:
Cluster Address: 172.17.0.2:2377
Leader: Yes
Reachability: Reachable
...
This confirms that the Docker Swarm has been successfully initialized on this node.
Join a worker node to the swarm
In the previous step, we initialized a Docker Swarm and the output provided a command to join a worker node. In a real-world scenario, you would execute this command on a separate machine that you want to add as a worker to your swarm. However, since we are using a single LabEx VM, we will simulate joining a worker node by using the join command on the same machine.
First, let's retrieve the join command for a worker node. You can get the join token and command by running docker swarm join-token worker on the manager node (which is our current VM).
docker swarm join-token worker
The output will be similar to this:
To add a worker to this swarm, run the following command on the worker node:
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 172.17.0.2:2377
Copy the full docker swarm join --token ... command from the output. This command contains the unique token and the IP address and port of the swarm manager.
Now, execute the copied command to join the current node to the swarm as a worker.
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 172.17.0.2:2377
Replace the token and IP address with the values you obtained from the docker swarm join-token worker command.
You should see output indicating that the node has joined the swarm as a worker:
This node joined a swarm as a worker.
To verify that the node has joined the swarm, you can list the nodes in the swarm from the manager perspective. Since our single VM is acting as both manager and worker, we can use the docker node ls command.
docker node ls
The output will show the nodes in the swarm. You should see two entries for the same node ID, one with the status "Ready" and role "Manager", and another with the status "Ready" and role "Worker".
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
xxxxxxxxxxxx * labex-vm Ready Active Leader 20.10.21
xxxxxxxxxxxx labex-vm Ready Active 20.10.21
This confirms that the node has successfully joined the swarm as a worker.
Leave the swarm from a worker node
In the previous step, we successfully joined the node to the swarm as a worker. Now, we will learn how to remove a node from the swarm. To leave a swarm from a worker node, you use the docker swarm leave command.
Execute the following command to make the current node leave the swarm as a worker:
docker swarm leave
You should see output indicating that the node has left the swarm:
Node left the swarm.
Now, let's verify that the node has left the swarm. We can use the docker node ls command again. However, since the node has left the swarm, running docker node ls on this node will result in an error because it is no longer part of the swarm.
docker node ls
You will see an error message similar to this:
Error: This node is not a swarm manager. Use "docker swarm join" to join the swarm and try again.
This error confirms that the node is no longer part of the swarm.
To further verify, let's check the swarm status using docker info.
docker info
The "Swarm" section in the output should now indicate that the swarm is "inactive".
...
Swarm: inactive
...
This confirms that the node has successfully left the Docker Swarm.
Attempt to leave the swarm from a manager node without force
In the previous step, we successfully removed a worker node from the swarm. Now, let's see what happens when we try to remove a manager node using the same docker swarm leave command without any additional options.
Execute the following command to attempt to leave the swarm from the manager node:
docker swarm leave
You will receive an error message similar to this:
Error response from daemon: You are attempting to leave the swarm on a node that is participating as a manager. Use the --force flag to force the node to leave the swarm. Note that this may disrupt the swarm, so use this option with caution.
This error message indicates that you cannot simply leave a swarm from a manager node without explicitly forcing it. This is a safety mechanism to prevent accidental disruption of the swarm, especially in a multi-manager setup.
To confirm that the node is still a manager and part of the swarm, you can use the docker info command.
docker info
The "Swarm" section should still show "active" and "Is Manager: true".
...
Swarm: active
NodeID: xxxxxxxxxxxx
Is Manager: true
...
This confirms that the attempt to leave the swarm without the force flag was unsuccessful, and the node remains a swarm manager.
Force a manager node to leave the swarm
In the previous step, we learned that a manager node cannot leave the swarm without using the --force flag. This is to prevent accidental disruption of the swarm. In this step, we will use the --force flag to make the manager node leave the swarm.
Important Note: Forcing a manager node to leave the swarm can disrupt the swarm, especially if it's the last manager. Use this command with caution in a production environment.
Execute the following command to force the manager node to leave the swarm:
docker swarm leave --force
You should see output indicating that the node has left the swarm:
Node left the swarm.
Now, let's verify that the node has successfully left the swarm. We can use the docker info command.
docker info
The "Swarm" section in the output should now indicate that the swarm is "inactive".
...
Swarm: inactive
...
This confirms that the manager node has successfully left the Docker Swarm by using the --force flag. The swarm no longer exists on this node.
Summary
In this lab, we learned how to initialize a Docker Swarm using the docker swarm init command, which designates the current node as a manager and provides the command for other nodes to join. We then practiced joining a worker node to the newly created swarm.
Subsequently, we explored how to remove nodes from the swarm using the docker swarm leave command, first demonstrating how a worker node can leave the swarm. We then attempted to use the same command on a manager node without the force option, observing that it is not permitted by default. Finally, we successfully forced a manager node to leave the swarm using the --force flag.



