Docker Swarm vs Kubernetes
Tags: docker, kubernetes, devops, opensource
Published: 2024-03-30 14:14:08

Automating the deployment pipeline is becoming increasingly important in today's fast-evolving world, where applications are broken down into microservices. This is where Docker and Kubernetes come into play.

Before we dive deep into Docker and Kubernetes, let's first understand what containers are.

---

## What are containers?

A container packages the application code and its dependencies in one unit, which then can be executed on any infrastructure. It's lightweight and includes everything required to run the app, such as code, runtime environment, system libraries, and external dependencies. The core aim is to quickly deploy the application, which executes the same regardless of the underlying architecture.

Containers eliminate the "works on my machine" 🤣 phrase.

![Huge production bug, but developer says works on my machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/796dxmxxszvkewe7ykqa.jpg)

---

Now that we understand containers let's move on to Docker Swarm.

## What is Docker Swarm?

Docker Swarm is a container orchestration tool that allows you to manage and deploy applications using containerization. It's an open -source and popular as it is quick to set up and easy to use. Docker Swarm is called `swarm mode` and is a native Docker mode. `Swarm mode` allows DevOps engineers to create a cluster of Docker nodes and deploy services to run on these nodes. This cluster is called Swarm.

A `swarm` cluster consists of manager nodes, which manage the cluster and worker nodes, which are instructed to run tasks by the manager nodes.

### Swarm Architecture


<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0xo6rkw7gjd17d7h3q7w.jpg" alt="Swarm Architecture">
  <figcaption>Image Credit: <a href="https://betterstack.com/community/guides/scaling-docker/docker-swarm-kubernetes/" target="_blank">Docker Swarm vs Kubernetes: A Practical Comparison</a></figcaption>
</figure>


### Pros of Docker Swarm:

- Familiarity as `swarm mode` is a part of Docker.
- Easy to install and set up.
- Has its own Swarm API.
- Smooth integration with Docker Compose and Docker CLI.
- Intelligent node selection for picking optimal nodes.
- Built-in load balancing feature, eliminating the need to define load balancer files manually.
- Roll back to the previous version of the service in case of hazardous events such as failures.
- Fault tolerance, wherein `swarm` automatically checks for node failures and replaces them with new ones. 
- It can scale up and down as per demand.

### Cons of Docker Swarm:

- Limited customizations and extensions.
- Less functionality and lesser automation capabilities compared to Kubernetes.

### Swarm mode CLI commands:

- **swarm init**: Initialize a swarm. The Docker Engine this command targets becomes a manager in the newly created single-node Swarm.

Example:

```
> docker swarm init --advertise-addr 192.168.99.121

Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager.

To add a worker to this Swarm, run the following command:

    docker swarm join --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx 172.17.0.2:2377

To add a manager to this Swarm, run 'docker swarm join-token manager' and follow the instructions.
```
- **Swarm join**: 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. If you pass a manager token, the node joins as a manager. If you pass a worker token, the node joins as a worker.

Example:

```
> docker swarm join --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-7p73s1dx5in4tatdymyhg9hu2 192.168.99.121:2377
This node joined a swarm as a manager.
```

- Find more Swarm CLI at <a href="https://docs.docker.com/engine/swarm/#whats-next" target="_blank"> Docker Docs</a>

---

## What is Kubernetes?

Kubernetes is also a container orchestration tool which allows you to deploy and manage applications. It's open-source, portable, and highly configurable. These features make it an optimal choice for deploying complex applications. A cluster in Kubernetes consists of **worker nodes**, which are managed by Kubernetes **master**. The **master** controls, monitors and manages all the resources in the cluster.

### Kubernetes Architecture

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gqa4t0sc67575rkzv3c.jpg" alt="Swarm Architecture">
  <figcaption>Image Credit: <a href="https://betterstack.com/community/guides/scaling-docker/docker-swarm-kubernetes/" target="_blank">Docker Swarm vs Kubernetes: A Practical Comparison</a></figcaption>
</figure>

The above diagram depicts the components involved in Kubernetes.

- **Controller Plane:** It manages the operation of the Kubernetes cluster.
    - **Kube API Server:** Facilitates external communication with the cluster.
    - **Etcd:** Stores configuration data and cluster state in a key-value store.
    - **Scheduler:** Allocates pod placement based on resource needs and availability.
    - **Kube Controller Manager:** Maintains desired component states.

- **Kubernetes Node:** Executes control plane assigned tasks, including:
    - **Kubelet:** Manages containers and their lifecycle.
    - **Pod:** Smallest component of the cluster. Represents a running process instance.

**Execution Workflow:**

1. External clients interact with the cluster via ``kubectl`` commands.
2. The controller plane delegates tasks to Kubernetes nodes.
3. Nodes create pods for container execution.
4. External users access deployed apps through a load balancer.



### Pros of Kubernetes

- Wide range of functionalities such as service discovery, load balancing, storage orchestration, self-healing, horizontal scalability, automated rollout and rollbacks and batch execution.
- Open-source with a highly active community.
- Allows you to store and manage configuration data and secrets securely.
- Allows flexible management of resources when deploying an application.
- Is highly extensible through plugins.
- Interact with Kubernetes components via its API.

### Cons of Kubernetes

- Learning Kubernetes and managing its master requires specialized knowledge due to its steep learning curve.
- Frequent updates from the open-source community need careful patching to prevent workload disruptions.
- Kubernetes is too heavy for solo developers who are handling simple apps with occasional deployments.
- Teams require tools like kubectl CLI, CI/CD workflows, and DevOps practices to manage access, identity, governance, and security.

---

## Docker Swarm Vs Kubernetes

<table>
  <thead>
    <tr>
      <th>Feature</th>
      <th>Docker Swarm</th>
      <th>Kubernetes</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><b>Set up</b></td>
      <td>Easy with <b>docker</b> command</td>
      <td>Setting up a Kubernetes cluster manually can be complicated.</td>
    </tr>
    <tr>
      <td><b>Container support</b></td>
      <td>Works only with Docker containers</td>
      <td>Works with <b>containerd</b>, <b>Docker</b>, etc. </td>
    </tr>
    <tr>
      <td><b>Networking</b></td>
      <td>Basic</td>
      <td>Advanced</td>
    </tr>
    <tr>
      <td><b>Learning Curve</b></td>
      <td>Easy to get started</td>
      <td>Steep curve</td>
    </tr>
    <tr>
      <td><b>Complexity</b></td>
      <td>Simple & lightweight</td>
      <td>Complicated & heavyweight</td>
    </tr>
    <tr>
      <td><b>Load Balancing</b></td>
      <td>Automatic</td>
      <td>Manual</td>
    </tr>
    <tr>
      <td><b>CLIs</b></td>
      <td>Comes when installing Docker</td>
      <td>Need to install <b>kubectl</b> CLI</td>
    </tr>
    <tr>
      <td><b>Scalability</b></td>
      <td>No automatic scaling</td>
      <td>Automatic Scaling</td>
    </tr>
    <tr>
      <td><b>Security</b></td>
      <td>Only TLS support</td>
      <td>Supports SSL/TLS, RBAC & secret management</td>
    </tr>
  </tbody>
</table>

--- 

## Conclusion

In conclusion, while Docker Swarm and Kubernetes serve as container orchestration tools, they have distinct characteristics and functionalities.

Docker ``swarm`` offers easy setup and simplicity, making it ideal for smaller projects with straightforward deployment needs. However, it lacks the advanced features and scalability of Kubernetes.

On the other hand, **Kubernetes** provides a comprehensive set of features for managing complex applications at scale. Although it has a steep learning curve and requires specialized knowledge, Kubernetes offers extensive functionality, including advanced networking, automated scaling, and robust security features.

Ultimately, the choice between Docker Swarm and Kubernetes depends on the specific requirements and complexity of the project. Docker Swarm may suffice for simpler deployments, while Kubernetes is better suited for larger, more intricate applications that demand advanced orchestration capabilities.

--- 

### References

[Docker Swarm vs. Kubernetes: A Comparison](https://www.ibm.com/blog/docker-swarm-vs-kubernetes-a-comparison/)
[Docker Swarm vs Kubernetes: A Practical Comparison](https://betterstack.com/community/guides/scaling-docker/docker-swarm-kubernetes/)

---

> Note: This article is a part of the curriculum in the DevOps course. My student ID is C0884179.

--- 

### Check Out My Previous Articles

[Changing the MAC Address using Python](https://dev.to/dharmil18/changing-the-mac-address-using-python-564j)
[Writing a Network Scanner using Python](https://dev.to/dharmil18/writing-a-network-scanner-using-python-3b80)
[Man In The Middle Attack (MITM) Part 1 — ARP Spoofing](https://dev.to/dharmil18/man-in-the-middle-attack-mitm-part-1-arp-spoofing-3747)
[Man In The Middle Attack (MITM) Part 2 — Packet Sniffer](https://dev.to/dharmil18/man-in-the-middle-attack-mitm-part-2-packet-sniffer-1nch)