Skip to content
LF Cert Trainer

Lesson 3 · Section 2: Core Concepts

Lesson 3: Core concepts: how Kubernetes thinks

The mental model behind every other CKA topic: objects that record what you want, one API that everything goes through, and controllers that keep making reality match. · 20 min read · 4 min video

1/10Core concepts
0:00 / 3:53

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why these concepts come first

Suppose you ran your application with plain commands: start three containers on three servers, and you are done. The commands ran once and then stopped caring. When a server dies at three in the morning nothing starts the lost container somewhere else, and nothing even remembers that three were supposed to be running. Operating by commands means a human, or a pile of scripts, has to notice every failure and know every repair.

Kubernetes turns this around. You do not tell it what to do; you tell it what should exist: "three replicas of this image, reachable on port 80". It writes that down, and from then on its components work, without ever finishing, to make the real world match what was written. A crashed container, a dead node and a typo fixed by you are all handled the same way: there is a difference between what is wanted and what exists, so something acts to remove it.

That is why this lesson comes before the components and the objects themselves. If you know the model, most behaviour can be predicted instead of memorised, and most troubleshooting becomes one question: whose job was the next step, and why did it not happen?

What Kubernetes is made of

Everything goes through the API serverkubectlsavewatch, writewatchwatch, reportstartsYoukubectlAPI serverthe only way inetcdstores every objectControllersfix the differencesSchedulerpicks a nodekubeleton every nodeContainersthe real workload
Everything goes through the API server. You and every component of the cluster are clients of one API. Nothing talks to etcd directly, and the components never call each other: they read and write objects, and react to what the others wrote.

Parts:

  • You (kubectl)
  • API server (the only way in)
  • etcd (stores every object)
  • Controllers (fix the differences)
  • Scheduler (picks a node)
  • kubelet (on every node)
  • Containers (the real workload)

Connections:

  • You to API server: kubectl
  • API server and etcd: save
  • Controllers to API server: watch, write
  • Scheduler to API server: watch
  • kubelet to API server: watch, report
  • kubelet to Containers: starts

Three ideas carry the whole system:

  • Objects. An object is a record of intent stored by the cluster: a Pod, a Deployment, a Service, a Node, a Namespace. The cluster's state is nothing more than the set of all its objects.
  • One API. Objects are created, read, changed and deleted only through the API server. kubectl is an ordinary client of it, and so is every component of Kubernetes. The API server is the only process that talks to etcd, the database that stores the objects.
  • Controllers. A controller is a loop that watches some kind of object and makes changes until reality matches it. Most run inside the kube-controller-manager; the scheduler and the kubelet on each node follow the same pattern for their own part of the work.

The machines are split the same way. The control plane (API server, etcd, controller manager, scheduler) decides; the worker nodes (kubelet, container runtime, kube-proxy) run the containers. The next lessons take these components one at a time. For now, notice the shape: a hub with spokes, and no arrows between the spokes.

Every object has the same four parts

apiVersion: apps/v1          # which API group and version describes this object
kind: Deployment             # what kind of object it is
metadata:                    # who it is: name, namespace, labels, annotations
  name: web
  namespace: shop
  labels:
    app: web
spec:                        # desired state: written by you
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: nginx:1.29
status:                      # observed state: written by Kubernetes, never by you
  replicas: 3
  readyReplicas: 2
PartWho writes itWhat it answers
apiVersion and kindYouWhat type of object is this, and which schema checks it?
metadataYou, plus fields the system adds (uid, resourceVersion, creationTimestamp, ownerReferences)Which object is this, and how can others find it?
specYou (or a controller acting for you)What state do you want?
statusThe controller or kubelet responsible for the objectWhat state was last observed?

A few objects that only hold data, such as ConfigMaps and Secrets, have data instead of spec and no status: there is nothing to reconcile, only something to read.

apiVersion trips people up because of one historical accident. The oldest kinds (Pod, Service, Namespace, Node, ConfigMap, Secret) live in the unnamed core group and are written v1. Everything newer lives in a named group and is written group/version: apps/v1 for Deployments, batch/v1 for Jobs, networking.k8s.io/v1 for NetworkPolicies.

How it works: the reconciliation loop

The reconciliation loopwants 3has 2creates 1 PodreportsDesired statespec: replicas 3Controllercompare, then actThe clusterPods that existActual statestatus: 2 ready
The reconciliation loop. A controller compares what the object asks for with what exists, makes one change to close the gap, and looks again. The loop never ends, which is why the cluster repairs itself.

Parts:

  • Desired state (spec: replicas 3)
  • Controller (compare, then act)
  • The cluster (Pods that exist)
  • Actual state (status: 2 ready)

Connections:

  • Desired state to Controller: wants 3
  • Actual state to Controller: has 2
  • Controller to The cluster: creates 1 Pod
  • The cluster to Actual state: reports

The documentation compares a controller to a thermostat: you set a temperature, it measures the room, and it switches the heating on or off until the two agree. A Kubernetes controller does the same with objects:

  1. Observe. Watch the objects it is responsible for, and the things they own.
  2. Compare. Does what exists match the spec?
  3. Act. If not, make a change, usually by creating, updating or deleting other objects through the API.
  4. Repeat, forever.

The controller for ReplicaSets, for example, counts the Pods that match the ReplicaSet's selector. Two Pods where replicas: 3 is wanted means it creates one. Four means it deletes one. It does not care why the number is wrong, which is exactly what makes it robust: a crash, a drained node and a person running kubectl delete pod all look the same to it.

One command, followed end to end

One command, five independent actorscreatesseen bycreatesseen bycreatesseen bybindsstartsYoukubectl createDeployment webreplicas: 3Deployment ctrlcontrollerReplicaSetweb-7c5d9f6b8ReplicaSet ctrlcontroller3 PodsPending, no nodeSchedulersets nodeNamekubeleton the chosen nodeContainersPods now Running
One command, five independent actors. Follow the arrows from You to the running containers. Each actor only notices an object in the API that needs its attention and does its one job; nobody runs the whole sequence.

Parts:

  • You (kubectl create)
  • Deployment web (replicas: 3)
  • Deployment ctrl (controller)
  • ReplicaSet (web-7c5d9f6b8)
  • ReplicaSet ctrl (controller)
  • 3 Pods (Pending, no node)
  • Scheduler (sets nodeName)
  • kubelet (on the chosen node)
  • Containers (Pods now Running)

Connections:

  • You to Deployment web: creates
  • Deployment web to Deployment ctrl: seen by
  • Deployment ctrl to ReplicaSet: creates
  • ReplicaSet to ReplicaSet ctrl: seen by
  • ReplicaSet ctrl to 3 Pods: creates
  • 3 Pods to Scheduler: seen by
  • Scheduler to kubelet: binds
  • kubelet to Containers: starts

When you run kubectl create deployment web --image=nginx:1.29 --replicas=3, this is everything that happens:

  1. kubectl sends a Deployment object to the API server. The API server checks who you are, whether you may do this, and whether the object is valid, then saves it in etcd and answers deployment.apps/web created. Nothing is running yet.
  2. The Deployment controller is watching Deployments. It sees one with no ReplicaSet and creates a ReplicaSet with the same Pod template.
  3. The ReplicaSet controller is watching ReplicaSets. It sees one that wants three Pods and has none, and creates three Pod objects. They are Pending: they exist in the API but have no node.
  4. The scheduler is watching for Pods whose spec.nodeName is empty. It picks a node for each and records the choice (this is called binding).
  5. The kubelet on each chosen node is watching for Pods bound to its node. It asks the container runtime to pull the image and start the containers, then reports the result in the Pod's status.
  6. The status flows back up: the ReplicaSet counts ready Pods, the Deployment counts ready replicas, and kubectl get deployment web shows 3/3.

No component told another what to do. Each one noticed an object that needed its attention and did its single job. That is also why the system keeps working when a piece restarts: when the scheduler comes back, the unscheduled Pods are still sitting in the API, waiting to be noticed.

Under the hood · Why the loops react to state, not to events

Kubernetes controllers are level-triggered: they act on what the state is, not on a notification that something happened. An event-driven design ("a Pod died, so start one") breaks if the message is lost or the controller was down at the wrong moment. A state-driven design ("there are two, there should be three") gives the right answer whenever it runs, however many changes it missed.

In practice a controller opens a watch on the API server, a long-lived request that streams every change, and keeps a local cache. The watch only makes the loop fast; correctness comes from comparing full state. This is why controllers can be restarted, upgraded or moved to another control plane node without losing work.

Three metadata fields make this cooperation safe:

  • resourceVersion changes on every write. An update that carries a stale version is rejected with a conflict instead of silently overwriting someone else's change (the "object has been modified" error you sometimes see from kubectl edit).
  • generation goes up when the spec changes, and a controller copies the value it has dealt with into status.observedGeneration. If the two differ, the controller has not caught up with your latest change yet, and the rest of the status still describes the old spec.
  • ownerReferences point from an object to the object that created it. A Pod's owner is its ReplicaSet; the ReplicaSet's owner is its Deployment. When you delete an owner, the garbage collector deletes its dependents.
Under the hood · What the API server does with a request

Every write passes the same gates, in order: authentication (who are you?), authorization (may you do this? usually RBAC), admission (plugins that may change or reject the object, for example by filling in defaults or enforcing a quota), validation against the schema for that apiVersion and kind, and only then storage in etcd. A rejection at any gate is immediate and the error says which one, so read it: Unauthorized is authentication, Forbidden is authorization or admission, and is invalid or unknown field is validation.

The URL of an object follows from its manifest. A Deployment web in namespace shop lives at /apis/apps/v1/namespaces/shop/deployments/web; a Pod, being in the core group, at /api/v1/namespaces/shop/pods/<name>. Run any command with -v=6 to see the requests kubectl makes.

The objects you meet first

Each of these gets its own lesson. What matters here is the pattern: every row is a spec you write and a component that makes it true.

ObjectWhat you declareWho makes it true
PodOne or more containers that run together on one nodeScheduler (chooses the node), kubelet (runs it)
ReplicaSet"Keep N copies of this Pod template"ReplicaSet controller
Deployment"Run this version, N copies, and roll changes out gradually"Deployment controller, through ReplicaSets
Service"One stable name and IP for the Pods with these labels"EndpointSlice controller and kube-proxy
NamespaceA named compartment for objectsNamespace controller (cleans up on delete)
NodeA machine that can run PodsIts kubelet registers it and reports its health

Two more ideas tie the objects together:

  • Labels and selectors are the glue. Objects do not hold lists of each other. A ReplicaSet finds its Pods, and a Service finds its backends, by a label selector such as app: web. Change a Pod's label and it leaves the group at once: the ReplicaSet starts a replacement and the Service stops sending it traffic.
  • Namespaced or cluster-scoped. Most objects live inside a namespace, and their name only has to be unique there. A few describe the cluster itself and belong to no namespace: Nodes, Namespaces, PersistentVolumes, StorageClasses, ClusterRoles.

Using it

Example · Watch the loop repair a deleted Pod

Create a Deployment, break it on purpose, and watch the ReplicaSet controller fix it. Use two terminals, or run the watch first and press Ctrl+C when you have seen enough.

kubectl create deployment web --image=nginx:1.29 --replicas=3
kubectl get pods -l app=web --watch          # leave this running

# in a second terminal
kubectl delete pod -l app=web --wait=false   # delete all three at once

The watch shows three Pods Terminating and three new ones going Pending, ContainerCreating, Running, within a second or two of each other. You asked for the Pods to be deleted and they were; but the ReplicaSet still says three, so three exist again. kubectl create deployment labels everything app=<name>, which is why the selector -l app=web works.

Example · Compare what you asked for with what you have

Scale to more replicas than the cluster can start instantly and read both halves of the object:

kubectl scale deployment web --replicas=6
kubectl get deployment web -o jsonpath='{.spec.replicas} wanted, {.status.readyReplicas} ready{"\n"}'
kubectl get deployment web            # READY shows ready/wanted, for example 4/6
kubectl rollout status deployment web # waits until status matches spec

kubectl scale only changed one number in the spec. Everything after that was the controllers catching up, and status is where you watch them do it.

Example · Follow the chain of owners

Start from a Pod and walk up to the object you would actually have to change or delete:

POD=$(kubectl get pods -l app=web -o jsonpath='{.items[0].metadata.name}')
kubectl get pod "$POD" -o jsonpath='{.metadata.ownerReferences[0].kind}/{.metadata.ownerReferences[0].name}{"\n"}'
# ReplicaSet/web-7c5d9f6b8
kubectl get replicaset web-7c5d9f6b8 -o jsonpath='{.metadata.ownerReferences[0].kind}/{.metadata.ownerReferences[0].name}{"\n"}'
# Deployment/web
kubectl delete deployment web         # removes the ReplicaSet and the Pods with it

kubectl describe pod shows the same thing on its Controlled By: line. The names tell the story too: web-7c5d9f6b8-4xk2p is Deployment web, ReplicaSet hash 7c5d9f6b8, Pod suffix 4xk2p.

Tricks

Common mistakes

  • Treating "created" as "running". deployment.apps/web created only means the object passed validation and was stored. Always check the result: kubectl get, then kubectl describe if it is not what you expect.
  • Deleting Pods to stop an application. The owner recreates them. Delete or scale the Deployment instead; if Pods keep coming back, find their owner with kubectl describe pod (Controlled By:).
  • Fixing a Pod instead of its template. Editing a Pod that a ReplicaSet owns changes one copy, and the change is lost with that Pod. Change the Pod template in the Deployment so that every replacement gets it.
  • Trying to set status. Status in a manifest you apply is ignored. If the status is wrong, the fix is in the spec or in the component that reports it.
  • The wrong apiVersion. apiVersion: v1 with kind: Deployment fails with no matches for kind. Core kinds are v1; the rest carry their group. Check with kubectl api-resources or kubectl explain deployment.
  • Forgetting the namespace. "Not found" very often means "in another namespace". Add -n <namespace>, or -A to search all of them.

Recap

  • You describe what should exist as objects; Kubernetes works out the steps and keeps doing so.
  • Every object has apiVersion, kind, metadata, your spec (desired) and the cluster's status (observed).
  • Everything, including Kubernetes itself, goes through the API server; only it talks to etcd.
  • Controllers, the scheduler and the kubelet each watch for their own work, act, and look again, forever.
  • Stuck? Find the last step that happened and ask whose job the next one was. The Events usually say why.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.