Intoduction to Docker Swarm
Hello folks. You are gorgeous today as always. This is the last part of our Introduction to docker series. Docker Swarm is relatively advanced topic in the series. It is a tool provided by Docker to manage cluster of Docker host as a single virtual system.
In other words, Docker Swarm enables you to deploy, manage, and scale containerized applications across multiple hosts. It provides features like service discovery, load balancing, scaling, and rolling updates for your containerized applications.
Cluster: In simple, the cluster is a bunch of physical or virtual computer/system that works for the same purpose and acts like a single system.
For absolute beginners; Imagine you have an application runs on a single server and you are using Docker to run it. You have assigned a task that you need to improve performance by increasing server count. It looks easy but everytime you need to do this you will re-do everything from start. Docker Swarm helps you to manage resources, networks and deploy the application in servers.
This looks kind of outdated these days with all the Kubernetes things, cloud providers and their services but back then this was revolutionary. With Docker Swarm and tools like Ansible we could somewhat automate things and ease the process of scaling.
Ansible; is an open source IT automation engine that automates provisioning, configuration management, application deployment, orchestration, and many other IT processes. It is free to use, and the project benefits from the experience and intelligence of its thousands of contributors.
How Docker Swarm Works ?
To be able to work properly, Docker Swarm requires 2 or more machines. One of them must be leader and the others will be the workers. Workers will run the containers we want to launch and the leader will manage the entire swarm environment. Containers on the cluster are deployed using services on Docker Swarm. The services created by us will create a specified amount of replicas of container for the particular image that we specified.
Let's get started!
Docker Swarm Commands
We start by initialize swarm enviorment in leader.
docker swarm init --advertise-addr=<ip_address_of_machine>
Running this command will print a pre-generated command which looks like below and we will use this command to add worker.
docker swarm join --token SWMTKN-1-25ldw5iyn5qavpzjk21j3khk1xasdase1nt85h33gmhemnsaa9k572gwn-e4ami5o5h8ewv0pnc9blifhet x.x.x.x:2377
If you want to remove worker/node from the environment run the following command
docker swarm leave
If you want to list nodes and workers run the following command in leader.
docker node ls
If you want to list services run the following command in leader
docker services ls
If you want to scale a service, run the following command in leader
docker service scale <service_name>=<desired_amount>
And you can remove a service by running following command in leader
docker service rm <service_name>
The End
I am grateful for coming this far and I hope you find this article. As you can see Docker is a simple but powerful tool. You can now use Docker to develop, deploy and scale applications.
Stay tuned for more posts!
You can read my other posts about docker