Course contents

14. Cloud and Infrastructure Lesson 3 of 8

Kubernetes

Watch the control plane turn a Deployment into pods, a Service route only to ready pods, a crash healed in place and a lost node rescheduled.

16 minIntermediate7 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • You store desired state through the API server; etcd holds it; controllers converge actual state towards it.
  • The scheduler binds pods to nodes; the kubelet on each node starts and restarts containers.
  • A Service is a stable name and virtual IP whose endpoints are the Ready pods.
  • Readiness gates traffic; liveness restarts containers; neither replaces capacity planning.
  • Node failures are detected slowly and need spare room elsewhere for replacement pods.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Kubernetes is an open-source system that runs containers across a cluster of machines. You describe what should run (a container image, how many copies, how to reach them, what resources they need) as objects stored through an API, and Kubernetes keeps the cluster matching that description: it places containers on machines, restarts them when they crash, replaces them when machines fail and routes traffic to the healthy ones.

Why it exists

With more than a handful of hosts and services, placing containers by hand, restarting them, rolling out new versions and keeping load balancers up to date becomes error-prone toil. Kubernetes turns those jobs into control loops that run continuously, and gives every team the same API for deploying, scaling and exposing services, whichever cloud or data centre the cluster runs in.

How it works

Control plane. The API server is the only entry point: it authenticates, authorises and validates every request and stores objects in etcd, a consistent key-value store that uses the Raft consensus protocol. The scheduler watches for pods without a node and binds each to a node that has enough requested CPU and memory, respects constraints (affinity, taints, topology spread) and scores best. The controller manager runs controllers: the Deployment controller manages ReplicaSets, the ReplicaSet controller keeps N pods, the node lifecycle controller watches node heartbeats, and so on. All of them watch and update objects through the API server; none talks to etcd directly.

Nodes. Each worker node runs the kubelet, which watches for pods bound to its node, asks the container runtime (containerd, CRI-O) to start them, runs their probes and reports status. A network proxy (kube-proxy with iptables, IPVS or nftables rules, or an eBPF data plane such as Cilium) implements Services on every node.

Objects.

  • A Pod is one or more containers that share a network namespace and volumes, scheduled together on one node. Pods are disposable; each new one gets a new IP.
  • A Deployment declares a pod template and a replica count; it manages ReplicaSets and performs rolling updates between them.
  • A Service gives a set of pods (selected by labels) a stable DNS name and virtual IP. Its EndpointSlices list only pods whose Ready condition is true.

Probes. A readiness probe decides whether a pod receives traffic: while it fails, the pod is removed from endpoints, nothing else happens. A liveness probe decides whether a container is stuck: when it fails, the kubelet restarts the container. A startup probe holds off the other two while a slow app starts.

Healing. If a container exits, the kubelet restarts it in the same pod, with exponential back-off for repeated crashes (CrashLoopBackOff). If a node stops sending heartbeats, the node lifecycle controller marks it NotReady after a grace period (tens of seconds by default), its pods are marked not ready and removed from endpoints, and after a toleration period (five minutes by default) they are evicted; the owning ReplicaSet then creates replacements, which the scheduler places on other nodes.

When to use it

  • Many services, teams or environments that benefit from one deployment and operations model.
  • Workloads that scale horizontally and must survive machine failures without manual work.
  • Platforms where you want declarative, reviewable infrastructure (GitOps) and a large ecosystem of tooling.

When not to

  • A few services that a managed container platform, a PaaS or serverless functions run with far less to operate.
  • Teams without the capacity to run (or pay for) a cluster and learn its failure modes.
  • Latency-critical or specialised workloads that need tight control over hardware, unless you invest in tuning.

Trade-offs

Choice You gain You pay
Kubernetes instead of VMs and scripts Automated placement, healing, rollouts, one API Operational complexity, many moving parts to learn
Managed control plane (EKS, GKE, AKS) No etcd or API servers to run Cost, provider-specific add-ons and upgrade cadence
Readiness probes No traffic to pods that cannot serve A badly designed probe can remove every pod at once
Liveness probes Recover from deadlocks automatically Restart loops if the probe is too strict or too slow
Many small pods Fine-grained scaling, small blast radius More scheduling, networking and per-pod overhead

Alternatives

  • Managed container services (Cloud Run, ECS/Fargate, Azure Container Apps): run containers without managing a cluster.
  • Serverless functions: event-driven code without containers or nodes to size.
  • Nomad or plain VMs with autoscaling groups: simpler schedulers or no scheduler at all.
  • PaaS (Heroku-style platforms): push code, the platform builds and runs it.

Failure modes

  • No readiness probe: pods join the Service while still starting and fail requests on every rollout and scale-out.
  • Liveness probe on a dependency: a database blip makes every pod's liveness fail and the kubelet restarts all of them.
  • Slow node failure detection: traffic keeps reaching dead pods until the node is marked NotReady.
  • No spare capacity: replacement pods stay Pending because no node has room for their requests.
  • All replicas on one node or zone: one failure takes the whole service down; use topology spread or anti-affinity.
  • Control plane outage: running pods keep running, but nothing can be scheduled, scaled or healed until it is back.

In production

Use a managed control plane or run several API servers with a three- or five-member etcd. Spread replicas across nodes and zones, set resource requests from measurements, and keep enough headroom to absorb a node loss. Add readiness probes that check what the pod itself needs, liveness probes only for real deadlocks, and PodDisruptionBudgets so maintenance does not drain too many pods at once. Watch pending pods, restart counts, node conditions and Service endpoint counts.

Common misconceptions

  • "kubectl apply deploys my app." It stores desired state; the rollout happens afterwards and can fail.
  • "A crashed pod moves to another node." Its containers are restarted in place; only node loss or eviction leads to new pods elsewhere.
  • "Readiness failures restart my container." Readiness only removes the pod from endpoints; liveness restarts.
  • "Kubernetes makes my service highly available." Only if there are enough replicas, spread over failure domains, with capacity to spare.

Interview angle

Draw the control loop: API server and etcd hold desired state, controllers and the scheduler converge, kubelets act on nodes. Explain how a Service finds pods (labels, endpoints, Ready condition) and the difference between readiness and liveness. Walk through a node failure: detection, eviction, rescheduling and what users see meanwhile, then size the cluster for N+1 nodes. Mentioning that only the API server talks to etcd, and that apply is asynchronous, shows real understanding.

Model assumptions

This lesson uses a simplified model, not a benchmark. Pods are simulated servers with 4 workers and a 50 ms mean service time (lognormal, cv 0.35), about 80 req/s each; traffic is Poisson. The Service picks a ready pod at random. Its health check stands for readiness probes plus the endpoint update: every 250 ms, 2 failures remove a pod, 2 successes add it. The control plane (API server, etcd, scheduler, controllers, kubelets) is a scripted illustration: its messages are scripted packets, not simulated watches. A pod appears in the scene when its Pod object is created and stays offline until its container starts; evicted pods disappear. Pod starts, crashes, restarts, node loss and eviction are timed actions on a compressed timeline; real detection and eviction take tens of seconds to minutes. Utilisation is the fraction of busy workers.

The lesson as textEvery step of the simulation, written out.
  1. 1. You declare what you want; controllers make it so

    You do not tell Kubernetes *how* to start containers. You kubectl apply a Deployment that says *what* you want: three replicas of the web pod. The API server validates it and stores it in etcd, and answers you; nothing has run yet.

    Then watchers react, always through the API server: the controllers create three Pod objects, the scheduler binds each one to a node, and the kubelet on each node starts the containers. About 2 s in the pods are running, and after two good readiness probes they are Ready. No component talks to etcd except the API server, and the scheduler never talks to a kubelet directly.

    Scene: kubectl sends a Deployment to the API server, which stores it in etcd and replies. The controllers create three pods, the scheduler binds them to Node 1 and Node 2, and the kubelets start them; the pods become ready after two readiness probes.

    Key idea: Kubernetes is a set of control loops around one API: you store desired state, controllers converge actual state towards it.

  2. 2. A Service: one stable address for changing pods

    Pods come and go and get new IP addresses each time. A Service gives them one stable name and virtual IP. Its endpoint list holds only the Ready pods, and the node’s proxy (kube-proxy, or an eBPF data plane) sends each connection to one of them, here picked at random.

    Users now send 120 req/s. Each of the three pods gets roughly a third and runs about half busy; nothing fails.

    Scene: Users send 120 requests per second to the Service, which spreads them over the three ready pods, each about half busy, with no errors.

    Key idea: Clients address the Service, never pods; the Service tracks which pods are ready.

  3. 3. Scale to four: traffic waits for Ready

    You run kubectl scale --replicas=4. The same chain runs again: controller, scheduler, kubelet on Node 2. Pod 4’s container is running at 0.4 s, but a running process is not necessarily ready to serve: it may still be loading config, warming caches or opening connections.

    The readiness probe decides. Only after two successful probes is pod 4 added to the Service’s endpoints. Until then it receives no traffic, so nobody hits a half-started pod, and the error rate stays at zero.

    Scene: The deployment is scaled to four. Pod 4 starts on Node 2 at 0.4 seconds and receives traffic only after two successful readiness probes; no request fails.

    Key idea: Readiness gates traffic: a pod joins the Service only when its probe says it can serve.

  4. 4. A container crashes; the kubelet restarts it

    At 0.8 s pod 2’s container crashes. The requests it was handling fail, and until readiness takes it out of the endpoints a few more requests reach it and are refused.

    The kubelet on Node 1 notices the exit and restarts the container in the same pod (restart policy Always), here at 1.5 s. It is not rescheduled anywhere. Once two probes pass again, pod 2 is back in the Service. If it kept crashing, the kubelet would back off longer and longer between restarts (CrashLoopBackOff).

    Scene: Pod 2’s container crashes at 0.8 seconds; some requests fail until readiness removes it from the Service. The kubelet restarts it in place at 1.5 seconds and it rejoins after two good probes.

    Key idea: Container crashes are healed locally by the kubelet; the pod stays on its node.

  5. 5. A whole node disappears

    At 0.6 s Node 1 loses power: pods 1 and 2 vanish together, and its kubelet can no longer report. Requests in flight there fail, and so do new ones until both pods are removed from the endpoints.

    In a real cluster that removal waits for the node controller to notice missing heartbeats (a grace period of tens of seconds by default) and mark the pods not ready; here it is compressed. Afterwards pods 3 and 4 carry all 120 req/s at about 75% busy. They cope, but there is little headroom left.

    Scene: Node 1 fails at 0.6 seconds and pods 1 and 2 disappear. Requests sent to them fail until they are removed from the Service; then pods 3 and 4 serve everything at about 75% busy.

    Key idea: A node failure takes all its pods at once; detection is slower than a container crash because nobody on the node can report it.

  6. 6. The ReplicaSet notices, the scheduler places new pods

    Once the pods on the lost node are evicted (by default after about five minutes of NotReady; pods 1 and 2 disappear from the scene), the ReplicaSet controller sees 2 pods where 4 are desired and creates two new Pod objects. The scheduler places them on Node 3, which has the most room, and its kubelet starts them at about 0.6 s.

    After their readiness probes pass, pods 5 and 6 join the Service and the load spreads over four pods again, each well under half busy. No request fails during the repair, because traffic only moves to pods that are ready. Nobody ran a command: the control loop compared desired with actual and acted.

    Scene: The controller creates two replacement pods, the scheduler binds them to Node 3 and its kubelet starts them. After their readiness probes pass they join the Service; four pods share the traffic and no request fails.

    Key idea: Self-healing is reconciliation: controllers keep creating pods until actual matches desired; the scheduler decides where.

  7. 7. What Kubernetes does, and what it does not

    Kubernetes stores desired state behind one API and runs control loops that converge towards it: Deployments manage ReplicaSets, ReplicaSets keep N pods, the scheduler places pods, kubelets run and restart containers, Services route only to ready pods.

    It does not make your app correct, size your cluster or guarantee spare capacity. Plan for losing a node (spread replicas across nodes and zones, keep headroom, use PodDisruptionBudgets for maintenance), and keep the control plane itself highly available: a managed control plane or several API servers with a three- or five-member etcd.

    Scene: Four ready pods on Nodes 2 and 3 serve traffic through the Service; Node 1 is gone. The control plane is highlighted.

    Key idea: Kubernetes automates placement and repair; capacity planning and failure domains are still your design.