Kubernetes Field Guide · Chapter 1 of 16
Why Kubernetes: the problems Docker leaves you with
Start here, even if you have never touched a cluster. How we got from one app per server to containers, what Docker solved, the problems it leaves you with at 3am, and the one idea Kubernetes is built on.
- 35 min
- Beginner
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Kubernetes in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
How we got here
Kubernetes makes sense once you see the problem it was built for, and that problem only appeared after a few earlier fixes. Step through four eras of running software. Each one fixes the last one's problem and creates a new one.
Nothing to fix yet: you buy a server, install the OS, install your app.
- New capacity
- Weeks: order and rack a server
- Apps per server
- One, to avoid conflicts
- Typical load
- Mostly idle
New problem Servers sit mostly idle, apps fight over library versions, and "it works on my machine" is a daily sentence.
A hypervisor splits one server into many virtual ones, so idle servers get packed.
- New capacity
- Minutes to boot a VM
- Size
- Gigabytes: every VM has a full OS
- Apps per server
- A handful
New problem Every VM is a whole operating system to patch, and each one is set up a little differently. Nobody knows exactly what is on them.
An image holds your app and everything it needs. Containers share the host kernel, so they are small and start fast. The same image runs on your laptop, in CI and in production.
- Start time
- About a second
- Size
- Megabytes
- Apps per server
- Dozens
New problem Packaging is solved. But Docker runs containers on one machine, and production is never one machine.
Now you have 40 containers across 10 servers, deployed several times a day.
- Containers
- Tens to thousands
- Servers
- Many, and some will fail
- Changes
- Several deploys a day
The gap Someone has to decide where each container runs, restart it, move it, scale it, route to it and update it. That job is called orchestration, and Kubernetes is the orchestrator that won.
1 of 4
Every picture in this guide uses the same colours, so you can read a diagram before you read its labels:
- Machines: servers and nodes
- Your app and its containers
- Kubernetes objects, healthy
- Control plane and controllers
- Traffic and warnings
- Failures
What Docker solved, and what it did not
A container image is your app plus everything it needs to run: the runtime, libraries and config defaults, frozen into one file. A container is that image running as an isolated process. Docker made both easy: docker build makes the image, docker run starts it, and it behaves the same on your laptop and on a server.
That is a packaging solution. Look at what docker run actually promises: one container, on this machine, now. It knows nothing about your other servers, whether the app is healthy, or how many copies you wanted.
Once you run more than one server, someone has to answer the questions docker run does not: which server has room, what happens when one dies, how traffic finds the new copy, how to swap versions without dropping requests. Teams answer them with scripts, cron jobs and a runbook. Kubernetes replaces that pile with one system.
The 3am test: Docker vs Kubernetes
The easiest way to feel the difference is to break things. Pick an incident and play it on both setups at once: three servers running Docker, and a three-node Kubernetes cluster running the same app. Watch who does the work, and how long the app is unhealthy.
Interactive · The 3am test
Same incident, two setups. Pick one and press Play.
Docker on 3 servers
03:12
Kubernetes, 3 nodes
03:12
Notice the pattern. Docker is good at the one machine it runs on. Everything that spans machines, or needs a decision, falls to you. Kubernetes moves those decisions into software that never sleeps.
Desired state and the reconcile loop
With Kubernetes you stop giving instructions ("start a container on server 2") and start describing an outcome: three copies of web, image nginx:1.27. That description is the desired state.
Small programs called controllers then run a loop, forever: observe what is actually running, compare it with what you asked for, and act on the difference.
If a container dies, actual state drifts from desired state, and a controller fixes it. Nobody has to notice. Try it: kill containers, fail a node, change the replica count. Then switch to plain Docker and do the same.
Interactive
Desired state vs actual state
replicas: 3
desired 3running 3in sync
What Kubernetes takes off your hands
Everything in this guide is one of these jobs, done by a controller watching desired state. Each links to the chapter that covers it.
- Placing containers on serversYou pick a server with roomThe scheduler · chapter 2
- Restarting crashed containersRestart policy, one host onlyKubelet and ReplicaSets · chapter 4
- Recovering from a dead serverRebuild and redeploy by handPods rescheduled elsewhere · chapter 4
- Scaling up and downStart more containers by handReplicas and the autoscaler · chapter 9
- Deploying without downtimeA deploy script you maintainRolling updates and rollbacks · chapter 4
- Finding other servicesHard-coded IPs in env filesServices and cluster DNS · chapter 5
- Config and secretsEnv files copied to each serverConfigMaps and Secrets · chapter 6
- Knowing an app is healthyA check on the load balancerLiveness and readiness probes · chapter 10
Do you need Kubernetes?
Kubernetes has real costs: a control plane to run or pay for, upgrades every few months, networking and storage to understand, and a learning curve for everyone who touches it. It earns that cost when you have many services, deploy often, or create and destroy environments on demand. Answer for a system you know:
Interactive
Do you need Kubernetes? Answer for your own system.
One VM with Docker Compose
Cheapest. One bill, nothing new to learn.
A small, steady system does not need a scheduler. Back up the VM, put a load balancer in front if you need two.
A managed container service
AWS ECS or Fargate, Google Cloud Run, Azure Container Apps.
You get restarts, scaling and rolling deploys without running a control plane or learning Kubernetes.
Kubernetes
Managed (EKS, AKS, GKE), or K3s on your own servers (chapter 13).
Many services, frequent changes or on-demand environments are exactly what it is built for. The learning curve pays for itself.
Hands-on: your first cluster
The lab is docked at the bottom of your screen.
Live lab · no install
Your first cluster, in the browser
Create a Deployment, delete its pods and watch Kubernetes put them back.
- Tasks0/6
Meet your cluster
A kind cluster is already running. It has one node that is both control plane and worker.
kubectl get nodesOne node, Ready. Everything you create will run on it.
Declare desired state: 3 copies of nginx
You describe what you want. You never say how to start containers.
kubectl create deployment web --image=nginx:1.27 --replicas=3Watch the map: three pods went from dashed (starting) to solid (ready).
Break it: delete one pod
Click a pod in the map to paste its name, or use the one-liner.
kubectl delete $(kubectl get pods -l app=web -o name | head -n 1)Gone. Now look at the map.
Watch it heal
The reconcile loop noticed 2 instead of 3 and made a new pod. Its name is new and its age is seconds.
kubectl get pods -l app=webSelf-healing is not magic: it is a loop comparing desired with actual.
Change your mind: scale to 5
Changing desired state is one command. The same loop does the work.
kubectl scale deployment web --replicas=5Delete two at once, then scale back to 3
Predict first: how many pods will you see a few seconds later?
kubectl delete $(kubectl get pods -l app=web -o name | head -n 2) kubectl scale deployment web --replicas=3The loop runs in both directions: it creates missing pods and removes extra ones.
- Lab complete. Keep experimenting, or press Reset to start again.
- ready
- starting / not ready
- failing
- terminating
Prefer your own machine? The same steps on a local cluster
kind (Kubernetes in Docker) runs a whole cluster inside Docker on your laptop. It is free, starts in under a minute and is what every chapter in this guide uses. You need Docker running first.
brew install kind kubectl
kind- Creates clusters whose nodes are Docker containers.
kubectl- The command-line client for any Kubernetes cluster.
On Linux or Windows, follow the kind installation guide and the kubectl install page.
kind create cluster --name learn
kubectl get nodes
NAME STATUS ROLES AGE VERSION
learn-control-plane Ready control-plane 45s v1.34.0--name learn- Names the cluster. kubectl is pointed at it automatically (context kind-learn).
get nodes- One node that is both control plane and worker. Your version may differ.
Now declare some desired state: three copies of nginx.
kubectl create deployment web --image=nginx:1.27 --replicas=3
kubectl get pods -l app=web
NAME READY STATUS RESTARTS AGE
web-6c9b8d7f5d-4xk2p 1/1 Running 0 12s
web-6c9b8d7f5d-9qmzt 1/1 Running 0 12s
web-6c9b8d7f5d-tw7rc 1/1 Running 0 12screate deployment web- Records desired state: a Deployment named web.
--replicas=3- How many copies should exist at all times.
-l app=web- Filter by label. create deployment labels its pods app=web.
Delete one pod and watch the reconcile loop replace it.
kubectl delete $(kubectl get pods -l app=web -o name | head -n 1)
kubectl get pods -l app=web
pod "web-6c9b8d7f5d-4xk2p" deleted
NAME READY STATUS RESTARTS AGE
web-6c9b8d7f5d-9qmzt 1/1 Running 0 2m
web-6c9b8d7f5d-tw7rc 1/1 Running 0 2m
web-6c9b8d7f5d-zc8hn 1/1 Running 0 3s-o name- Prints pod/NAME, which kubectl delete accepts directly.
zc8hn, 3s- A brand-new pod. The old one is gone for good; its replacement has a new name.
Mini mission
Scale it, then break it
Scale web to five replicas. Predict what happens if you delete two pods at once, then do it and watch with -w. Finally, scale back to three.
Reveal one safe solution
kubectl scale deployment web --replicas=5
kubectl delete $(kubectl get pods -l app=web -o name | head -n 2)
kubectl get pods -l app=web -w
kubectl scale deployment web --replicas=3
Two new pods appear within seconds, so you are back to five. Scaling down to three makes the controller delete two: the same loop, in the other direction. Press Ctrl+C to stop watching.
Check yourself
Quiz
0 of 5 answered