Docker Swarm is a tool for clustering and scheduling Docker containers. With Swarm, you can set up and manage a group of Docker nodes as a single virtual system.
In this article, you will create an Nginx service replicated across 3 nodes. The advantage is that you can deploy replicas very easily, allowing you to scale your infrastructure whenever you need to.
For this article, you will need 3 servers running Ubuntu 22.04. One will be the “Manager”, from which you will control replicas (the Manager will also act as a node), and the other 2 servers will be “Workers” (Nodes). You can deploy as many as you need. The idea is to set up the following infrastructure:
Installing and configuring the servers
First, we will update the server and install Docker on all 3 servers.
# apt update && apt upgrade
# apt install apt-transport-https ca-certificates curl gnupg lsb-release -y
# 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
# 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
# apt update
# apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
# systemctl is-enabled docker
# systemctl status dockerConfigure the hosts (nano /etc/hosts) on all 3 servers with their public IP addresses (example):
200.234.228.230 manager01
200.234.230.228 worker01
200.234.232.71 worker02To connect the servers, you will need to open port 2377 in the Clouding firewall on all 3 servers. You can do this by adding the following firewall rule to the profile in use:
Manager configuration
We will only perform this step on the “manager01” server (replace it with your server's public IP address):
200.234.228.230This will return a command that we will run in the next step.
Workers configuration
You will only perform this step on the “worker01” and “worker02” servers. It adds the servers to Swarm (this is the exact line provided by the previous command; copy it directly):
# docker swarm join --token SWMTKN-1-4ptgo95auiw4azer1uy9mw223wcg5s1p39z7qojkeubiyttran-6x1yt4a0fdquxmqomm2dslexi 200.234.228.230:2377If you get a Timeout error, it is most likely due to the server's firewall:
Swarm Visualizer
This step is optional. It allows you to view nodes, containers, services, … from a web interface. You will need to run it on “manager01”:
# apt install unzip
# wget https://github.com/dockersamples/docker-swarm-visualizer/archive/master.zip
# unzip master.zip
# mv docker-swarm-visualizer-master dockersamples
# docker run -it -d -p 5000:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizerTo access the web interface, you will need to open port 5000 in the Clouding firewall on the master server. You can do this by adding the following firewall rule to the profile in use:
Creating an Nginx service with 3 replicas
Now that everything is ready, you can create your Nginx service with 3 replicas:
# docker service create --name mi-web --publish 8080:80 nginx
# docker service scale mi-web=3If you access manager01's public IP address on port 5000 from a browser, you will see Swarm Visualizer, in this case http://46.183.119.183:5000/
You have reached the end of the article. The advantage of using Swarm is that you can have scalable infrastructure: we can create more replicas within the same node or add nodes at any time. In our example, these could be several containers running the Nginx service, which we could load balance by following this article.
You can find more information in the documentation for Docker.
Remember, if you have any questions about this or any other issue related to your servers on Clouding, feel free to write to soporte@clouding.io We're here for whatever you need. Get in touch!