Docker Swarm on Ubuntu

Docker Swarm is a container orchestration tool that enables the deployment, scaling, and management of Docker containers across a cluster of machines

· 4 min read
Docker Swarm on Ubuntu

Docker Swarm is a container orchestration tool that enables the deployment, scaling, and management of Docker containers across a cluster of machines. It is built into the Docker Engine and provides a simple way to create and manage a swarm of Docker nodes, which can be used to run distributed applications with high availability and fault tolerance.

With Docker Swarm, you can define services that consist of one or more containers, and then deploy them to the swarm using a single command. The swarm takes care of scheduling the containers across the available nodes in the cluster, ensuring that they are running on machines with sufficient resources and availability.

Docker Swarm also provides features such as service discovery, load balancing, rolling updates, and secret management, making it easy to build and operate complex distributed applications using Docker containers. Additionally, Docker Swarm is fully compatible with the Docker Compose tool, which allows you to define multi-container applications in a simple and intuitive way.

Installing Docker

These steps need to be done on the 3 machines, Manager and Workers

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

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

sudo apt update

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

sudo systemctl is-enabled docker
sudo systemctl status docker

sudo usermod -aG docker ${USER}
newgrp docker

docker run hello-world

The result shall looks like this :

upload in progress, 0

Creating Docker Swarm

To initialize Docker Swarm, run the docker swarm init command below. The additional parameter --advertise-addr will bind the Docker Swarm within the specific IP address, and the parameter --default-addr-pool determines the internal IP address for containers that running on the Swarm.

On the manager run
docker swarm init --advertise-addr IP_OFYOUR_MANAGER

you'll get a line to join a worker which will look like this

docker swarm join --token SWMTKN-1-30gw5rn2srvvrerp0eg2q9be24e4io5u0revgi0fev50hv87vgjf4zq-8vervi8yfcasgmof38lon7wfv IP_OFYOUR_MANAGER:2377

Run this command all all the workers

To check the swarm status and make sure your workers join the manager successfully run this command

sudo docker node ls

Installing Swarmpit

Swarmpit is an open-source web-based user interface for managing and monitoring Docker Swarm clusters. It provides a graphical interface that allows users to easily deploy, scale, and manage services running on a Swarm cluster, as well as view detailed information about the nodes and containers in the cluster.

Swarmpit is built using modern web technologies such as React and Redux, and it provides a responsive design that works well on desktop and mobile devices. It supports single sign-on authentication using various providers, including GitHub, Google, and others.

Some of the features of Swarmpit include:

  • Service management: create, update, and scale services, view service logs and events, and access the containers in a service
  • Node management: view node information, such as CPU and memory usage, and start, stop, or restart nodes
  • Container management: view container information, including resource usage, network interfaces, and file systems, and start, stop, or restart containers
  • Secret management: create, update, and manage secrets used by services in the cluster
  • Event logs: view detailed event logs for services, nodes, and the Swarm cluster as a whole

Swarmpit is designed to be lightweight and easy to deploy, making it an ideal tool for managing Docker Swarm clusters of all sizes. It can be installed on a single node or scaled out to multiple nodes for high availability and fault tolerance.

GitHub - swarmpit/swarmpit: Lightweight mobile-friendly Docker Swarm management UI
Lightweight mobile-friendly Docker Swarm management UI - swarmpit/swarmpit
docker run -it --rm \
  --name swarmpit-installer \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  swarmpit/install:1.9

and here it is

Next blog will be to put this to use. I have several docker containers on many Virtual Machines to move to this Docker Swarm infrastructure.