Kiến Thức Linux Tutorial

How to Install Docker Swarm on Ubuntu 22.04

Docker Swarm

Docker Swarm is a native clustering and orchestration tool integrated into Docker Engine. It allows you to manage a group of Docker nodes as a single virtual system, simplifying the deployment and scaling of containerized applications.

This guide will walk you through installing Docker Swarm across Ubuntu 22.04 servers.


What You Need

Before you begin, ensure the following:

  • Three Ubuntu 22.04 servers: one for the Swarm Manager and two as Worker Nodes.
  • A non-root user with sudo privileges on each server.

Initial Setup

Start by configuring the environment on all servers before installing Docker or enabling Swarm features.

Open Required Ports

Docker Swarm relies on specific network ports for node communication and service traffic. These can be opened using UFW, the default firewall tool on Ubuntu.

Allow SSH access and activate UFW:

sudo ufw allow OpenSSH
sudo ufw enable

Next, open a range of ports (30000–35000) for services:

sudo ufw allow 30000:35000/tcp

Open the essential ports used by Docker Swarm:

for ports in 2377/tcp 7946/tcp 7946/udp 4789/udp
do
sudo ufw allow $ports
done

Finally, apply the changes and confirm the firewall status:

sudo ufw reload
sudo ufw status

Add the Docker Repository

Now, configure your servers to install Docker from the official Docker repository.

Install essential dependencies:

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

Add Docker’s GPG key and repository:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo
  "deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu
  \"$(. /etc/os-release && echo \"$VERSION_CODENAME\")\" stable" |
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update the package index:

sudo apt update

Install Docker Engine

Now you can install Docker Engine on all servers:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Once installed, verify Docker is enabled and running:

sudo systemctl is-enabled docker
sudo systemctl status docker

(Optional) Allow Docker Commands for Non-Root Users

To manage Docker as a non-root user, add your user to the Docker group:

sudo usermod -aG docker username

Switch to the user and test with a sample container:

su - username
docker run hello-world

Initialize Docker Swarm

Use one server as the Swarm Manager to initialize the cluster.

Set up Swarm mode:

docker swarm init --advertise-addr 192.168.5.30 --default-addr-pool 10.20.0.0/16

Check the Swarm status:

docker info

View current nodes:

docker node ls

Add Worker Nodes

To add a worker, generate a token:

docker swarm join-token worker

Run the provided join command on each worker node:

docker swarm join --token SWMTKN-... 192.168.5.30:2377

Back on the manager node, verify the full node list:

docker node ls

Deploy a Service to the Swarm

Create a test service (e.g., using Nginx):

docker service create --replicas 1 --name test-nginx -p 30001:80 nginx:alpine

View service details:

docker service inspect test-nginx
docker service inspect --pretty test-nginx

Check service status:

docker service ls
docker service ps test-nginx

Test service access:

curl 192.168.5.30:30001
curl -I 192.168.5.30:30001

Scale the Service

Scale the service across nodes:

docker service scale test-nginx=3

Check the service on all nodes:

docker service ps test-nginx

From each worker node:

docker ps
curl 192.168.5.31:30001

Clean Up

To remove the service and image:

docker service rm test-nginx
docker service ps
docker rmi nginx:alpine
docker images

Conclusion

You’ve successfully set up Docker Swarm on Ubuntu 22.04, deployed a sample service, scaled it, and performed basic clean-up. This foundational knowledge prepares you for managing scalable, containerized applications with Docker Swarm.

Add Comment

Click here to post a comment