Day 21 - Docker Swarm

Day 21 - Docker Swarm

What is Docker Swarm?

With Docker Swarm, we can manage multiple docker machines together into a single cluster. It will take care of distributing our services or our application instances into separate hosts for high availability and for load balancing across different system and hardware.

If our container got crash, then it cannot be get revived anyway. So, Container Orchestration is a solution for this.

A Container Orchestration solution consists of multiple docker hosts that can host containers in that way even if one fails, the application is still accessible through the others.

Docker Swarm is an orchestration management tool that runs on Docker applications. It helps end-users in creating and deploying a cluster of Docker nodes.

Docker swarm uses master-slave architecture. One will be a Swarm Manager and others will be Workers/Slaves.

docker-daemon

Features of Docker Swarm

Some of the features of Docker Swarm are:

  1. Auto-Scaling: For each service, you can declare the number of tasks you want to run. When you scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state.

  2. Resource Allocation: The swarm manager node constantly monitors the cluster state and reconciles any differences between the actual state and your expressed desired state.

  3. Decentralized access: Swarm makes it very easy for teams to access and manage the environment.

  4. Load Balancing: You can expose the ports for services to an external load balancer. Internally, the swarm lets you specify how to distribute service containers between nodes.

  5. Roll-back a task: Swarm allows you to roll back environments to previous safe environments.

Components of Docker Swarm

  1. Services: Defines task that needs to be executed on the manager node and worker node.

  2. Task: They are the docker container that executes the commands.

  3. Manager Node: Takes the commands to create services, allocate IPs and assign tasks.

  4. Worker Node: Perform the tasks of running a container.

How Does Docker Swarm Work?

In Swarm, containers are launched using services. A service is a group of containers of the same image that enables the scaling of applications. Before you can deploy a service in Docker Swarm, you must have at least one node deployed.

There are two types of nodes in Docker Swarm:

  1. Manager node which maintains cluster management tasks.

  2. Worker node which receives and executes tasks from the manager node

Consider a situation where a manager node sends out commands to different worker nodes.

The manager node knows the status of the worker nodes in a cluster, and the worker nodes accept tasks sent from the manager node. Every worker node has an agent that reports on the state of the node's tasks to the manager. This way, the manager node can maintain the desired state of the cluster.

The worker nodes communicate with the manager node using API over HTTP.

In Docker Swarm, services can be deployed and accessed by any node of the same cluster.

Docker Swarm Commands

sudo docker swarm init -> To initialize a swarm in manager node.
sudo docker info -> Gives the info about the docker engine.
sudo docker node ls -> List of docker swarm nodes.
sudo docker service create --name <servicename> <imagename> -> Create a service on manager node.
sudo docker service ls -> List of running services.
sudo docker service rm <servicename> -> To remove a service
sudo docker swarm join --token <token> -> To join worker node to a manager node.

NOTE: Token will be generated when we initialize the swarm on manager node.
docker swarm leave -> To leave a swarm.
docker swarm join-token worker -> To generate a token from manager node.
docker service scale <servicename>-<replicas> -> To scale up swarm with replicas

Conclusion

Docker Swarm is a container orchestration tool that allows for the deployment, management, and scaling of containerized applications.

It enables the creation of a swarm of Docker nodes, turning them into a single virtual system, where containers can be easily distributed and managed.

Docker Swarm provides a simple and efficient way to orchestrate containers, offering features like service discovery, load balancing, and fault tolerance. While it may not be as feature-rich as some other orchestration tools, its ease of use and integration with Docker make it a viable choice for smaller-scale container deployments.

Overall, Docker Swarm is a lightweight and user-friendly solution for container orchestration, providing a practical option for teams looking to manage containerized applications in a straightforward manner.

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌