Open In App

How to Use Docker For Fault Tolerance with Docker Replicas?

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. A cluster of docker engines is called a swarm. Docker Swarm is a cluster management and orchestration feature embedded in the Docker Engine.

Docker Replicas

Docker replicas refer to running multiple instances of a docker container to ensure high availability, scalability, and load balancing. Docker replicas allow you to scale your application stack by creating multiple instances of a service.

Docker Swarm

Docker swarm allows the running of a docker container across a cluster of nodes and manages those containers as a single unit.

There are two types of nodes in Docker Swarm:

  1. Manager node: The manager node distributes tasks to worker nodes. 
  2. Worker node: completes the tasks given by the manager node.

task carries a Docker container and the commands to run inside the container.  It is the atomic scheduling unit of the swarm. The swarm manager node constantly checks the state of the cluster and ensures it is always equal to the actual state specified by the user. For example, if a service specifies running 3 replicas of a container and 1 of the containers crashes, the swarm manager node will assign the new replica to one of the available nodes in the cluster.

Docker Service

The main building block of the docker swarm is a service. A docker swarm service is a group of Docker containers managed together. In a service, you specify the image to be used and the commands to run inside the running container. You can also specify other info such as the number of replicas of an image to run, and the port to which the service will be available outside the swarm. Docker swarm breaks the service into one or more tasks based on the replicas value. One task in the docker swarm corresponds to exactly one running container.

Docker Compose

Docker compose is a tool for defining and running multi-container docker applications. In compose you create a YAML file defining all the services that are needed in your application, along with the image, ports, environment variables, network, and volumes associated with each service. For example, you might describe an application comprised of a web front-end service with message queueing services and a database backend.

Enable Docker Swarm

To enable docker swarm in your machine to run multiple instances of container for fault tolerance you need to run the following command:

docker swarm init

swarm_1

The above command will initialize a new docker swarm and now you can deploy multiple containers using docker services.

YAML File creation

Below is a simple docker-compose.yml file:

XML




version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - 80:80
    deploy:
      replicas : 3


The compose file consists of the following information:

  1. version: version of docker-compose file format.
  2. services: specifies services in your application stack.
  3. web: name of the service
  4. image: an image that container will be running.
  5. replicas: number of containers that should be running.
  6. For the above yaml file web service will be broken into three tasks.

Deploy Containers

To deploy the above file in the docker swarm you need to use the below command:

docker stack deploy my-stack -c docker-compose.yml

Now open the docker desktop to see the 3 running containers.

containers

To check whether container is properly running the nginx image or not, open the web browser and paste the below url:

http://localhost:80

You can also see the list of services currently running using the below command:

docker service ls

Docker stack deploy

If any node fails and one of the above three containers stops, the docker swarm will automatically restart it on some other healthy worker node.

Scale

Now if you want to create more containers you can use the below command:

docker service scale my-stack_web=10

This will now create 10 containers running the nginx image.
Scaling the Containers

You can also see the 10 containers running on the docker desktop.

FAQs on Docker Swarm

1. What is a difference between Docker,Swarm, and Kubernetes?

Docker is containerization platform and Docker Swarm and Kubernetes are container  orchestration platforms.

2. What is a load balancer in Docker Swarm?

Load balancer in Docker Swarm distributes the load across the multiple containers.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads