Full lesson
Explore the full explanation, examples, and visuals at your own pace.
One server, one docker run
Your shop-api crashed at 3 a.m., and it's staying down. You started it once with docker run, and that was the whole plan. Nothing restarts it. So when users send GET /products, the requests get refused, and they see errors until you wake up and fix it.
Two worlds after the same crash
With one server, a crash sends a 3 a.m. alert, and you restart shop-api by hand with docker run again. You're the recovery plan. A cluster is a group of servers working together. There, the Control plane, the part that manages the cluster, notices the crash and replaces the pod automatically.
A manager who never cooks
Think of a restaurant. The manager is the Control plane. The cooks are your shop-api pods, working at kitchen stations, which are the nodes. The order slip is replicas: 3. The manager never cooks. They just watch the count, and when a cook disappears, someone new walks in.
The loop that never stops
You don't tell Kubernetes how to start copies. You write down what you want, three shop-api pods, and it loops forever. It counts what's running, compares that to three, and starts or removes pods if the numbers differ. Then it repeats. The mental model is simple: wanted versus running, checked constantly.
shop-api.yaml
This file is a Deployment, which is the written wish for your app. The key line is replicas: 3, the count you want. The label app: shop-api ties those copies together. And the image is shop-api:1.0, the same one you ran with docker run.
apiVersion: apps/v1
kind: Deployment
metadata:
name: shop-api
spec:
replicas: 3
selector:
matchLabels:
app: shop-api
template:
metadata:
labels:
app: shop-api
spec:
containers:
- name: shop-api
image: shop-api:1.0
ports:
- containerPort: 8080Terminal
kubectl is the command-line tool that sends your file to the cluster. After we apply it, we ask for the pods. A pod is one running copy of shop-api. Here are three, all Running. Those random-looking names are generated, so yours will differ. The count is what matters.
$ kubectl apply -f shop-api.yaml
deployment.apps/shop-api created
$ kubectl get pods # example output, names vary
NAME READY STATUS AGE
shop-api-6b7f9d8c4-x2kqp 1/1 Running 40s
shop-api-6b7f9d8c4-m7tfz 1/1 Running 40s
shop-api-6b7f9d8c4-q9wbn 1/1 Running 40sWhere the 3 pods actually live
Here's the layout. kubectl sends replicas: 3 to the Control plane. The Control plane schedules the pods onto machines. Node 1 ends up with two shop-api pods, Node 2 with one. A Service sits in front, one stable front door, and sends user traffic to all three.
3 a.m. again: Node 2 loses power
Now the same 3 a.m. failure returns, but bigger. A whole server, Node 2, loses power. What happens to shop-api when nobody's awake to fix it?
Running pods: 3 drops to 2
Node 2 is gone, and so is the shop-api pod running on it. The Service still tries to send user traffic there, but those requests fail. Meanwhile the Control plane stops hearing from Node 2. Only the two pods on Node 1 are left, so running pods drop from three to two.
The gap gets filled on Node 1
The Control plane picks Node 1 and schedules a replacement shop-api pod there. The pod starts as Pending, placed but not started. Then it's Running, meaning the container is up. Once its checks pass, it's Ready. Now three of three are ready again, usually within seconds to a minute.
Traffic only goes to Ready pods
A user sends GET /products to the Service, which forwards it to one of the two healthy shop-api pods, and a 200 OK comes back. The Service skips any pod that isn't Ready, meaning one not yet passing its readiness check. Once the new pod reports Ready, it joins and gets the next request. One caveat: requests already sent to the dead pod can still fail.
Worth the YAML when...
Kubernetes pays off when you run many copies across several servers. The cost is real: new concepts, YAML, and a cluster to maintain. It won't fix bugs in shop-api itself. For one small app, plain docker run may be enough.
- Many copies across several servers
- Cost: new concepts, YAML, cluster upkeep
- One small app? docker run may suffice
Kubernetes keeps checking wanted copies against running copies, fixing any gap.
Kubernetes keeps checking wanted copies against running copies, and fixes any gap. That's the whole mental model. So write what you want in the Deployment, replicas: 3 for shop-api, and let the cluster handle the 3 a.m. restarts.
