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.