Docker Swarm, Kubernetes’s Clever Little Brother
Docker Swarm, Kubernetes’s Clever Little Brother
If Kubernetes seems like a bit too much of a barrier, consider giving Docker's native orchestrator a try.
Join the DZone community and get the full member experience.Join For Free
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 its 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:
- Scheduling — Automatic provisioning of containers to servers with optimal resource utilization.
- Cloud-agnostic — Works the same way in any cloud or on-premise environment
- High availability
- Restart faulty containers
- Rolling updates
- Container monitoring
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. Multiple 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.
We saw how we could quickly you can 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:
Opinions expressed by DZone contributors are their own.