Running your Applications in the Docker Swarm

Running your Applications in the Docker Swarm

Introduction

Docker Swarm is a container orchestration platform developed by Docker. It is a native clustering and scheduling tool for Docker containers. Swarm enables developers to create and manage a cluster of Docker nodes and deploy containerized applications across the cluster.

Implementation

Firstly, we need to spin up 3 instances and name them accordingly, where one of the instances serves as the manager/master and the others are provisioned as the worker. After the instances are ready, allow the following inbound rules :

Custom TCP - 2377 - Anywhere IPv4

Custom TCP - 8001 - Anywhere IPv4

Now, docker is to be installed in all the 3 instances using the following commands :

sudo apt-get update sudo apt-get install 
ca-certificates 
curl 
gnupg

sudo 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 sudo 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

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

After installing docker in each of the instances, run the following command in the master node to get the key that assigns the other nodes as the workers of this manager node, and a swarm is initialized :

sudo docker swarm init

Now, that the key is being generated, it is therefore used to add the other nodes as workers in the swarm.

You can use the given command to check whether all the nodes are available in the swarm or not :

sudo docker node ls

Finally, the service is created by the manager node and assigned accordingly to the workers available in the swarm. For example, this code snippet can be used to deploy an Nginx service with 3 replicas.

sudo docker service create --name my_web \
                        --replicas 3 \
                        --publish published=8080,target=80 \
                        nginx

Conclusion

Docker Swarm can easily scale your applications horizontally by adding more nodes to the cluster. Swarm also provides load balancing and scheduling features, which ensure that your containerized applications are running efficiently across the cluster.

Overall, Docker Swarm is a powerful and flexible container orchestration tool that makes it easy to deploy and manage containerized applications at scale.