Install and Configure Docker Swarm on Ubuntu 20.04 LTS
Introduction
Docker Swarm is a container orchestration tool. That is, it allows for creation of a cluster of docker hosts and turns them into a single virtual server. This enables your application running on containers achieve high performance and high availability by distributing it between available hosts within the cluster. You will learn how to install and configure docker swarm in this article.
Prerequisites
- Deploy two or more fully updated Ubuntu 20.04 LTS at Vultr with at least 2GB of RAM and 1 vCPU cores.
- Create a non-root user with sudo access on both servers.
1. Configure Cluster Hosts
For whichever number of servers deployed, one will be the manager node and the rest worker nodes. Manager node handles all cluster management tasks while the worker nodes run the containers. In this article, we will use two nodes, for example:
192.0.2.11 manager
192.0.2.12 worker-1
SSH to all Ubuntu server as a non-root user with sudo access.
Edit host file
/etc/hosts
in all the nodes.$ sudo nano /etc/hosts
Add the following code in the file.
192.0.2.11 manager 192.0.2.12 worker-1
Ping all the nodes using their hostnames.
$ ping -c 4 manager $ ping -c 4 worker-1
2. Install Docker CE
Install Docker CE on all the nodes. Perform all stages in this step on all nodes.
Update the system packages.
$ sudo apt update
Install all required packages.
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Install Docker repository signing keys.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Update the system packages.
$ sudo apt update
Install Docker CE.
$ sudo apt install docker-ce -y
Confirm the status of Docker.
$ sudo systemctl status docker
Enable the Docker service to automatically run at system boot.
$ sudo systemctl enable docker
Add your current user to
docker
group.$ sudo usermod -aG docker ${USER}
3. Create Docker Swarm Cluster
To create a Docker Swarm cluster, you will first need to initialize the swarm mode on the manager node. Then, join the worker nodes to the cluster. Strictly use the nodes IP address.
Initialize the swarm mode.
$ sudo docker swarm init --advertise-addr 192.0.2.11
Go to the worker-1 node and add it to the cluster. Modify the
--token
value with your own.$ sudo docker swarm join --token SWMTKN-1-2jxta71638d1pyioznb9jo4hi4u5ppd8t7lc90linwi9acu54s-aef4mqdy23ktrkcxsp57uyoma 192.0.2.11:2377
Go to the manager node and verify if all the worker nodes successfully joined the cluster.
$ sudo docker node ls
4. Deploy Application to the Cluster
Go to the manager node and create a 'Docker getting started web-page' service named
docker-tutorial
that will run on default http port 80 and expose it to port 80 on the host server.$ sudo docker service create --name docker-tutorial --publish 80:80 docker/getting-started
Verify the status of the created service.
$ sudo docker service ls
5. Create Replicas of the Service
With two nodes in our cluster, we will make two replicas of the service. This will enable access to the service form both the manager and worker nodes.
Create replicas.
$ sudo docker service scale docker-tutorial=2
Verify the status of the service replicas.
$ sudo docker service ls
Go to your browser and access the service from all your nodes. For example:
manager node.
http://192.0.2.11
worker-1 node.
http://192.0.2.12
Conclusion
You have successfully installed and configured Docker Swarm. You can now scale your application by adding more worker nodes to the cluster and creating more replicas of the services you have running.