How Kubernetes looks after containers

You write down what you want, like three copies of the shop at about 60% CPU, and Kubernetes keeps making it true, even when a pod dies or a whole machine goes. Watch it put things right, then try breaking the cluster yourself.

Level: DeepWatch: 17 steps · ~75 secondsBreak the cluster yourselfRecall: 3 questions · 8 cardsCompare 4 ways to run itYAML · TypeScript

What you'll take away

  • Explain how a Deployment, a ReplicaSet and the scheduler keep three pods running
  • Tell readiness from liveness: one only cuts traffic, the other restarts the container
  • Read how maxSurge and maxUnavailable keep a rolling update from going down
  • Work out how many pods the HPA asks for from CPU: ⌈pods × measured / target⌉

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
Say what you want3 / 3 nodes · v1
control plane · shop:v1 × 0
Service shop
kubectl apply: 3 × shop:v1 → …
Step 1 / 17 · step by step
0Wanted (replicas)
0Ready
0restarts
v1image
0%CPU a pod

The picture is a block of the cloud district. Three nodes (machines) stand in a row, each with room for three pods. A pod is a small building: lit when ready, dashed while starting, amber when it hangs, red when it died, violet once it runs v2. The control plane is the tower behind them, its sign saying what the Deployment wants, and a gold light is the scheduler sending a pod to a node. The Service is the gate in front. The timings are the model's; where Kubernetes has a default, it's said.

Step 4 · Every option at once, on one clock

Compare them all: Kubernetes

One shop four ways, through the same events: a container crashes at minute 2, a machine dies at 6, traffic doubles at 13, and a new version ships at 17. The bar below each is the site's state over time.

minute 0.0
docker runone machine, no restart policydown 0.0 min · slow or errors 0.0 min

Use it when Trying things on your own machine; work nobody minds being down

Skip it when Anything that must stay up: one crash and it's down until someone notices

A model: the machine that dies is the one the 1-replica pod is on. The ~5-minute wait before replacing pods and the HPA's 15-second check are Kubernetes defaults; start-up times are made up. Nobody fixes the dead machine within these 20 minutes. Press any tile to see it large above, with when it fits.

Who does what

PartWhat it is
PodOne or more containers that run together, with one IP. Replaced, never repaired
NodeA machine (VM or real) that runs pods; its kubelet starts the containers
DeploymentWhat you want: the image, how many, how to roll out a new version
ReplicaSetMade by the Deployment, one per version: keeps the pod count right
SchedulerPicks a node with room (by the pods' requests) for each new pod
ServiceOne name and IP in front of pods picked by label; only ready ones get traffic
IngressHTTP from outside the cluster to Services, by host and path
ConfigMap · SecretConfig kept out of the image, given to pods as env vars or files
Readiness probeFails: no traffic from the Service (the pod keeps running)
Liveness probeFails a few times in a row: the container is restarted
HPAChanges replicas to keep a metric, like CPU, near a target
NamespaceA folder for objects: names, access rules and quotas per team or app

You describe it, it keeps it true

You never tell Kubernetes "start a container on node-2". You say "three of shop:v1", and controllers keep comparing that with what is running and fix the difference, all day. That's why a dead pod comes back with nobody awake: nothing special happened, the count was just wrong.

Is it worth it?

For one small app on one server, often not: a container with --restart=always and a backup gets you far. Kubernetes pays off when you have several services, need more than one machine, and want rollouts, restarts and scaling done the same way for all of them. Someone still has to run the cluster, or you pay a cloud to.

In real code

shop.k8s.yaml
# The shop from the lesson: kubectl apply -f shop.k8s.yaml
# Three copies of shop:v1, a Service in front, and an HPA that keeps CPU near 60% with 3 to 6 pods.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop
  labels: { app: shop }
spec:
  replicas: 3
  selector:
    matchLabels: { app: shop }
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # one pod over the count while rolling out
      maxUnavailable: 0  # never fewer ready pods than replicas
  template:
    metadata:
      labels: { app: shop }
    spec:
      containers:
        - name: shop
          image: registry.example/shop:v1
          ports:
            - containerPort: 8080
          envFrom:
            - configMapRef: { name: shop-config }
          env:
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef: { name: shop-db, key: password }
          resources:
            requests: { cpu: 250m, memory: 256Mi }  # what the scheduler reserves
            limits: { memory: 512Mi }               # over it: OOMKilled
          readinessProbe:          # fails: the Service stops sending it requests
            httpGet: { path: /ready, port: 8080 }
            periodSeconds: 5
          livenessProbe:           # fails 3 times in a row: the kubelet restarts the container
            httpGet: { path: /healthz, port: 8080 }
            periodSeconds: 10
            failureThreshold: 3
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: shop-config
data:
  LOG_LEVEL: info
  CURRENCY: THB
---
apiVersion: v1
kind: Service
metadata:
  name: shop
spec:
  selector: { app: shop }
  ports:
    - port: 80
      targetPort: 8080
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: shop
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: shop }
  minReplicas: 3
  maxReplicas: 6
  metrics:
    - type: Resource
      resource:
        name: cpu
        target: { type: Utilization, averageUtilization: 60 }
Run: kubectl apply -f shop.k8s.yaml (checked with kubeconform -strict against the Kubernetes 1.31 schemas: a schema check, no cluster)It printed: kubeconform -strict, Kubernetes 1.31: 4 of 4 valid (Deployment, ConfigMap, Service, HorizontalPodAutoscaler)

The shop from the picture: a Deployment (3 replicas, a rolling update that never drops below 3 ready, readiness and liveness probes, CPU requests), a ConfigMap, a Service in front and an HPA that keeps CPU near 60% with 3 to 6 pods. It's checked with kubeconform -strict against the Kubernetes 1.31 schemas, which catches a misspelt key or a wrong type. That's a schema check: there's no cluster here to run it on.

Common questions

What is a pod, and how is it different from a container?

A pod is the smallest thing Kubernetes runs: one or more containers living together on one IP. A dead pod isn't repaired; a new one replaces it with a new name and a new IP. That's why a Service sits in front.

Table: who does what

Readiness vs liveness probe: what's the difference?

Fail readiness and the Service just stops sending the pod requests; it keeps running. Fail liveness a few times in a row and the kubelet restarts the container. Don't let liveness start checking before a slow app is up, or it restarts for ever: that's what startupProbe is for.

A node dies. Do its pods move to another node at once?

No. Pods don't move; they're replaced, and by default Kubernetes waits about 5 minutes in case the node comes back. With replicas: 1 the site is down meanwhile, so run at least two on different nodes for anything that must stay up.

How does a rolling update avoid downtime?

The Deployment grows a v2 ReplicaSet one pod at a time while the v1 one shrinks, and a v1 pod goes only once a new v2 pod passes readiness. This lesson's file sets maxSurge: 1 and maxUnavailable: 0. To go back, there's kubectl rollout undo.

How does the HPA work out how many pods?

⌈current pods × measured / target⌉, kept between the min and max you set: 4 pods at 90% against 60% gives 6. Within 10% of the target it changes nothing, and by default it waits 5 minutes before scaling down.

Why a Service? Can't I call a pod's IP directly?

Every new pod gets a new IP, so a direct call goes stale fast. A Service keeps one name and IP that don't change, picks pods by label and sends requests only to the ones that pass readiness.

How do a Deployment, a ReplicaSet and a Pod fit together?

The Deployment holds what you want (the image, how many, how to roll out) and makes one ReplicaSet per version. The ReplicaSet keeps the pod count right. The scheduler picks a node for each new pod, and that node's kubelet starts the container.

Does a small app need Kubernetes?

Often not. For one app on one server, a container with --restart=always and good backups gets you far. Kubernetes pays off with several services across more than one machine, and someone still has to run the cluster, or you pay a cloud to.

Read: is it worth it?

Next lesson

ServerlessLevel: Intermediate

Kubernetes still needs nodes someone looks after; next, serverless runs your code with no machines at all, and what that costs.