Lecture 5 of 1419 min3D modelFree lecture

Deployments, self-healing and scaling

Take the controls of a live cluster model. Crash pods, scale up and down, roll out a new version and fail a whole node — and watch Kubernetes reconcile back to the desired state every time. Then learn the settings that make it work in production.

In this lecture you will

  • Explain how ReplicaSets keep the desired number of pods running
  • Describe a rolling update and how to roll back
  • Configure liveness and readiness probes and explain the difference
  • Scale workloads manually and automatically with the Horizontal Pod Autoscaler

In the last lecture a Deployment turned into three running pods. Real systems are messier: processes crash, machines fail, traffic spikes, and new versions ship several times a day. This lecture puts you in control of a cluster model to see how Kubernetes handles each of these — and which settings you, as its operator, are responsible for.

Try it: break the cluster

The model below runs a Deployment named web with three replicas. Use the controls to cause trouble and watch the control loop respond:

3D modelSelf-healing, scaling and rolling updatesUse the controls · drag to rotate
Loading 3D model…

Simplified model: pods are placed on the least-loaded healthy node, and a rolling update adds one new pod before removing one old pod.

  • Crash a pod — the ReplicaSet sees two pods where three are desired and creates a replacement.
  • Scale 3 ↔ 5 — changing the desired count is all it takes; controllers add or remove pods.
  • Roll out v2 — new pods (amber) start one at a time; an old pod is removed only after a new one is running. The service never drops below capacity.
  • Fail node-3 — its pods are lost and recreated on the remaining nodes. Restore the node and new pods can be scheduled there again.

Deployments and ReplicaSets

A Deployment doesn’t manage pods directly. It manages ReplicaSets, each of which keeps a fixed number of identical pods — one specific version of your pod template — running. When you change the template, for example the image tag, the Deployment creates a new ReplicaSet and gradually shifts replicas from the old one to the new one.

That structure makes rollbacks simple, because the previous ReplicaSet still exists:

kubectl set image deployment/web web=shop-web:1.5.0   # start a rolling update
kubectl rollout status deployment/web                  # watch it progress
kubectl rollout undo deployment/web                    # go back if something is wrong

The pace of an update is controlled by maxSurge (how many extra pods may exist during the update) and maxUnavailable (how many may be missing). The model above uses one surge and zero unavailable — the safest setting for most services.

Try other settings, and switch off the readiness probe to see why the next section matters:

InteractivemaxSurge, maxUnavailable and readinessChange the two settings, then the probe
Loading visual…

Four replicas; a new pod needs one step to start serving. More surge finishes sooner at the cost of extra pods; more unavailability finishes sooner at the cost of capacity. Without a readiness probe, Kubernetes counts a starting pod as available and removes old pods too early.

Probes: telling Kubernetes what “healthy” means

A running process isn’t necessarily a working service. Kubernetes lets you define probes that the kubelet checks regularly:

Readiness probe
Can this pod serve traffic right now? Until it passes, the pod receives no requests from its Service. Essential for zero-downtime deploys.
Liveness probe
Is this pod stuck beyond recovery? If it keeps failing, the kubelet restarts the container.
Startup probe
Gives slow-starting applications time to boot before liveness checks begin.
readinessProbe:
  httpGet: { path: /healthz/ready, port: 80 }
  periodSeconds: 5
livenessProbe:
  httpGet: { path: /healthz/live, port: 80 }
  initialDelaySeconds: 20
  periodSeconds: 10

Keep the liveness check simple — “is the process responsive?” — and don’t make it depend on databases or other services. Otherwise a brief database hiccup can make Kubernetes restart every pod at once.

Scaling

Scaling a Deployment is just changing its desired replica count, either by hand — kubectl scale deployment/web --replicas=5 — or automatically:

  • The Horizontal Pod Autoscaler (HPA) adjusts the number of replicas to keep a metric, usually average CPU utilization relative to the pods’ CPU requests, near a target.
  • When new pods don’t fit on existing nodes, a cluster autoscaler (or a tool such as Karpenter on AWS) adds nodes, and removes them again when they are idle.

Watch an autoscaler handle a traffic spike. Change its target and how long it waits before scaling down:

InteractiveThe autoscaler meets a traffic spikeChange the target and the spike
Loading visual…

The HPA uses the documented rule desired = ceil(current × actual utilization ÷ target), ignores changes within 10%, and by default waits five minutes before scaling down. New pods need time to start, so a low target is headroom you pay for. Each pod here serves 100 requests per second at 100% of its CPU request.

Both depend on accurate resource requests. The scheduler uses requests to decide where pods fit, and the HPA measures utilization against them — so requests that are far too high waste money, and requests that are far too low cause crowded, slow nodes.

That completes the first module. You can now build images, run and connect containers, and explain how Kubernetes keeps applications running. Module 2 continues with Services and networking, configuration and secrets, ingress, Helm and observability.

Key takeaways

  • A Deployment manages ReplicaSets, which keep the specified number of identical pods running at all times.
  • Rolling updates replace pods gradually, so the service stays available; rollbacks return to the previous ReplicaSet.
  • Readiness probes decide when a pod gets traffic; liveness probes decide when it should be restarted.
  • The Horizontal Pod Autoscaler changes the replica count based on metrics such as CPU usage.

Check your understanding

3 questions · answers explained

Q1A pod in a Deployment with three replicas crashes. What restores the third replica?

The desired state still says three. The controller’s reconciliation loop sees the difference and creates a replacement pod, which the scheduler places on a healthy node.

Q2During a rolling update, what stops users being sent to new pods that are still starting up?

A pod only receives Service traffic once its readiness probe passes. Without one, traffic can hit an app that is still loading and cause errors during every deploy.

Q3What does the Horizontal Pod Autoscaler change?

The HPA adjusts a workload’s replica count to keep a metric — typically average CPU utilization — near a target. Adding nodes when pods don’t fit is the job of a cluster autoscaler.

You’ve finished the free lectures.

Continue with the remaining 9 lectures of Containers & Kubernetes — one-time purchase of $69₹4,499.