Docker Swarm, Kubernetes’s clever little borther

Docker Swarm, Kubernetes’s clever little brother

Is kubernetes suitable for any container based project?

There is no doubt that kubernetes is one of the most talked about technologies in the domain of cloud and containers. Kubernetes provides a complete solution to managing containers, but there are cases where it is not the best solution.

The main disadvantage of kubernetes is its complexity and learning curve. This complexity is due to several reasons:

We need to know both the “language” of Docker and the “language” of kubernetes.

There are a lot of “moving parts” in the infrastructure (kube-apiserver, etcd, kube-scheduler, kube-controller, kubelet, kube-proxy etc..)

There are many types of objects (Service, Pod, Deployment, ReplicaSet)

A vast amount of features that we do not necessarily need.

In short, projects with tight schedules, or small projects consisting of only a few containers, can’t justify the investment in kubernetes. Should such projects give up on using a container orchestration framework? Of course, the answer is no.

In this article, I will first talk about the benefits of orchestrators in general. Then I will demonstrate Docker Swarm, Docker’s native orchestrator.

Docker Swarm may not be rich in features like his older brother kubernetes, but it’s so easy to use, that at the end of this article you’ll know how to run and manage your containers in an orchestrator.

So why do you need an orchestrator?

There are many numerous types of orchestrators which can be seen in the following (slightly dated) lecture.

Orchestrators are container management systems, that usually provide the following advantages:

SchedulingAutomatic provisioning of containers to servers with optimal resource utilization.

Cloud-agnosticWorks the same way in any cloud or on-premise environment

High availability

Restart faulty containers

Rolling updates

Container monitoring

And more …

Docker swarm

Docker Swarm is the official Docker orchestrator, and therefore comes built-in with Docker.

It also uses the same “language” as docker (same command line syntax / docker compose), so anyone who works with Docker can take advantage of Docker swarm without installing third parties and learning new systems.

Docker swarm’s architecture is very easy to understand. While kubernetes has dozens of object types, Swarm has only a handful:

Manager — Manages the cluster, can have replicas for redundancy.

Worker node — A machine in a cluster that runs containers.

Service — A set of containers of the same type.

Container — The basic building block is a simple Docker container (not a pod).

Running a Docker swarm cluster

Now that we know what an orchestrator is and what Docker swarm is about, let’s see how we can set up a Swarm cluster in a few simple steps.

1. Some servers should be prepared, (one server can also do), and the following ports should be open: 2377,7946, 4789. The servers can be on any cloud, on several different clouds, or on-premise

2. SSH to one of the servers that will be the manager (with an external ip, for example, x.x.x.x) and run:

$ docker swarm init — advertise-addr x.x.x.x

3. The previous command will generate the swarm manager and return instructions on how to add additional servers to the cluster that the manager will manage. The output will look like this:

docker swarm join — token SWMTKN-1–49nj1cmql0jkz5s x.x.x.x:2377

Now run the output above in all the other servers. This will attach them to the cluster.

4. To see the servers that are participating in the cluster, go back to the manager and run:

$ docker node ls

ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS

e32e23r3 worker2 Ready Active

Erg56y7j8 worker1 Ready Active

T43t54y * manager1 Ready Active Leader

5. All that is left is to run a few containers in the cluster using the following command:

docker service create — replicas 2 — name helloworld nginx

docker service create — replicas 1 — myredis redis

“service” is a set of containers of the same type.

6. To see the services running in cluster Run:

$ docker service ls

ID NAME SCALE IMAGE COMMAND

f54fg55g5g5g helloworld 2/2 nginx

E32e2323r44r myredis 1/1 redis

7. A few more useful commands:

Change the scale of service

$ docker service scale helloworld = 5

See all the metadata in the container

$ docker service inspect helloworld

Delete a group of containers

$ docker service rm helloworld

In this script, there is a full example of running a docker swarm cluster on three servers, one in AWS, one in AZURE and one ON-PREMISE.

Summary

That’s all!

We saw how we could quickly set up a cloud agnostic docker cluster with an orchestrator.

The image below shows an example of Docker swarm using a simple web UI tool that I helped to create back in 2017: