DOCKER · DOCKER FROM SCRATCH · CHAPTER 10

Orchestration: Swarm and Kubernetes

When one server is not enough: replicas, high availability and the jump to Kubernetes.

6 min readCourse: Docker from ScratchLevel: beginner

From point A to point B

Point A: with Docker and Compose you manage a complete application, but on a single host. If that server goes down, the application goes down; if more traffic arrives than it can handle, there is nowhere to grow. Point B: by the end of this chapter you will understand what an orchestrator solves, create a Docker Swarm cluster with a replicated service and recognize the pieces of Kubernetes, which is where the path continues.

Why you need an orchestrator

With docker run or Compose, you decide which host each container runs on and you keep an eye on it to make sure it stays alive. That stops working as soon as you go beyond one server:

  • Availability: if the host fails, someone has to launch the containers again on another one.
  • Scale: with more load you need more copies of the application, spread across several servers, and traffic balanced between them.
  • Updates: changing versions without cutting service means replacing the copies a few at a time.

An orchestrator automates all of that. You declare the state you want, for example "three copies of this image, published on port 8080", and the orchestrator spreads the containers across the cluster's nodes, watches them and replaces them when they fail.

Courses from a few years ago listed three options: Docker Swarm, Kubernetes and Apache Mesos. Mesos was retired and moved to the Apache Software Foundation Attic in October 2025. Today the conversation is Swarm or, above all, Kubernetes.

Docker Swarm: the orchestrator built into Docker

Swarm comes included in Docker Engine: you install nothing else. A Swarm cluster has two types of node:

  • Managers: they keep the cluster state and decide where each container runs. For high availability you use three or five, always an odd number.
  • Workers: they run the containers the managers assign to them.

The unit of work is the service: the equivalent of a docker run, but with a number of replicas that Swarm maintains and spreads across the nodes.

A Docker Swarm cluster with a replicated service The manager receives docker service create with three replicas and port 8080, stores the desired state and schedules one replica on each worker. The routing mesh answers on port 8080 on every node, and if a node fails its replica is rescheduled elsewhere. request to :8080 on any node manager · Leaderdocker service create--replicas 3 -p 8080:80 desired state: 3 × nginx schedules worker 1mesh :8080 web.1 worker 2mesh :8080 web.2 worker 3mesh :8080 web.3 if a node failsits replica is recreated the routing mesh forwards each request to a live replica

A service with three replicas spread across the cluster; the published port answers on any node. Diagram: The House of Routing.

Creating a Swarm cluster and a replicated service

You need Docker installed on each server (three virtual machines are enough). On the one that will be the manager:

# On the manager: use its IP on the network the nodes share
docker swarm init --advertise-addr 192.168.1.10
# Swarm initialized ... To add a worker to this swarm, run the following command:
#   docker swarm join --token SWMTKN-1-... 192.168.1.10:2377

Copy the docker swarm join command it returns and run it on each worker. Then, back on the manager:

docker node ls            # the three nodes, one marked as Leader

# A service with three nginx replicas, published on 8080
docker service create --name web --replicas 3 -p 8080:80 nginx:stable

docker service ls
docker service ps web     # which node each replica runs on

Now test it the way an orchestrator would:

# Scale: Swarm creates two more replicas and spreads them out
docker service scale web=5

# Update with no downtime: replaces the replicas one by one
docker service update --image nginx:alpine web

# Simulate a failure: shut down a worker and watch Swarm reschedule its replicas
docker service ps web

# Clean-up
docker service rm web

The published port answers on any node in the cluster, even one without a replica: Swarm's routing mesh forwards the request to an active container.

Connecting to what you know. A Compose file can also be deployed to Swarm with docker stack deploy -c compose.yaml my-app. With the deploy: key you set replicas, limits and update policies. From chapter 5 to a cluster is a short step.
Security. The docker swarm join token lets nodes join your cluster. Treat it like a password; if it is exposed, rotate it with docker swarm join-token --rotate worker.

Kubernetes: the industry standard

Kubernetes was born at Google, was donated to the Cloud Native Computing Foundation (CNCF) and today is the orchestrator every cloud offers: EKS on AWS, GKE on Google Cloud and AKS on Azure. It solves the same problems as Swarm, with far more room for configuration and extension.

Kubernetes architecture kubectl talks to kube-apiserver in the control plane, which also hosts etcd, kube-scheduler and kube-controller-manager. On every worker node, kubelet receives instructions from the API server and asks the container runtime to run the pods; kube-proxy handles Service networking. kubectl you / CI CONTROL PLANE kube-apiserver etcd kube-scheduler kube-controller-manager cloud-controller-manager optional · only in a cloud provider decides: what runs, where, how many WORKER NODE 1 kubelet kube-proxy container runtime pod pod pod WORKER NODE 2 kubelet kube-proxy container runtime pod pod run the workloads Every component talks to kube-apiserver; only the API server reads and writes etcd.

The control plane decides; the nodes run the pods. Diagram: The House of Routing.

ComponentWhereWhat it does
kube-apiserverControl planeThe front door: everything, including kubectl, talks to it
etcdControl planeDistributed key-value database holding the cluster state
kube-schedulerControl planeDecides which node each pod runs on
kube-controller-managerControl planeCompares the actual state with the desired one and fixes the differences
kubeletEvery nodeAgent that starts and watches the pods on its node
Container runtimeEvery nodeRuns the containers; today it is usually containerd
kube-proxyEvery nodeImplements Service networking

The command-line tool is kubectl. The equivalent of the Swarm service you created above is a Deployment:

kubectl cluster-info
kubectl get nodes

kubectl create deployment web --image=nginx:stable --replicas=3
kubectl get pods -o wide            # three pods spread across the nodes
kubectl scale deployment web --replicas=5
Two updates compared with older courses. kubectl run no longer accepts --replicas: it creates a single pod. For replicas you use kubectl create deployment. And Kubernetes no longer runs containers with Docker: since version 1.24 it uses a CRI-compatible runtime directly, usually containerd. Your Docker images work just the same, because they follow the OCI standard.

Swarm or Kubernetes

Docker SwarmKubernetes
InstallationIncluded in Docker EngineIts own distribution or a managed cloud service
Learning curveLow: reuses Docker and Compose conceptsHigh: many new objects and concepts
Scale and ecosystemSmall and medium clustersIndustry standard, with a huge ecosystem
In the cloudYou manage itEKS, GKE, AKS and many more

Swarm is a good option for small teams that want high availability without complexity. For a career in modern cloud, Kubernetes is essential, and that is why it is the next topic on the path.

Challenge

Answer before you open the solution. Getting it wrong here is the cheapest way to learn.

1. What problem does an orchestrator solve that Compose does not?

Running and watching containers on several hosts: spreading them out, replacing them if a node goes down, scaling them and updating them with no downtime.

2. Where do you run docker service create?

On a manager. Workers only run the tasks assigned to them.

3. A Swarm service publishes port 8080 with three replicas on a five-node cluster. Which nodes does the port answer on?

All five. The routing mesh forwards the request to an active replica even if the node has none.

4. Which Kubernetes component decides which node a new pod runs on?

kube-scheduler. Then the kubelet on that node starts it.

5. Kubernetes no longer uses Docker as its runtime. Do your images built with Docker stop working?

No. They are standard OCI images and containerd runs them just the same.

What you take away

  • An orchestrator spreads, watches, scales and updates containers across several hosts.
  • Swarm comes with Docker: swarm init, swarm join and service create --replicas.
  • An odd number of managers for high availability; workers to run the containers.
  • Kubernetes: control plane (API server, etcd, scheduler, controllers) and nodes (kubelet, runtime, kube-proxy).
  • Replicas in Kubernetes with kubectl create deployment --replicas.
  • Kubernetes uses containerd; your Docker images still work.

You have finished the ten chapters of Docker from Scratch. Before moving on to Kubernetes, the extra chapter gets you reading and writing YAML with confidence.

Official resources

Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's Docker for Absolute Beginners course (December 2024). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official Docker documentation.

Navigate