Skip to content

Intermediate · Kubernetes fundamentals · topic 20 of 53 · 30 min

Why Kubernetes, and how a cluster works

Explanation

Kubernetes is an orchestrator: it runs many containers across many machines, restarts them when they fail, scales them and updates them without downtime. A cluster has a control plane (the brain: API server, etcd, scheduler, controllers) and worker nodes (kubelet, kube-proxy and a container runtime). You describe the state you want in YAML, and controllers keep working until the cluster matches it.

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.
Think of it like this: Think of an air conditioner with a thermostat. You set 24 degrees (the desired state). You do not say "run the compressor for 10 minutes". The AC keeps measuring the room (the actual state) and switches on or off to close the gap. Kubernetes works the same way for containers.

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.

ComponentRuns onJob
kube-apiserverControl planeThe front door. kubectl and every other component talk to it over a REST API. It checks requests and stores objects.
etcdControl planeA key-value database that holds the whole cluster state. If you lose it without a backup, you lose the cluster.
kube-schedulerControl planePicks a node for each new pod, based on free CPU and memory and any rules you set.
kube-controller-managerControl planeRuns the controllers (Deployment, ReplicaSet, Node, Job and more) that push the actual state towards the desired state.
cloud-controller-managerControl planeTalks to the cloud provider: creates load balancers, notices deleted VMs. Only in clusters that run on a cloud.
kubeletEvery nodeThe agent on each node. It makes sure the containers of the pods placed on its node are running, and reports their status.
kube-proxyEvery nodeSets up network rules so that traffic to a Service reaches the right pods. Some network plugins replace it.
Container runtimeEvery nodeActually 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.

  1. You run kubectl apply -f deployment.yaml. kubectl sends the YAML to the API server.
  2. The API server checks it and stores it in etcd.
  3. The Deployment controller sees a new Deployment and creates a ReplicaSet.
  4. The ReplicaSet controller sees it needs 3 pods and creates 3 Pod objects.
  5. The scheduler sees 3 pods with no node and assigns each one to a node.
  6. The kubelet on each chosen node asks containerd to pull the image and start the container, then reports "Running".

kubectl: the everyday commands

bash
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?
kubectl reads the cluster address and your credentials from a kubeconfig file, usually ~/.kube/config.

Where to run a cluster

OptionWhat it isGood for
kindKubernetes in Docker: each node is a containerLearning, and testing in CI pipelines
minikubeA one-node cluster in a VM or container, with handy add-onsLearning on a laptop
k3dRuns k3s, a light Kubernetes distribution, in DockerFast local clusters with little memory
EKS, AKS, GKEManaged Kubernetes on AWS, Azure and Google CloudProduction: 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.

Careful: Common mistakes. (1) Running a command against the wrong cluster: always check kubectl config current-context before apply or delete, and give production a clearly different context name. (2) Deleting a pod to "stop" an app: its Deployment simply creates a new one; scale the Deployment or delete it instead. (3) Changing live objects with kubectl edit: the cluster then differs from the YAML in Git, and the next apply undoes your fix. Change the file, then apply it. (4) Forgetting -n: you look in the default namespace and think your pods are missing.
Tip: Start with kind or minikube on your laptop. Everything in this module works the same on a managed cluster.

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

Kubernetes fundamentals

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.

bash
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=12
What the on-call engineer sees the next morning, and the scale-up for match night.

Practice (0/3 done)

Task 1 · Question

Which component stores the whole state of a Kubernetes cluster?

Task 2 · Code · Python

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]}.

Task 3 · Practice

Lab: create a local cluster with kind (or minikube) and explore it with kubectl. Write down what each command showed you.

Quick quiz

Question 1

1.Which component decides which node a new pod should run on?

Question 2

2.What does the kubelet do?

Question 3

3.What does "declarative" mean in Kubernetes?

Question 4

4.A Deployment wants 3 replicas. You delete one of its pods by hand. What happens?

Question 5

5.Which command shows a pod's details including recent events like scheduling failures?

Read more (free)

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.

From Managed Kubernetes: EKS, AKS and GKE

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.

From Pods, ReplicaSets and Deployments

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.

From GitOps and platform engineering

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

  1. 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.
  2. Module 5 exam: Kubernetes fundamentals · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, CI/CD pipelines. 53 topics left in the course before the final exam.

← Previous: Image and container security