Full lesson
Explore the full explanation, examples, and visuals at your own pace.
Illustrative night on one server
At 3 a.m., shop-api crashes, and nothing brings it back. At 3:05, a pager alert wakes you. By 3:20, you've SSHed into the server. At 3:25, you run docker run again, and the app finally returns. On one server, a crash stays down until a human steps in.
Same crash, two worlds
With one server, shop-api crashes, a page wakes the on-call engineer, and they run docker run by hand. Only then is it back online. A cluster, meaning several machines managed as one, works differently. The Control plane notices the crash, starts a new pod, and shop-api is back online without a human.
You declare: 3 copies. Kubernetes keeps making that true.
That's desired state. You describe what should be running, three copies, not the steps to start them. Kubernetes does the starting and restarting for you.
The manager who never cooks
Think of a restaurant kitchen. The manager is the Control plane, holding a clipboard that says replicas: 3. The cooks are your shop-api pods, working at stations, which are Node 1 and Node 2. The manager never cooks. They just count, and when a cook leaves, they send in a replacement.
shop-api-deployment.yaml
This Deployment is the manager's clipboard. It's a file declaring what you want running. Replicas: 3 is the count. The template wraps your Docker image, shop-api:1.0, into a pod, the smallest unit Kubernetes runs. And the readiness probe asks /healthz whether each pod is ready for traffic.
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: 8080
readinessProbe:
httpGet:
path: /healthz
port: 8080Terminal
Two commands. The first, kubectl apply, sends the file to the cluster. The second, kubectl get pods, lists what's running. We see three shop-api pods, each showing one of one ready and a Running status. The odd suffixes are generated names, so yours will differ. Three copies, exactly as declared.
kubectl apply -f shop-api-deployment.yaml
kubectl get podsdeployment.apps/shop-api created NAME READY STATUS RESTARTS AGE shop-api-6f9c7d5b8-2kqpx 1/1 Running 0 30s shop-api-6f9c7d5b8-8mzvt 1/1 Running 0 30s shop-api-6f9c7d5b8-tr4hw 1/1 Running 0 30s
Step 1: Control plane places three pods
Here's where the declaration goes to work. kubectl applies the file, delivering replicas: 3 to the Control plane, the cluster's brain, which stores that wish. Then it schedules pods onto the worker nodes. In this setup, shop-api pod 1 and pod 2 land on Node 1, and pod 3 lands on Node 2.
Step 2: a Service in front
Users shouldn't have to know which pod is alive, so a Service sits in front. It's one stable address. It finds shop-api pods by their app: shop-api label, then spreads user traffic across all three, whether they run on Node 1 or Node 2.
Step 3: Node 2 crashes
Now Node 2 loses power, and shop-api pod 3 goes down with it. Requests the Service sends to that pod fail, so the running count drops from three to two. The cluster doesn't notice instantly. By default, the Control plane waits a short grace period before declaring the node gone.
Desired vs running: the gap
The table shows the Deployment, shop-api, with desired three and running three. Then the running count changes to two. The Control plane keeps comparing those two numbers, and now they don't match. That gap triggers the repair, not a person. Declare the state you want, and Kubernetes keeps closing the gap.
Step 4: a replacement pod on Node 1
The Control plane schedules a replacement onto Node 1. The new shop-api pod 4 starts as Pending, then Running once its container starts. After /healthz passes, it's Ready, and the Deployment counts three of three ready again. Notice this is a brand-new pod, not pod 3 moved over.
Service only forwards to ready pods
Watch a request from the User. It hits the Service, which forwards it to shop-api pod 1, and a 200 OK comes back. Pod 4 is still starting, so the Service skips it until it's Ready. Dead pod 3 gets dropped too. Users mostly see healthy answers, though a few requests may fail while Kubernetes is still detecting the crash.
Kubernetes keeps comparing running pods to your declared count, and fixes gaps.
Kubernetes keeps comparing the pods that are running to the count you declared, and it fixes any gap. So write your desired state in YAML, like replicas: 3, and add a readiness check, so the Service only trusts pods that are actually ready to serve traffic.
