Search

Search papers and lessons

Kubernetes

Level 3 · 16 min read

Kubernetes runs containers across a cluster of machines by letting you declare the state you want and then running control loops that keep the real system moving toward it, even as machines fail.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Distributed systems
  • Linear algebra
  • Networking
  • Programming languages
  • ReadReady to readNot yet

TL;DR

Kubernetes is a system that runs many containers on a cluster of machines. You describe the state you want, such as "three copies of this web server", and Kubernetes keeps comparing that description with what is actually running. When a container crashes or a machine dies, it starts replacements elsewhere on its own. It builds on the Linux Containers that package each program and on Distributed Consensus, which keeps its record of the desired state safe.

Builds on: Container Image, Linux Containers, Distributed Consensus · Leads to: Cluster Scheduling, the placement problem it must solve each time it starts a workload

Where it's used

Web services at scale. A company serving millions of users runs each service as several identical containers spread across many machines. Kubernetes keeps the copies running, rolls out a new version one copy at a time, and moves work away from a failed machine, so the team describes the service once instead of logging into each server.

Batch and data jobs. Training runs for machine learning models and nightly data pipelines start many short-lived containers that need a lot of CPU or memory for a few hours. Kubernetes places each container where there is room and frees the machines when the job finishes.

Multi-cloud and on-premises setups. Because the description of a workload is the same wherever the cluster runs, a team can run the same deployment in its own data centre and in a public cloud, on physical servers or on virtual machines. Managed Kubernetes services in the big cloud providers run the control plane for the team and leave the workloads to the team.

How it works

Follow one tiny case from start to finish: a web service that should have three copies, and one copy has crashed.

  1. Declare the desired state. An operator submits a description that says "run three replicas (identical copies) of this container image". The Kubernetes API server accepts it and stores it. Check: the stored record says three, and it stays at three even when a copy later crashes.

  2. Commit the record safely. The API server writes the change to its store, etcd. etcd replicates the change across several machines with a consensus protocol, so every copy applies the same changes in the same order (State Machine Replication). Each write goes through one elected leader, and it counts only after a majority agrees, so the record survives the loss of one machine. Check: with three etcd members, two must agree, so one machine can fail without losing the record.

  3. Notice the difference. A controller called the replica-set controller watches the stored records. A pod is the smallest unit Kubernetes runs; here it is one container. The controller counts the pods that match the description and finds two running, one short of three. Check: the difference is desired minus actual, 3−2=13 - 2 = 1, so the controller needs to create one pod.

  4. Place the new pod. The scheduler picks a machine with enough free CPU and memory for the pod's requests, the amounts the pod declares it needs (one CPU is one core). Machine A has 1.0 CPU free and the pod requests 0.5, so the scheduler binds the pod to machine A. Check: after binding, machine A has 1.0−0.5=0.51.0 - 0.5 = 0.5 CPU free.

  5. Start the container. The kubelet, an agent on machine A, sees the new assignment, asks the container runtime (the software that actually runs containers) to pull the image and start it. It reports the pod's status back to the API server. Check: the status now shows two old pods plus one new pod, which is three.

  6. Stop when the states match. The controller sees three running pods and three desired, so the difference is zero and it does nothing. The loop keeps running, so it will act again the next time anything drifts. Check: nothing in this step changes the stored description, only the running state.

flowchart LR
  A[Operator writes desired state] --> B[API server stores it in etcd]
  B --> C[Controller compares desired with actual]
  C -->|difference| D[Scheduler binds pod to a node]
  D --> E[Kubelet starts container]
  E --> F[Status reported back]
  F --> C

The math

Step 3 in symbols: the control loop

Let DD be the number of replicas the operator asked for and AtA_t the number running at time tt. The error is

et=D−At.e_t = D - A_t .

The controller acts on the error each time it runs, the same pattern as a feedback loop. In the simplest form it creates max⁡(0,et)\max(0, e_t) pods, and it deletes max⁡(0,−et)\max(0, -e_t) pods when there are too many. Repeating this converges: once At=DA_t = D, the error is zero and the action is empty. The loop does not need to know why a pod is missing; it only needs the count, which is why the same mechanism handles crashes, deleted pods and a node that has disappeared.

Step 4 in symbols: the scheduling check

A node has capacity CC in CPU, and the pods placed on it request r1,…,rmr_1, \dots, r_m. A new pod with request rr fits when

∑i=1mri+r≤C.\sum_{i=1}^{m} r_i + r \le C .

This is a feasibility check. Which feasible node to pick is a separate scoring question, handled by Cluster Scheduling.

Step 5 in symbols: how many failures the record survives

Let the etcd cluster have nn members. A write needs a Quorum of

q=⌊n2⌋+1q = \left\lfloor \frac{n}{2} \right\rfloor + 1

members to agree, so the cluster tolerates n−qn - q failed members. Three members give q=2q = 2 and one tolerated failure. Five give q=3q = 3 and two tolerated failures. Four members give q=3q = 3 and still only one tolerated failure, so clusters use odd sizes: the extra member adds a vote to wait for without adding a failure the cluster can survive.

Worked examples

Example
1. One missing replica

The desired count is D=3D = 3 and the running count is A=2A = 2.

  1. Error: e=3−2=1e = 3 - 2 = 1.
  2. The controller creates max⁡(0,1)=1\max(0, 1) = 1 pod.
  3. The scheduler places it on a machine with room.
  4. Running count becomes 2+1=32 + 1 = 3.
  5. The next error is 3−3=03 - 3 = 0, so the controller does nothing until something changes.
Example
2. Fitting pods on one node

A node has 4 CPU. Three pods already placed there request 0.5, 1.0 and 1.5 CPU. Will a new pod requesting 1.5 CPU fit?

  1. Sum of existing requests: 0.5+1.0+1.5=3.00.5 + 1.0 + 1.5 = 3.0.
  2. The new pod's request plus that sum: 3.0+1.5=4.53.0 + 1.5 = 4.5.
  3. Compare with capacity: 4.5>44.5 > 4.
  4. The pod does not fit on this node, so the scheduler looks at another node.
  5. Check the other case as well: a pod requesting 1.0 CPU gives 3.0+1.0=4.0≤43.0 + 1.0 = 4.0 \le 4, so it does fit, and the node becomes exactly full.
Example
3. A realistic service on four nodes

A web service runs 12 replicas. Each replica requests 0.25 CPU and 256 MiB of memory. The cluster has four nodes, each with 4 CPU and 8 GiB (8,192 MiB) of memory.

  1. CPU per node: 4÷0.25=164 \div 0.25 = 16 replicas.
  2. Memory per node: 8,192÷256=328{,}192 \div 256 = 32 replicas.
  3. CPU is the tighter limit, so each node takes 16 replicas, and the cluster takes 4×16=644 \times 16 = 64.
  4. The 12 replicas fit with room to spare. They request 12×0.25=312 \times 0.25 = 3 CPU out of 1616 CPU in the cluster, about 19%19\%.
  5. Lose one node, and the remaining three still hold 3×16=483 \times 16 = 48 replicas, which is still more than 12. The control loop can restart the lost pods without any manual step.

Try it: each replica requests 1 CPU and 2 GiB, on the same four nodes. How many replicas fit? Answer: CPU gives 44 per node, memory gives 8÷2=48 \div 2 = 4 per node. Both limits are equal, so the cluster takes 4×4=164 \times 4 = 16 replicas.

Common mistakes and tradeoffs

It is tempting to think that Kubernetes builds or runs the containers itself. In fact it describes and places them. Building an image is a separate step, and a container runtime on each machine does the actual running. Kubernetes asks the kubelet to start a container, and the kubelet hands the work to the runtime.

It is tempting to think a pod has a permanent identity. In fact a pod is disposable: when the controller replaces it, the new pod has a new name and usually a new address. Clients therefore reach a group of pods through a service, which gives a stable name and spreads requests across the healthy pods behind it.

It is tempting to think the cluster has no single point of failure because it has many machines. In fact the control plane depends on its store. If etcd loses its Quorum, the cluster can keep running the containers it already has, but it cannot accept new changes until the quorum returns. This is why the store runs on several machines with a consensus protocol.

It is tempting to treat resource requests as a hard cap. In fact requests are what the scheduler uses to decide placement, and limits are the separate ceiling a container may not exceed. Setting requests too low packs machines too tightly, and the containers then compete when load rises.

Use Kubernetes when you have many services or jobs on several machines and want the same description to work across them. Do not use it for one small application on one machine: the operational cost is real, and a simpler platform or a single well-run server is often the better choice.

History

Containers existed on Linux before Kubernetes, but running them across many machines by hand did not scale. Large internal systems took on that job first, and Google's cluster manager, Borg, ran its production workloads for years before the public system appeared. Those internal systems shaped the same ideas that Kubernetes uses: declared state, control loops and placement by resource requests.

Google open-sourced Kubernetes in 2014, and version 1.0 followed in 2015, when it was handed to the Cloud Native Computing Foundation. The project caught on because it gave every cloud provider and company the same description format, and because the control-loop design made it tolerate failures that would stop a simpler script.

Recap

  • You declare the desired state, and the API server stores it in etcd, which commits each write by consensus.
  • Controllers run a loop that compares desired with actual and acts on the difference, which is why failures are repaired automatically.
  • The scheduler places each pod on a node whose free CPU and memory cover its requests.
  • Stable service names hide the replacement of individual pods.
  • The control plane's safety depends on the consensus store, and a quorum of ⌊n/2⌋+1\lfloor n/2 \rfloor + 1 members is needed to accept changes.

Read next: Cluster Scheduling for how the scheduler chooses among feasible nodes, and In search of an understandable consensus algorithm for the consensus protocol that etcd uses.

Check yourself

QuestionWhy does the controller compute the difference between desired and actual counts rather than remembering which pod it last created?Show answer

The difference is a function of the current state, so the loop works from what is running now. A crashed pod, a deleted pod or a vanished node all show up as a change in the count, and the controller repairs them the same way without needing a history of events.

QuestionAn etcd cluster has 5 members. How many can fail while writes still succeed, and what is the quorum?Show answer

The quorum is ⌊5/2⌋+1=3\lfloor 5/2 \rfloor + 1 = 3. The cluster tolerates 5−3=25 - 3 = 2 failed members.

QuestionA node has 2 CPU. Pods already there request 0.75 and 0.5 CPU. How much CPU is free, and does a pod requesting 1.0 CPU fit?Show answer

The used CPU is 0.75+0.5=1.250.75 + 0.5 = 1.25, so 2−1.25=0.752 - 1.25 = 0.75 CPU is free. A pod requesting 1.0 does not fit, because 1.25+1.0=2.25>21.25 + 1.0 = 2.25 > 2.

QuestionWhy should clients reach a service rather than a specific pod's address?Show answer

Pods are replaced when they fail or when a new version rolls out, and a replacement gets a new address. A service provides a stable name and spreads requests across whichever pods are currently healthy, so clients do not need to track each change.

QuestionA team says Kubernetes is the same as Docker because both run containers. What is the difference?Show answer

Kubernetes decides where containers run across many machines and keeps them at the declared count, while a container runtime on each machine starts and stops the containers. Kubernetes asks a runtime to do that work, so the two operate at different levels.

Sources