Docker is a go-to choice for application deployment for ALL the distributed systems. But, managing the application after deployment is as essential as deploying it. Docker Swarm is one such management tool developed by Docker Inc. In this blog, I will not only explain what is Docker Swarm, but I will also walk you through the topics mentioned below.
Table of Contents:
Let me give a quick introduction about Docker to get everybody on the same page.
What are Docker and Docker Container?
Docker is an open-source tool used to automate application deployment in the form of lightweight containers. And Docker containers are not only lightweight but also make the application platform-independent. So, the application that runs on your computer will run the same way on your friend’s computer!
The Docker Containers overcome a lot of problems faced by the virtual machines. Hence, the Docker Container wins the game of Docker Container vs. Virtual Machines any day! To enumerate, Docker Containers are faster, portable, provide isolation, use less memory, etc.
But just the Docker containers cannot do everything independently, and that is where an orchestrator comes in. So, let’s check out how Docker Swarm helps better management of Docker containers.
What is Docker Swarm?
Docker Swarm is native to the Docker platform. The Docker Swarm is integrated into Docker itself, and the Docker Swarm is installed when you install Docker.
Docker Swarm is a small-scale management tool that provides a good orchestration system for Dockerized applications. Docker Swarm turns a pool of Docker hosts into a single virtual server allowing clustering with the built-in Swarm orchestration. In essence, we can utilize the Docker Swarm model to efficiently deploy, manage, and scale a cluster of nodes on Docker.
Features of Docker Swarm
Have a look at some of the prominent features of the Docker Swam mode, and why do we need it:
- Simple and Fast: The Swarm mode offers simplicity and speed for development and deployment.
- Decentralized access: Swarm makes it pretty easy for organizations to enter and manage the environment
- High security: The communication between the manager and a worker nodes is highly secure within the swarm
- Autoload balancing: We also have an autoload balancing option within your environment, and you can write your own script for this.
- High scalability: With load balancing comes high scalability.
- Roll-back: It also allows you to roll back environments to previous safe environments
Docker Swarm Mode Concepts
So, whenever I refer to ‘Docker Swarm’, I was actually referring to Docker Swarm Mode but not the deprecated Docker Swarm. Let’s look at some of the key concepts of swarm mode:
Service and Tasks
We use services to launch our Docker containers. And they can be deployed in either global or replicated ways.
- Global services are used to monitor containers that want to run on a swarm node.
- Whereas a Replicated service is to specify the number of similar tasks that we require on a host machine.
- Scaling becomes effortless with Docker Swarm mode.
- Services can be accessed by any of the nodes in the same cluster.
- A service is a description of a task, whereas a task performs the work.
- A developer creates a service with Docker, which can start tasks. However, when a task is allotted to a node, the same task can’t be assigned to another node.
Node
- A node in Swarm mode is an instance of the Docker engine.
- It is possible to run multiple nodes on a single server. But in production, nodes are spread across various devices.
The architecture and Working of Docker Swarm Mode
The idea of how you make use of Docker Swarm is simple: you declare the applications as stacks of services and let Docker handle the rest. The key components of a Docker Swarm are Docker Nodes, Docker Services, and Docker Tasks.
There are two kinds of Docker Nodes, the Manager Node, and the Worker Node. And a Swarm consists of at least one node, be it physical or virtual machines running Docker version 1.12 or later.
Manager Node
The manager node, as the name suggests, is responsible for the cluster management tasks:
- To maintain the cluster state
- Scheduling of Services
- And serving Swarm mode HTTP API endpoints.
But, for persistence, we use something called Raft implementation. The managers maintain a consistent internal state of services and the whole Swarm cluster itself. The best practice in production/non-testing purposes we should implement more than one manager node.
Worker Node
The Worker nodes are nothing but the instances of Docker Engine which are created to run Docker Containers. They are not involved in managing tasks. A manager node can exist without a worker node; a worker cannot exist without a manager. A manager node is also a worker node by default.
The worker nodes talk to the manager node via API over HTTP. As said earlier, services can be deployed and accessed by any node of the same cluster. So, while creating a service, you’ll have to specify the container image. You can set up commands and services to either global or replicated: a global service will run on every Swarm node, and on a replicated service, the manager node assigns tasks to worker nodes.
Now comes the most interesting section. I will show you how to create a cluster, upgrade nodes, start a service and, much more!
Demo on Docker Swarm for Beginners
Now comes the most interesting part, hands-on with Docker Swarm! I will show you how to set up a Swarm cluster with 1 manager node and 2 worker nodes. Check out the prerequisite to get started:
- Three Ubuntu (or any other Linux distros) virtual machines or
- Three Amazon AWS ec2 instances (I will be using ec2)
Know how to create a free AWS account. - Interest to try out new things 😇
Step 1: Install Docker
Install Docker on all three instances or VMs with the following command
curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh
You can check if it is installed by the below command:
docker --version
Step 2: Create a Swarm Manager
We will have to create a manager to handle our worker nodes. And, you should also know that the manager is also a worker node with some special privileges.
docker swarm init --advertise-addr [private or public ip address]
Use private IP addresses if all the instances/VMs are in the same network, if not you should be using the pubic IP instead.
Note: Make sure you copy the above-selected command which will be required in the next step.
Step 3: Add Worker Nodes
The next step is to join our two worker nodes to the Swarm cluster by using the token which was generated earlier.
docker swarm join --token <token-id> <ip:port>
Worker 1:
Worker 2:
Step 5: Check the Nodes
Now, let us check the status of our Swarm cluster. Perform the command below.
docker node ls
Hurray! Mission accomplished. We have successfully created a Swarm cluster with 1 Manager node and 2 Worker Nodes. Wait no more and start deploying your applications on the swarm cluster.
What is the Difference Between Kubernetes and Docker Swarm?
Kubernetes and Docker Swarm are the two most used open-source platforms, that have similar features. But, on closer review, various fundamental differences can be seen between how these two functions. Refer to the table below for a better picture.
Feature | Kubernetes | Docker Swarm |
Application Deployment | Applications can be deployed in K8s using innumerable microservices, deployments, and pods. | Applications can be used only as microservices in a swarm cluster. |
Scalability | Kubernetes acts like more of an all-in-one framework when working with distributed systems. It is thus significantly more complicated as it provides strong guarantees in terms of the cluster state and a unified set of APIs. Container scaling and deployment are therefore slowed down. | Docker Swarm can deploy containers much faster than Kubernetes, which allows faster reaction times for scaling on demand. |
Container Setup | By utilizing its own YAML, API, and client definitions, Kubernetes differs from other standard docker equivalents. Thus, Docker Compose or Docker CLI cannot be used to define containers. Also, YAML definitions and commands must be rewritten when switching platforms. | The Docker Swarm API offers much of the familiar functionality from Docker, supporting most of the tools that run with Docker. However, Swarm cannot be utilized if the Docker API is deficient in a particular operation. |
Networking | Kubernetes has a flat network model, allowing all the pods to communicate with each other. Network policies are in place to define how the pods interact with one another. The network is implemented typically as an overlay, requiring two CIDRS for the services and the pods. | When a node joins a swarm cluster, it creates an overlay network for services for each host in the docker swarm. It also creates a host-only docker bridge network for containers. This gives users a choice while encrypting the container data traffic to create its own overlay network. |
Availability | Kubernetes offers significantly high availability as it distributes all the pods among the nodes. This is achieved by absorbing the failure of an application. Unhealthy pods are detected by load balancing services, which subsequently deactivate them. | Docker also offers high availability architecture since all the services can be cloned in Swarm nodes. The Swarm manager Nodes manage the worker’s node resources and the whole cluster. |
Load Balancing | In Kubernetes, pods are exposed via service, allowing them to be implemented as a load balancer inside a cluster. An ingress is generally used for load balancing. | Swarm Mode comes with a DNS element that can be used for distributing incoming requests to a service name. Thus services can be assigned automatically or function on ports that are pre-specified by the user. |
Logging and Monitoring | It includes built-in tools for managing both processes. | It does not require using any tools for logging and monitoring. |
GUI | Kubernetes has detailed dashboards to allow even non-technical users to control the clusters effectively. | ocker Swarm, alternatively, requires a third-party tool such as Portainer.io to manage the UI conveniently. |
FAQs
Ques. Is docker swarm going away?
Ans. Docker Swarm Mode is still alive and included in docker-ce. But there is no as-a-service provider for docker swarm mode anymore.
Ques. At what point do I need Kubernetes?
Ans. If you have transitioned or are looking to transition to a microservice architecture then Kubernetes will suit you well because it’s likely you’re already using software like Docker to containerize your application. If you’re unable to meet customer demands due to slow development time, then Kubernetes might help.
Ques. Is the Docker swarm suitable for production?
Ans. Running the Docker Engine in swarm mode has proven success with production workloads.
Ques, Should I learn Docker swarm or Kubernetes?
Ans. I would suggest you first learn Docker rather than skipping to Kubernetes, There is confusion related to Docker swarm and its similarities with Kubernetes. Kubernetes is providing an ecosystem for shipping Docker containers.
Related Posts
- Docker Tutorial | Full Course | Beginner to Advanced
- How To Setup A Three Node Kubernetes Cluster For CKA: Step By Step
- Check out and Subscribe to our YouTube channel on “Docker & Kubernetes.”
- Certified Kubernetes Administrator (CKA) Certification Exam
- Certified Kubernetes Administrator (CKA) Certification: Step By Step Activity Guides
Join FREE Class
Begin your journey towards becoming a Certified Kubernetes Administrator [CKA] from our Certified Kubernetes Administrator (CKA) training program. To know about the Roles and Responsibilities of a Kubernetes administrator, why learn Docker and Kubernetes, Job opportunities for Kubernetes administrator in the market. Also, know about Hands-On labs you must perform to clear the Certified Kubernetes Administrator (CKA) Certification exam by registering for our FREE class.
Leave a Reply