1080*80 ad

Setting Up a Three-Node Docker Swarm Cluster on Ubuntu 22.04

How to Set Up a Docker Swarm Cluster on Ubuntu 22.04: A Step-by-Step Guide

Container orchestration is essential for managing modern, scalable applications. While Kubernetes often dominates the conversation, Docker Swarm offers a simpler, lightweight, and powerful alternative directly integrated into the Docker Engine. If you’re looking to build a resilient and high-availability environment, a Docker Swarm cluster is an excellent starting point.

This guide will walk you through the entire process of creating a robust three-node Docker Swarm cluster on Ubuntu 22.04, consisting of one manager node and two worker nodes.

Prerequisites

Before we begin, ensure you have the following setup:

  • Three servers running Ubuntu 22.04. These can be virtual machines or physical hardware.
  • A static IP address configured on each server.
  • Root or sudo access on all three nodes.
  • A clear naming convention. For this tutorial, we will use manager, worker1, and worker2.

Step 1: Install Docker Engine on All Three Nodes

The first step is to install the Docker Engine on every server that will be part of the swarm. The process is identical for the manager and both worker nodes.

First, update your package list and install the necessary dependencies to allow apt to use a repository over HTTPS:

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release

Next, add Docker’s official GPG key to ensure the authenticity of the packages you’re about to install:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Now, set up the Docker repository in your apt sources:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Finally, update your package list again and install the latest version of Docker Engine, containerd, and Docker Compose:

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

Verify the installation by checking the Docker service status. It should be active and running:

sudo systemctl status docker

Repeat these installation steps on all three nodes (manager, worker1, and worker2).

Step 2: Configure the Firewall for Swarm Communication

For the nodes to communicate and form a cluster, specific network ports must be open. If you are using ufw (Uncomplicated Firewall) on Ubuntu, you need to allow traffic on these ports.

The following ports are critical for Docker Swarm operations:

  • TCP port 2377: For cluster management communications.
  • TCP and UDP port 7946: For communication among nodes.
  • UDP port 4789: For overlay network traffic.

Run these commands on all three nodes to open the required ports:

sudo ufw allow 2377/tcp
sudo ufw allow 7946/tcp
sudo ufw allow 7946/udp
sudo ufw allow 4789/udp

After adding the rules, reload the firewall to apply the changes:

sudo ufw reload

Proper firewall configuration is a critical security step to ensure your cluster is both functional and protected from unauthorized access.

Step 3: Initialize the Swarm on the Manager Node

Now it’s time to create the swarm. This action is performed only on the node you designate as the manager.

On your manager server, run the docker swarm init command. It’s crucial to use the --advertise-addr flag to specify the manager’s IP address. This is the address that other nodes will use to connect to the swarm.

Replace <MANAGER_IP> with your manager node’s actual static IP address:

sudo docker swarm init --advertise-addr <MANAGER_IP>

Upon successful execution, the command will output a message indicating that the swarm has been initialized. More importantly, it will provide you with the command and a unique token needed for worker nodes to join the swarm.

The output will look similar to this:

Swarm initialized: current node (dxn1d2q123...) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-49nj... <MANAGER_IP>:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Copy this entire docker swarm join command. You will need it in the next step.

Step 4: Join Worker Nodes to the Swarm

With the manager ready, you can now add your worker nodes to the cluster. Log into your worker1 server and paste the docker swarm join command you copied from the manager’s output.

On worker1, run:

sudo docker swarm join --token <YOUR_WORKER_TOKEN> <MANAGER_IP>:2377

You should see a confirmation message: This node joined a swarm as a worker.

Repeat the same command on your worker2 server.

Security Tip: The join token is a secret. Treat it like a password and do not expose it publicly. If you ever lose the token, you can easily retrieve it by running sudo docker swarm join-token worker on the manager node.

Step 5: Verify the Cluster Status

Your three-node Docker Swarm cluster should now be fully operational. To verify this, return to your manager node and run the following command to list all the nodes in the swarm:

sudo docker node ls

The output will provide a clear overview of your cluster, showing the ID, hostname, status, availability, and role of each node. It should look something like this:

ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
a1b2c3d4e5f6 *                manager    Ready     Active         Leader           24.0.5
g7h8i9j0k1l2                  worker1    Ready     Active                          24.0.5
m3n4o5p6q7r8                  worker2    Ready     Active                          24.0.5

The asterisk (*) indicates the node you are currently on. Seeing a Ready status and Active availability for all nodes confirms that your cluster is successfully configured.

Deploying Your First Service

A cluster isn’t useful without running services. Let’s deploy a simple Nginx web server to see the swarm in action.

On your manager node, create a new service with three replicas. Docker Swarm will automatically distribute these replicas across the available nodes.

sudo docker service create --name my-web-server --replicas 3 -p 8080:80 nginx

Let’s break down this command:

  • --name my-web-server: Assigns a name to our service.
  • --replicas 3: Tells the swarm to maintain three running instances of this container.
  • -p 8080:80: Maps port 8080 on the host to port 80 in the container.
  • nginx: The Docker image to use.

You can check the status of your service with:

sudo docker service ls

This will show that your service has 3/3 replicas running. To see where each replica is deployed, use:

sudo docker service ps my-web-server

One of the most powerful features of Docker Swarm is its built-in routing mesh. This means you can access the Nginx service by visiting the IP address of any node in the cluster on port 8080. For example, open a web browser and navigate to http://<WORKER1_IP>:8080 or http://<MANAGER_IP>:8080. The swarm will automatically route your request to one of the running Nginx containers, regardless of which node it’s on.

Congratulations! You have successfully deployed and configured a scalable, resilient three-node Docker Swarm cluster on Ubuntu 22.04. You can now build on this foundation to deploy complex, multi-service applications with built-in load balancing and high availability.

Source: https://kifarunix.com/how-to-setup-three-node-docker-swarm-cluster-on-ubuntu/

900*80 ad

      1080*80 ad