Docker Swarm is a Docker container grouping and programming tool, which is a cluster. With Swarm we can establish and manage a group of Docker nodes as a single virtual system. In this article we are going to create a Nginx service replicated in 3 nodes. The advantage is that we can deploy replicas very easily, with which we can scale our infrastructure when we need it.
For this article we will need three servers with Ubuntu 18.04, one will be the “Manager” from which we will control replicas (the Manager in turn will be node) and the other 2 servers will be “Workers” (Nodes). We can deploy as many as we need. The idea is to assemble the following infrastructure:
Servers installation and setup
The first thing we’ll do is update the server and install Docker on all three 3 servers.
# apt update
# apt upgrade
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# apt update
# apt-get install -y docker-ce
# systemctl start docker
# systemctl enable docker
We will set up the hosts (vi /etc/hosts) on the three servers with:
Configuración para Manager
This step will only be executed on the server “manager01”:
# docker swarm init --advertise-addr 126.96.36.199
It will return a command, which is the one we will execute on the next step.
Configuración para Workers
This step will only be executed in the servers "worker01" and "worker02", it is useful to add the servers to Swarm:
# docker swarm join --token SWMTKN-1-3dkep05rmz11kjqa3g4u9bil8s4kf69wrba8smtvmqnl74pajj-0ln4m6n43vgkx7flgxh7x9z4z 188.8.131.52:2377
This step is optional, it will allow us to see our nodes, containers, services, etc. from our web interface. We will execute it in “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/visualizer
We create an Nginx service with three replicas
Now that we have everything ready, we will create our Nginx service with three replicas:
# docker service create --name mi-web --publish 8080:80 nginx
# docker service scale mi-web=3
If we enter the public IP of manager01 through port 5000 from a browser we will see Swarm Visualizer,
in our case http://184.108.40.206:5000/
We have now reached the end of the article, the advantage of using Swarm is that we can have a scalable infrastructure, we can create more replicas within a single node or add nodes at any time. In our example there could be several containers with the Nginx service that we could balance with HAProxy for example.
Have you tried it? Leave us your comments! 🙂