Understanding How Docker Swarm Works
Creating a Swarm Cluster
In this section, we will be using 3 machines:
- The "manager" machine: This machine will serve as the manager of the Swarm cluster. Its responsibilities include managing the cluster and scheduling containers on the worker nodes.
- The "worker01" machine: This machine will function as a worker node within the Swarm cluster. It will be responsible for running containers.
- The "worker02" machine: Similar to "worker01", this machine will also serve as a worker node within the Swarm cluster and run containers.
To ensure proper functionality, the following ports should be open on all machines:
- Port 2377 TCP: Cluster management traffic between managers and between managers and workers.
- Port 7946 TCP/UDP: It facilitates overlay network node discovery.
- Port 4789 UDP: This port is essential for overlay network traffic.
Additionally, Docker must be installed on all machines. Since this tutorial focuses on Ubuntu 22.04, you can install Docker using the following commands:
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh --version 29.1.5
After installing Docker, the Docker service should be running. You can check its status with the following command:
sudo systemctl status docker
If the service isn't running, you can start it using the following commands:
sudo systemctl enable docker
sudo systemctl start docker
Our cluster is not ready yet. We need to initialize the Swarm cluster and add the worker nodes to it.
Initializing the Swarm
To create and initialize the Swarm cluster, run docker swarm init on the manager node. The is the IP address of the manager node. You should use an address that every worker can reach (typically the private IP on the same network) because managers and workers communicate over it.
Let's run the following command on the manager node to initialize the Swarm cluster:
# Export the private IP address of the manager node as an environment variable
export MANAGER_IP=
# Initialize the Swarm cluster
docker swarm init --advertise-addr $MANAGER_IP
This will show output similar to the following:
Swarm initialized: current node (9i91nnzwypfbqfzjamtyucndy) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2o2julopgcjvmgt95p3eaqwp7evyy6xsqgj9fplqqdd6v332e0-77zt8cv7ea22m9igrl2tkuhp8 10.135.0.6:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
The output displays the command that needs to be executed on the worker nodes to add them to the cluster.
Copy the command and navigate to each of your worker nodes to run it. The command should be similar to the following:
docker swarm join --token :2377
Painless Docker - 2nd Edition
A Comprehensive Guide to Mastering Docker and its EcosystemEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
