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.
-
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.
-
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.
-
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, , so the controller needs to create one pod.
-
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 CPU free.
-
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.
-
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 be the number of replicas the operator asked for and the number running at time . The error is
The controller acts on the error each time it runs, the same pattern as a feedback loop. In the simplest form it creates pods, and it deletes pods when there are too many. Repeating this converges: once , 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 in CPU, and the pods placed on it request . A new pod with request fits when
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 members. A write needs a Quorum of
members to agree, so the cluster tolerates failed members. Three members give and one tolerated failure. Five give and two tolerated failures. Four members give 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
The desired count is and the running count is .
- Error: .
- The controller creates pod.
- The scheduler places it on a machine with room.
- Running count becomes .
- The next error is , so the controller does nothing until something changes.
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?
- Sum of existing requests: .
- The new pod's request plus that sum: .
- Compare with capacity: .
- The pod does not fit on this node, so the scheduler looks at another node.
- Check the other case as well: a pod requesting 1.0 CPU gives , so it does fit, and the node becomes exactly full.
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.
- CPU per node: replicas.
- Memory per node: replicas.
- CPU is the tighter limit, so each node takes 16 replicas, and the cluster takes .
- The 12 replicas fit with room to spare. They request CPU out of CPU in the cluster, about .
- Lose one node, and the remaining three still hold 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 per node, memory gives per node. Both limits are equal, so the cluster takes 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 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 . The cluster tolerates 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 , so CPU is free. A pod requesting 1.0 does not fit, because .
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
- Kubernetes, Wikipedia: https://en.wikipedia.org/wiki/Kubernetes
- Kubernetes documentation: https://kubernetes.io/