How to use docker swarm join command to add nodes

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to add nodes to an existing Docker Swarm using the docker swarm join command. We will cover the process of joining a node as a worker, which executes tasks assigned by managers, and as a manager, which participates in swarm management.

You will also explore the --availability flag and understand how to use it when joining nodes to control their initial availability within the swarm. This hands-on lab will provide practical experience with the essential steps for expanding your Docker Swarm cluster.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/NetworkOperationsGroup(["Network Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/exec("Execute Command in Container") docker/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/run -.-> lab-555241{{"How to use docker swarm join command to add nodes"}} docker/ps -.-> lab-555241{{"How to use docker swarm join command to add nodes"}} docker/exec -.-> lab-555241{{"How to use docker swarm join command to add nodes"}} docker/network -.-> lab-555241{{"How to use docker swarm join command to add nodes"}} end

Join a node to a swarm as a worker

In this step, we will learn how to join a node to an existing Docker Swarm as a worker. A worker node is a machine that runs the services deployed to the swarm. It receives and executes tasks assigned by the manager nodes.

Before joining a node to a swarm, you need to have a running Docker Swarm manager node. For this lab, we assume a swarm is already initialized and you have the join token for worker nodes.

First, let's simulate having a join token. In a real scenario, you would get this token from the manager node using the docker swarm join-token worker command. For this lab, we will use a placeholder token.

Let's assume the join command provided by the manager is similar to this:

docker swarm join --token SWMTKN-1-abcdefghijklmnopqrstuvwxyz-1234567890abcdef --advertise-addr <THIS_NODE_IP> <MANAGER_IP>:2377

In this command:

  • docker swarm join: This is the command to join a node to a swarm.
  • --token SWMTKN-1-abcdefghijklmnopqrstuvwxyz-1234567890abcdef: This is the unique token that authenticates the node to the swarm.
  • --advertise-addr <THIS_NODE_IP>: This flag specifies the IP address that the joining node will use to advertise itself to other nodes in the swarm. Replace <THIS_NODE_IP> with the actual IP address of the node you are joining.
  • <MANAGER_IP>:2377: This is the address of a manager node in the swarm. The joining node connects to this address to join the swarm. Replace <MANAGER_IP> with the actual IP address of a manager node. The default swarm port is 2377.

Since we are working in a single VM environment for this lab, we will simulate the join process using a simplified command. We will use 127.0.0.1 as the manager IP and a placeholder token.

Execute the following command to simulate joining the swarm as a worker. Note that this command will not actually join a real swarm in this single VM setup, but it demonstrates the command structure.

docker swarm join --token SWMTKN-1-abcdefghijklmnopqrstuvwxyz-1234567890abcdef 127.0.0.1:2377

You should see output indicating that the node is attempting to join the swarm. In a real scenario, if the join is successful, you would see a message like "This node joined a swarm as a worker."

Join a node to a swarm as a manager

In this step, we will learn how to join a node to an existing Docker Swarm as a manager. Manager nodes handle swarm management tasks, such as maintaining the swarm state, scheduling services, and handling swarm-wide operations. A swarm can have multiple manager nodes for high availability.

Similar to joining as a worker, you need a join token from an existing manager node to join as a manager. The command to get the manager join token from an existing manager node is docker swarm join-token manager.

Let's assume the join command for a manager node is similar to this:

docker swarm join --token SWMTKN-1-abcdefghijklmnopqrstuvwxyz-1234567890abcdef --advertise-addr <THIS_NODE_IP> <MANAGER_IP>:2377

Notice that the command structure is the same as joining a worker, but the token is different. The token for joining a manager node is unique and grants higher privileges.

For this lab, we will again simulate the join process in our single VM environment using a placeholder manager token and 127.0.0.1 as the manager IP.

Execute the following command to simulate joining the swarm as a manager:

docker swarm join --token SWMTKN-1-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0987654321fedcba 127.0.0.1:2377

You should see output indicating that the node is attempting to join the swarm. In a real multi-node swarm, if the join is successful, you would see a message like "This node joined a swarm as a manager."

Understand and use --availability flag when joining

In this step, we will explore the --availability flag, which can be used when joining a node to a Docker Swarm. The --availability flag controls whether a node is available for scheduling tasks. This is particularly useful for managing node maintenance or temporarily taking a node out of service without removing it from the swarm.

The --availability flag can take one of three values:

  • active: The node is available for scheduling tasks. This is the default state for a newly joined node.
  • pause: The node will not be assigned new tasks, but existing tasks will continue to run.
  • drain: The node will not be assigned new tasks, and existing tasks will be shut down and rescheduled on other available nodes.

When joining a node, you can specify its initial availability. For example, to join a node as a worker with its availability set to drain, you would use a command like this:

docker swarm join --token SWMTKN-1-abcdefghijklmnopqrstuvwxyz-1234567890abcdef --availability drain --advertise-addr <THIS_NODE_IP> <MANAGER_IP>:2377

For this lab, we will simulate joining a node with the --availability drain flag. Again, we will use a placeholder token and 127.0.0.1 as the manager IP in our single VM environment.

Execute the following command to simulate joining the swarm with availability set to drain:

docker swarm join --token SWMTKN-1-abcdefghijklmnopqrstuvwxyz-1234567890abcdef --availability drain 127.0.0.1:2377

You should see output indicating the node is attempting to join. In a real swarm, the node would join with its availability set to drain, meaning it would not be scheduled for new tasks and existing tasks would be moved.

Summary

In this lab, we learned how to use the docker swarm join command to add nodes to an existing Docker Swarm. We explored the process of joining a node as a worker, understanding the role of the join token, the manager address, and the --advertise-addr flag. We also examined how to join a node as a manager, which requires a different join token obtained from the manager node. Finally, we learned about the --availability flag and how it can be used during the join process to control the initial availability of a node within the swarm.