Intermediate · Kubernetes fundamentals · topic 20 of 53 · 30 min
Why Kubernetes, and how a cluster works
Explanation
Why do we need an orchestrator?
Running one container with docker run is easy. Now imagine a real company: 40 small services, each running 3 copies, spread over 10 servers. Questions appear at once. Which server has room for a new container? What happens when a server dies at 2 am? How do you update a service to a new version without stopping it? How do you go from 3 copies to 30 during a festival sale, and back again afterwards?
An orchestrator is software that answers these questions for you. You tell it what you want, and it decides where containers run, watches them, and fixes problems. Kubernetes (often written K8s, because there are 8 letters between the K and the s) is the most used orchestrator. Google started it in 2014, and it is now an open-source project of the Cloud Native Computing Foundation (CNCF).
- Self-healing: if a container crashes or a machine dies, Kubernetes starts a replacement somewhere else.
- Scaling: change a number from 3 to 30, and Kubernetes starts 27 more copies.
- Rollouts and rollbacks: update to a new image version a few copies at a time, and go back if it breaks.
- Service discovery and load balancing: give a group of copies one stable name and spread traffic across them.
- Bin packing: place containers on machines so that CPU and memory are used well.
The parts of a cluster
A cluster is a group of machines working together. Each machine is called a node; it can be a virtual machine in the cloud or a physical server. The control plane is the set of components that manage the cluster. Worker nodes are where your application containers actually run. In production the control plane runs on 3 or more machines, so one failure does not stop it.
| Component | Runs on | Job |
|---|---|---|
| kube-apiserver | Control plane | The front door. kubectl and every other component talk to it over a REST API. It checks requests and stores objects. |
| etcd | Control plane | A key-value database that holds the whole cluster state. If you lose it without a backup, you lose the cluster. |
| kube-scheduler | Control plane | Picks a node for each new pod, based on free CPU and memory and any rules you set. |
| kube-controller-manager | Control plane | Runs the controllers (Deployment, ReplicaSet, Node, Job and more) that push the actual state towards the desired state. |
| cloud-controller-manager | Control plane | Talks to the cloud provider: creates load balancers, notices deleted VMs. Only in clusters that run on a cloud. |
| kubelet | Every node | The agent on each node. It makes sure the containers of the pods placed on its node are running, and reports their status. |
| kube-proxy | Every node | Sets up network rules so that traffic to a Service reaches the right pods. Some network plugins replace it. |
| Container runtime | Every node | Actually starts and stops containers. Usually containerd or CRI-O. Images you built with Docker run fine. |
Desired state and reconciliation
Kubernetes is declarative. Declarative means you write down the result you want ("3 copies of payments-api version 1.4"), not the steps to get there. The opposite, imperative, means giving step-by-step commands ("start a container here, then another there").
Every controller runs a reconciliation loop, also called a control loop: observe the actual state, compare it with the desired state, act to close the gap, and repeat forever. If a pod dies, the next loop notices only 2 copies are running and creates a third. Nobody has to wake up.
- You run kubectl apply -f deployment.yaml. kubectl sends the YAML to the API server.
- The API server checks it and stores it in etcd.
- The Deployment controller sees a new Deployment and creates a ReplicaSet.
- The ReplicaSet controller sees it needs 3 pods and creates 3 Pod objects.
- The scheduler sees 3 pods with no node and assigns each one to a node.
- The kubelet on each chosen node asks containerd to pull the image and start the container, then reports "Running".
kubectl: the everyday commands
kubectl get nodes # list the machines in the cluster
kubectl get pods # pods in the current namespace
kubectl get pods -n payments # pods in the "payments" namespace
kubectl get pods -A # pods in all namespaces
kubectl describe pod payments-api-7d9c-x2k # details and recent events of one pod
kubectl apply -f deployment.yaml # create or update objects from a file
kubectl delete -f deployment.yaml # delete the objects in that file
kubectl logs payments-api-7d9c-x2k # the container's output
kubectl exec -it payments-api-7d9c-x2k -- sh # open a shell inside the container
kubectl get deployment payments-api -o yaml # the full object as YAML
kubectl config current-context # which cluster am I talking to?Where to run a cluster
| Option | What it is | Good for |
|---|---|---|
| kind | Kubernetes in Docker: each node is a container | Learning, and testing in CI pipelines |
| minikube | A one-node cluster in a VM or container, with handy add-ons | Learning on a laptop |
| k3d | Runs k3s, a light Kubernetes distribution, in Docker | Fast local clusters with little memory |
| EKS, AKS, GKE | Managed Kubernetes on AWS, Azure and Google Cloud | Production: the provider runs the control plane for you |
With a managed service you still manage your worker nodes (or pick a mode where the provider does that too) and your apps. You pay for the cluster and its nodes, so check the current pricing page before you leave a practice cluster running.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Why Kubernetes, and how a cluster works · 1/8
In this lesson you will learn why we need Kubernetes and how a cluster is built.
Prefer a video by a teacher? Find free videos on “kubernetes architecture explained control plane nodes for beginners” on YouTube ↗
In real life: A food delivery startup outgrows docker compose
A Bengaluru food delivery startup ran 12 services with docker compose on 4 VMs. One night a VM died during the dinner rush, and the orders service was down until an engineer woke up and restarted it by hand on another VM. After moving to a managed Kubernetes cluster, the same failure is handled in about a minute with nobody awake: the node is marked NotReady, the ReplicaSet controller notices fewer running pods, the scheduler places replacements on healthy nodes, and the kubelets start them. During the IPL final the team scales the orders service from 4 to 12 replicas with one command.
kubectl get nodes
# NAME STATUS ROLES AGE
# node-1 Ready <none> 40d
# node-2 NotReady <none> 40d <- the VM that died
# node-3 Ready <none> 40d
kubectl get pods -n orders -o wide # replacements now run on node-1 and node-3
kubectl scale deployment orders-api -n orders --replicas=12Practice (0/3 done)
Which component stores the whole state of a Kubernetes cluster?
Write one step of a reconciliation loop. reconcile(desired, pods) gets the desired number of replicas and a list of pods, each a dict like {"name": "web-a", "status": "Running"}. Pods with status "Running" or "Pending" are healthy; any other status must be deleted. If there are more healthy pods than desired, delete the extra ones from the END of the list. If there are fewer, create the missing number. Return {"create": n, "delete": [names in list order]}.
Lab: create a local cluster with kind (or minikube) and explore it with kubectl. Write down what each command showed you.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Pods, ReplicaSets and DeploymentsKubernetes fundamentals · same module
- Services, Ingress and the Gateway APIKubernetes fundamentals · same module
- Namespaces, ConfigMaps, Secrets, resources and probesKubernetes fundamentals · same module
- Managed Kubernetes: EKS, AKS and GKECloud Computing (AWS, Azure, Google Cloud) · Containers in the cloud
- Kubernetes securityDevSecOps
- GitOps and platform engineeringGitOps, platform engineering and DevOps careers
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which component decides which node a new pod should run on?
Answer: kube-scheduler
The scheduler watches for pods with no node assigned and picks a node based on resources and rules.
What does the kubelet do?
Answer: Makes sure the containers of pods assigned to its node are running
The kubelet is the agent on every node. It starts containers through the runtime and reports their status.
What does "declarative" mean in Kubernetes?
Answer: You describe the desired state and controllers make it happen
You write the result you want in YAML, and reconciliation loops keep the cluster matching it.
A Deployment wants 3 replicas. You delete one of its pods by hand. What happens?
Answer: A new pod is created to get back to 3
The ReplicaSet controller sees 2 of 3 and creates a replacement. That is self-healing.
Which command shows a pod's details including recent events like scheduling failures?
Answer: kubectl describe pod <name>
describe prints the object and the Events list at the bottom, which is the first place to look when something is wrong.
Which command points kubectl at an AKS cluster?Related
Answer: az aks get-credentials
az aks get-credentials writes the AKS cluster details into your kubeconfig.
What do containers in the same pod share?Related
Answer: A network identity (one IP) and optionally volumes
Containers in a pod share the network namespace, so they reach each other on localhost, and can mount the same volumes.
Which object replaced PodSecurityPolicy (removed in Kubernetes 1.25) for enforcing Pod Security Standards?Related
Answer: Pod Security Admission, configured with namespace labels
The built-in Pod Security Admission controller enforces privileged, baseline or restricted per namespace.
From Kubernetes security
Which two tools are graduated CNCF GitOps projects?Related
Answer: Argo CD and Flux
Argo CD and Flux both reconcile Kubernetes clusters from Git.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 4 more topics in “Kubernetes fundamentals” before its exam: Pods, ReplicaSets and Deployments, Services, Ingress and the Gateway API, Namespaces, ConfigMaps, Secrets, resources and probes, Helm and Kustomize.
- Module 5 exam: Kubernetes fundamentals · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, CI/CD pipelines. 53 topics left in the course before the final exam.