Docker Swarm on Ubuntu 18.04
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 &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot; apt update apt-get install -y docker-ce systemctl start docker systemctl enable docker
We will set up the hosts on the three servers with:
18.104.22.168 manager01 22.214.171.124 worker01 126.96.36.199 worker02
Setup for Manager
This step will only be executed on the server “manager01”:
docker swarm init --advertise-addr 188.8.131.52 [/ php] It will return a command, which is the one we will execute on the next step. <a href="https://clouding.io/kb/wp-content/uploads/2018/08/KB-Swarm-01.png"><img class="aligncenter size-large wp-image-9645" src="https://clouding.io/kb/wp-content/uploads/2018/08/KB-Swarm-01-1024x113.png" alt="" width="900" height="99" /></a> <h3>Setup for Workers</h3> This step will only be executed in the servers "worker01" and "worker02", it is useful to add the servers to Swarm: <pre>docker swarm join --token SWMTKN-1-3dkep05rmz11kjqa3g4u9bil8s4kf69wrba8smtvmqnl74pajj-0ln4m6n43vgkx7flgxh7x9z4z 184.108.40.206: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 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 my-web –publish 8080: 80 nginx docker service scale my-web = 3 [/ php]
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 following this article.
Have you tried it? Leave us your comments! 🙂