How to deploy a Docker Swarm on Ubuntu 22.04

How to deploy a Docker Swarm on Ubuntu 22.04

Jack Wallen shows you how to create a cluster of Docker servers, called a Swarm, on Ubuntu Server 22.04.

Programming code abstract technology background of software developer and Computer script
Image: monsitj/Adobe Stock

Docker is my go-to container deployment runtime engine. With it, I can quickly deploy containers to a network that are easily accessible. What happens when I want to be able to scale those applications? Doing so on a single Docker server would be a challenge. To that end, you deploy multiple Docker instances and cluster them together. That, my friends, is called a Docker Swarm.

SEE: Hiring kit: Back-end Developer (TechRepublic Premium)

Let me show you how to deploy a Docker Swarm on my de facto standard server distribution, Ubuntu. There are quite a few moving parts involved, so let’s get to it.

What you’ll need to deploy a Docker Swarm

I’m going to demonstrate with a three-node cluster (one controller and two nodes). For that, you’ll need three instances of Ubuntu Server 22.04. You’ll also need a user with sudo privileges.

How to install the latest version of Docker

You’ll want to do this on every node for your Swarm.

First, add the Docker GPG key with the command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, add the official Docker repository:

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

We’ll next install a few dependencies with the command:

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

Finally, we can install the latest version of the Docker engine:

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

To finish things up, add your user to the docker group with the command:

sudo usermod -aG docker $USER

Log out and log back in for the changes to take effect.

How to change the hostname on your servers

Let’s change the host names of your Docker Swarm servers. This will be done on all nodes. We’ll configure them as docker1, docker2 and docker3, but you can name them whatever you like.

To set the hostname, the command is:

sudo hostnamectl set-hostname HOSTNAME

Where HOSTNAME is the name of the host for the machine.

How to map the hosts file

This is also done on every node. Open the hosts file with the command:

sudo nano /etc/hosts

At the bottom of the file, you’ll map each host like so (changing the entries to match your hostnames and IP addresses):

192.168.1.60 docker1
192.168.1.61 docker2
192.168.1.62 docker3

Save and close the file.

How to initialize the Swarm

Go to the controller node (docker1) and initialize the Swarm with the command, making sure to edit the IP address to match your needs:

docker swarm init --advertise-addr 192.168.1.60

You should then be presented with a join command that looks something like this:

docker swarm join --token SWMTKN-1-05rgkgq9hgvas7wfglzrumxymzxw3downs1afcbdr9kc7hq4cm-8ku8kxjsq57l1xnkl5lzjppro 192.168.1.60:2377

Run that command on each of your nodes. Once they’ve joined, you can verify the Swarm on the controller node with the command:

docker node ls

You should see something like this in the output:

tpsl7enzswhkeef3dh8uswkxp *   docker1    Ready     Active         Leader     20.10.17
xnye548afhe1hc832kulh5sui      docker2    Ready     Active                          20.10.17
cammaze2fcfcomjpdo0fwz105   docker3    Ready     Active                          20.10.17

Congratulations, your Docker Swarm is ready for your deployments. You can keep adding new nodes as needed for even more scaling and failover capability.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Source of Article