Introduction to Docker Swarm Mode

Docker swarm is a container orchestration tool. Swarm Mode in Docker was introduced in version 1.12 which enables the ability to deploy multiple containers on multiple Docker hosts. For this Docker uses an overlay network for the service discovery and with a built-in load balancer for scaling the services. One of the key advantages of docker swarm is that the configuration of the docker services such as volume and network can be modified without the need to manually restart the service Docker will update the configuration, stop the service tasks with the out-of-date configuration, and create new ones matching the desired configuration.

Table of Content

  • What is Docker and Docker Container?
  • What is Docker Swarm?
  • What are the Task and Service in Docker Swarm?
  • How Does Docker Swarm Work?
  • Docker Swarm Init
  • Docker Swarm Architecture
  • Features of Docker Swarm
  • What is Docker Swarm Used For?
  • Different Modes of Docker Swarm
  • How to Install and Setup Docker Swarm on AWS? A Step-By-Step Guide
  • How to Setup Docker Swarm Mode?
  • How to Create a Task in Docker Swarm Cluster?
  • Getting Started With Swarm Mode
  • What is Stack in Docker Swarm?
  • Docker Swarm Mode Key Concepts
  • Docker Swarm Filters
  • Docker Swarm Mode CLI Commands
  • Docker Container vs Docker Swarm
  • Docker Swarm vs Kubernetes 
  • Docker Service vs Docker Task
  • Benefits of Docker Swarm

What is Docker and Docker Container?

Docker is an open-source container platform that helps in packing an appliation with all its dependencies in on one bundle i.e., Docker Image. On using this docker image assigning the resources such as RAM, CPU executing it as process known as Docker Container. Docker platform make the application containerized and portable to the all systems that supports docker.

What is Docker Swarm?

Docker Swarm is a native clustering and orchestrating tool that helps in managing the a docker engines. In this the group of docker engines turned into as single virtual docker host. It facilitates the users to deploy, manage and scale the applications seamlessly across multiple docker nodes. It comes with providing the features such as service discovery, load balancing, scaling, and rolling updates. It provides an easy and integrated way to manage the containerized applications in a cluster.

To know more about Docker Swarm refer to this – Article

What are the Task and Service in Docker Swarm?

In Docker Swarm, a Service is a higher-level abstraction used to define how containers should be deployed, managed, and scaled across a swarm of Docker nodes. It includes specifications like the number of replicas, networking, and load balancing.

A Task is an individual instance of a container that is created and managed by a service. Each task represents a single unit of work and is scheduled to run on one of the nodes in the swarm. Tasks are the actual running containers that fulfill the requirements specified by the service.

How Does Docker Swarm Work?

When you want to deploy a container in the swarm first, you have to launch services. Service consists of multiple containers of the same image. These services are deployed inside a node so to deploy a swarm at least one node has to be deployed. As you see below diagram the manager node is responsible for the allocation of the task, dispatch the tasks, and schedule the tasks. API in the manager is the medium between the manager node and the worker node to communicate with each other by using the HTTP protocol. The service of one cluster can be used by the other. All the execution of the task is performed by the worker node.

Docker Swarm Init

To initialize the docker swarm cluster we use the command called “docker swarm init”. For converting the docker engine to the swarm manager we will use this command after converting into the swarm mode then you will able the manager and the worker nodes then the swarm will distribute the work across them.

After Initializing The Swarm Mode

  • After initializing the docker swarm init it will bring the swarm into existence.
  • After bringing the swarm into existence know it will turn the current node into a manager node. And also it will generate one token.
  • The token that is generated will be used for the further joining of the worker nodes and master nodes.

Docker Swarm Architecture

There are two types of nodes in Docker Swarm:

  1. Manager node: Carries out and oversees cluster-level duties. 
  2. Worker node: Receives and completes the tasks set by the manager node.

A single manager node can be created but the worker node can not be created without a manager node. The ideal number for the count of the manager node is seven. Increasing the number of the manager node does not mean that the scalability will increase.

Features of Docker Swarm

The following are the features of Docker Swarm:

  1. Cluster management: To create Swarm you can use the Docker engine CLI where you can deploy the applications. Additional orchestration software is not required to manage a swarm.
  2. Multi-host networking: Swarm can contain multiple overlay networks so while deploying the service you can specify the network on which you want to deploy your service. The swarm manager automatically assigns addresses to the containers on the overlay network when it initializes or updates the application.
  3. Load balancing: While deploying any service on a particular port the swarm automatically balances the load of these ports.
  4. Scaling: When you scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state.

What is Docker Swarm Used For?

Docker swarm is a container orchestration tool that is used to Docker containers and scale them. Instead of a single host with the help of Docker Swarm, we can manage multiple nodes which are called clusters where we can deploy and maintain our containers in multiple hosts.

  1. Docker Swarm assures that our application is always available even if one of the nodes fails by creating the container in another node which is available
  2. Based on the incoming traffic we can scale the containers up and down by adding to the multiple nodes.
  3. If there are multiple containers the incoming load will be balanced automatically by the Docker Swarm.
  4. Docker Swarm has a number of security features, including traffic encryption between nodes and mutual TLS authentication.
  5. Docker Swarm will automatically take care of failed containers and nodes. By this, we can maintain high availability.

Simply Docker Swarm is mainly used to deploy, scale, and manage the containers and nodes which are available in the cluster.

Different Modes of Docker Swarm

Docker Swarm mainly consists of two modes they are:

  1. Global Mode: In this mode, Docker Swarm will maintain containers in all the slave nodes and master nodes. It will maintain the replicas of containers in all the nodes which are available in the cluster.
  2. Replicated Mode: In this mode, Docker Swarm will deploy the containers based on the no.of replicas required for you. If you required 3 replicas it will deploy the containers based on the node availability.

How to Install and Setup Docker Swarm on AWS? A Step-By-Step Guide

Step 1: Login to AWS Console

Step 2: Launch the EC2 Instances

  • Launch the 3 EC2 instances with ubuntu AMI (Here we taking 1 docker manager and 2 swarm nodes ).

  • Choosing AMI as ubuntu and the instance type as “t2.micro”

  • Choose the key pair option as No key pair, because here we demonstrating through web console.

Step 3: Specify the inbound Security rules

Step 4: Specify the Metadata to instances to automate the docker setup process to all the nodes at once.

( or )

  • All specify this below code in this metadata.
#!/bin/bash

# Update package lists and install dependencies
sudo apt-get update -y && sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

# Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Set up the stable repository for Docker
sudo add-apt-repository -y \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

# Update the package database with Docker packages from the newly added repo
sudo apt-get update -y

# Install Docker
sudo apt-get install -y docker-ce

# Start Docker service
sudo systemctl start docker

# Enable Docker to start on boot
sudo systemctl enable docker

# Install Docksal
sudo curl -fsSL ger.docksal.io | bash

# Add the current user to the docker group
sudo usermod -aG docker "${USER}"

Step 5: Specify the names to the instances after once they created as shown in the below figure:

How to Setup Docker Swarm Mode?

Step 6: Start Docker Swarm

  • The following command helps to switch to the root user
  • Run the following command to get the token for docker swarm joining.
docker swarm init

Step 7: Joining Docker Swarm Nodes

  • Copy the token from the above step and run in the docker swarm node 1 to join into the docker swarm cluster.

  • Copy the token from the above step and run in the docker swarm node 2 to join into the docker swarm cluster.

How to Create a Task in Docker Swarm Cluster?

Step 8: Create a Task in Docker Swarm

Run the following command in docker swarm manager to create a task in the swarm cluster:

docker service create --name myjavawebapp -p 8080:8080 bnvschaitanya/java-web-app

Step 9: Create a Replica to Task

  • Scale the created service from default 1 to 4 with the following command:
docker service scale myjavawebapp=4

Step 10: Access the application through browser using any of your cluster instance ip with 8080 port as follows:

http://65.1.3.176:8080

Getting Started With Swarm Mode

Follow the steps mentioned below to get familiar with the docker swarm mode.

Step 1: In the first step we need to initialize the docker engines of swarm mode.

Step 2: After initializing the swarm mode know add the nodes into the docker swarm cluster. When you intilize the swarm mode it will generates two tokens one is for the manger node and another is for the worker node by using following command you can join the nodes according to the requirement.

docker swarm init <Token> 

Step 3: Know you can start deploying the you application in the form of containers in docker swarm. Docker swarm will take care of deployment of application an scaling of the application across the worker nodes.

Step 4: WIth the help od docker swarm cli you can manage the swarm like adding or removing the worker nodes scaling the services and inspecting the swarm state.

What is Stack in Docker Swarm?

A stack is nothing but a collection of one or more services deployed as a single unit. The stack is deployed by using compose file in which you can mention the service of the stack and all the required configurations to deploy the stack.

With the help of stack, it is very easy to deploy and maintain complex containers like multi-containers in the Docker swarm. We can deploy with the help of a single file called Docker Compose. yaml we can define all the necessary configurations. You can be assured that they deployed and scaled together.

If we deploy the new version of the application and the end users find any bugs while using it you can roll back to the previous version of the application by using Docker Swarm.

Note: Stack is mainly used to deploy the multi-container application with ease.

Docker Swarm Mode Key Concepts

The following are the docker swarm mode key concepts:

  • Node: A Node is an instance of a Docker engine that connects to the Swarm. You can run one or more nodes on a single physical computer or cloud server. Nodes can be either a manager or workers. The manager node dispatches units of work called tasks to worker nodes. Worker nodes receive and execute tasks dispatched from manager nodes.
  • Services: A service is a high-level concept relating to a collection of tasks to be executed by workers. An example of a service is an HTTP Server running as a Docker Container on three nodes.
  • Load Balancing: Docker includes a load balancer to process requests across all containers in the service.

Docker Swarm Filters

The following are the docker swarm filters:

  • Constraints: Based on conditions, users are restricted from creating containers on particular Docker hosts.
  • Drain Node: If we applied Drain Nodes on any node then Docker swarm will not assign any replicas to that node. 
  • Port: Avoids the port conflicts between the application by deploying the same port applications in two different nodes.

Docker Swarm Mode CLI Commands

The following are the docker swarm mode CLI commands:

  • docker swarm init: This command is used to initialize the swarm.      
docker swarm init [OPTIONS]
  • docker swarm join: By using this command you can join a node to a swarm. The node joins as a manager node or worker node based on the token you pass with the –token flag.
docker swarm join [OPTIONS] HOST:PORT
  • docker service creates: This is a cluster management command, and must be executed on a Swarm manager node.
docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
  • docker service inspects: This command is used to inspect the particular service and all the details will display in JSON format.
docker service inspect [OPTIONS] SERVICE [SERVICE...]
  • docker service ls: This command is used to see the complete list of all the services in that network.
docker service ls [OPTIONS]
  • docker service rm: This command is used to remove the specific service you want to remove.
docker service rm SERVICE [SERVICE...]

Docker Container vs Docker Swarm

The following are the difference between Docker Container and Docker Swarm:

Docker Container Docker Swarm

Docker Container is an executable package that consists of all the code and dependencies.

Docker Swarm is the container orchestration tool that manages all the containers available in the Swarm cluster.

The container can be run on any OS as Docker is an underlying runtime. Docker Swarm will manage the cluster which consists of Docker nodes.

A single, isolated, and self-contained unit called a “Docker container” is capable of running an application.

You may manage many Docker containers across various servers in a cluster using the technology known as Docker Swarm.

Docker containers are more suitable for microservices applications than monolithic applications.

Docker Swarm will scale, deploy and balance the containers.

Docker Swarm vs Kubernetes

The following are the docker swarm and Kubernetes:

  Kubernetes

   Docker Swarm 

Kubernetes is an open-source platform used for maintaining and deploying a group of containers Docker Swarm is designed to be a simple and lightweight container orchestration.
In practice, Kubernetes is most commonly used alongside Docker for better control and implantation of containerized applications. With Docker swarm, multiple containers run on the same hardware much more efficiently than the VM environment & productivity of Docker is extremely high. 
Applications are deployed as a combination of pods, Deployment, and services.  Apps are deployed in the form of services.
It supports auto-scaling of the container in a cluster. With more efficient  Docker Swarm does support auto-scaling but is not as efficient as docker swarm.
The health check is of two kinds: liveness and readiness. Health checks are limited to service.
Hard to set up and configure. Docker swarm setup and installation are easy.

It does not have extensive documentation but is quite less than Docker Swarm. But it does include everything from installation to deployment. 

Docker swarm documentation is more effective, more extensive, and has even more capabilities & it includes everything from installation to deployment & quick-start instructions as well as a more detailed tutorial. 

Kubernetes installation is provided to be quite difficult than Docker swarm and even the command for Kubernetes is quite more complex than Docker swarm.

Docker swarm installation is quite easier, by using fewer commands you can install Docker in your virtual machine or even on the cloud.

Azure, buffer, intel, Evernote, and Shopify Using Kubernetes.  Citizens Bank, and MetLife companies using Docker swarm. 

Docker Service vs Docker Task

The following are the differences between docker service and docker task:

Aspect Docker Service Docker Task
Definition A higher-level abstraction that defines how to run containers as a group An individual instance of a container created by a service
Purpose Manages the deployment and scaling of a group of containers Executes a single unit of work within a service
Management Handles orchestration, load balancing, and scaling Represents the actual work being performed by a container
Scalability Easily scaled up or down to manage load Cannot be individually scaled; scaling is done at the service level
State Desired state defined (e.g., number of replicas) Current state of a specific container instance
Failure Handling Automatically reschedules tasks if a container fails Does not manage failures; relies on service for rescheduling

Benefits of Docker Swarm

The following are the benefits of Docker Swarm:

  1. Simplified Setup and Management: Docker Swarm provides an easy-to-use and integrated toolset for orchestrating containers, making it straightforward to set up and manage a cluster of Docker nodes.
  2. Scalability: Docker Swarm allows seamless scaling of services up or down with simple commands, enabling dynamic adjustment of resources based on demand.
  3. High Availability: Swarm mode ensures that services are replicated across multiple nodes, providing fault tolerance and resilience by automatically redistributing tasks in case of node failures.
  4. Integrated Load Balancing: Docker Swarm includes built-in load balancing to distribute network traffic across multiple containers, ensuring optimal performance and resource utilization.

Docker Swarm Node – FAQs

When should I Use Docker Swarm?

Docker Swarm used be for simple, native Docker Orchestration purpose. It facilitates with easy setup and management for container clustering.

What is Swarm mode in docker?

The Swarm mode in Docker is a native clustering and orchestrating feature that enables the deployment, management and scaling of the containers across a cluster of docker hosts.

Why docker swarm mode is deprecated?

The Docker Swarm mode is deprecated because of kuberentes and it has become the more widely adopted and strong container orchestration platform leading to shift in focus and support.

What is docker swarm mode ingress?

Docker Swarm mode ingres is a built-in load balancing features that routes the incoming traffic to the appropriate service across the swarm cluster.

Is Docker Swarm Used in Production?

Yes Docker Swarm is used in the production environments but it is not much used as the kubernetes in the production environment. Kubernetes had more advantages then the docker swarm.

Is Docker Swarm Free?

Yes Docker swarm is free to use. You can run the docker swarm on as many as docker engine instances as you want without incurring any licensing costs.

What Ports are Used by Docker Swarm?

  • TCP Port for the manager node: 2377
  • Worker node TCP port: 2376

Is Docker Swarm a Load Balancer?

Docker swarm itself isn’t a full-fledged load balancer but you can achieve this by using two mechanisms.



    Contact Us