Introduction
In this lab, you will learn how to elevate a worker node to a manager node in a Docker Swarm. You will begin by initializing a Docker Swarm on a LabEx VM and joining a worker node to it.
Following the setup, you will identify the worker node within the swarm and then use the docker node promote command to change its role to a manager. Finally, you will verify the node's new role to confirm the promotion was successful. This hands-on exercise will demonstrate a key aspect of Docker Swarm management.
Initialize a Docker Swarm
In this step, you will initialize a Docker Swarm on your LabEx VM. 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 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: baeda1f
Built: Tue Oct 25 18:01:18 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: 363bd3a
Built: Tue Oct 25 17:59:35 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.10
GitCommit: b4bd5d2bb63a5d10182b7e90689158e7c7b9b06b
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Now, let's initialize the Docker Swarm. When you initialize a swarm, the current node becomes the first manager node. Manager nodes handle swarm management tasks, such as maintaining the swarm state, scheduling services, and serving the swarm mode API.
Use the docker swarm init command to initialize the swarm. We will specify the advertise address to ensure other nodes can join the swarm using the VM's IP address. Replace YOUR_VM_IP_ADDRESS with the actual IP address of your LabEx VM. You can find this IP address in the LabEx environment details.
docker swarm init --advertise-addr YOUR_VM_IP_ADDRESS
After running the command, you will see output indicating that the swarm has been initialized and providing a command for other nodes to join the swarm as workers. Keep this join command handy, as you will need it in the next step.
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-xxxxxxxxxxxxxxxxxxxxxxxx YOUR_VM_IP_ADDRESS:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Join a worker node to the swarm
In the previous step, you initialized a Docker Swarm and the current node became the manager. Now, you will simulate adding a worker node to this swarm. In a real-world scenario, you would perform this step on a separate machine that you want to join as a worker. However, for this lab, we will use the same VM to simulate a worker node joining the swarm.
To join a node as a worker, you need the join command provided after initializing the swarm. This command includes the swarm join token and the IP address and port of the manager node.
If you don't have the join command from the previous step, you can retrieve it on the manager node using the docker swarm join-token worker command.
docker swarm join-token worker
This command will output the join command for a worker node:
To add a worker to this swarm, run the following command on the worker node:
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx YOUR_VM_IP_ADDRESS:2377
Now, execute the join command you obtained. Since we are simulating a worker on the same VM, you will run this command on the same terminal.
docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx YOUR_VM_IP_ADDRESS:2377
You should see output indicating that the node has joined the swarm as a worker:
This node joined a swarm as a worker.
This means your single VM is now acting as both a manager and a worker node within the swarm.
List swarm nodes and identify the worker node
In the previous steps, you initialized a Docker Swarm and then joined the same node to the swarm as a worker. Now, let's list the nodes in the swarm to see the current state and identify the roles of each node.
To list the nodes in the swarm, use the docker node ls command. This command provides information about each node in the swarm, including its ID, hostname, status, availability, manager status, and version.
docker node ls
You should see output similar to this:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
xxxxxxxxxxxx labex-vm Ready Active Leader 20.10.21
yyyyyyyyyyyy labex-vm Ready Active 20.10.21
In this output, you will see two entries, both with the hostname labex-vm. This is because your single VM is acting as two nodes in the swarm: one manager and one worker.
- The node with
MANAGER STATUSasLeaderis the manager node you initialized in the first step. - The node without a
MANAGER STATUSlisted is the worker node you joined in the second step.
Note the ID of the worker node. You will need this ID in the next step to promote it to a manager. The ID is a unique identifier for each node in the swarm.
Promote the worker node to a manager
In the previous step, you listed the nodes in the swarm and identified the worker node by its ID and the absence of a MANAGER STATUS. Now, you will promote this worker node to a manager node. Promoting a worker node to a manager increases the number of manager nodes in your swarm, which is important for high availability and fault tolerance in a production environment.
To promote a node, you use the docker node promote command followed by the ID of the node you want to promote. You obtained the worker node's ID in the previous step.
Replace WORKER_NODE_ID with the actual ID of your worker node.
docker node promote WORKER_NODE_ID
After executing the command, you should see output confirming that the node has been promoted:
Node WORKER_NODE_ID was promoted to a manager.
This indicates that the node that was previously a worker is now also a manager node in your swarm. Your swarm now has two manager nodes (although they are both running on the same physical VM in this simulated environment).
Verify the node's new role
In the previous step, you promoted the worker node to a manager. Now, let's verify that the node's role has been updated successfully by listing the nodes in the swarm again.
Use the docker node ls command to list the nodes.
docker node ls
This time, you should see output similar to this:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
xxxxxxxxxxxx labex-vm Ready Active Leader 20.10.21
yyyyyyyyyyyy labex-vm Ready Active Reachable 20.10.21
Notice that the node that was previously listed without a MANAGER STATUS now shows Reachable under MANAGER STATUS. This confirms that the node has been successfully promoted and is now recognized as a manager node by the swarm. The node with Leader status is the primary manager, while the node with Reachable status is a secondary manager.
Having multiple manager nodes in a swarm provides redundancy. If the leader manager node becomes unavailable, another manager node can take over its role, ensuring the continued operation of the swarm.
Summary
In this lab, you learned how to initialize a Docker Swarm on a single node, making it the first manager. You then practiced joining a worker node to this swarm, expanding the cluster. The lab guided you through listing the swarm nodes to identify the newly added worker. Finally, you performed the key action of promoting the worker node to a manager node using the docker node promote command and verified the successful role change.



