Course contents · Lesson 8 of 22

Kubernetes Architecture: Control Plane and Nodes

Kubernetes Concepts · Lesson 8 of 22 · 10 min Engineer

In the last lesson, kubectl apply replied with one line, and moments later three pods ran on machines you never logged into. Something chose those machines and something started the containers. This lesson opens up the Kubernetes architecture behind that: the control plane that decides, the worker nodes that do the work, and the path one request takes between them.

The control plane: the head office

A cluster has two halves. The control plane stores the desired state and decides what runs where; the worker nodes are the machines that run your pods. In the diagram, notice that every line ends at one box: the API server.

Architecture diagram. You, with kubectl, send requests over HTTPS to the API server, kube-apiserver, the front door to every request. It sits in the control plane box, the head office, with etcd, the store of all cluster state, kube-scheduler, which picks a node per pod, and kube-controller-manager, which runs the control loops; each of these connects only to the API server. Below, two worker node boxes, the branches, each hold a kubelet that starts pods, containerd as the runtime, kube-proxy for Service rules, and two pods with one container each. Each node links to the API server to watch for work and report status
Everything meets at the API server, and only the API server touches etcd.

The kube-apiserver, or API server, is the front desk. It serves the Kubernetes API (application programming interface): the requests the cluster understands, such as "create this Deployment". Every request, from you or another component, arrives here over HTTPS. The API server checks who is asking and whether they are allowed, validates the object and saves it.

etcd holds the record books. It is a key-value store, a database that saves each value under a unique name, its key, and holds all cluster state. Only the API server reads or writes it. Lose its data without a backup and the cluster forgets every object, so production clusters run etcd on three or five machines that keep copies in sync.

The kube-scheduler watches for new pods that have no node yet. It filters out nodes that cannot take a pod, for example those without enough CPU or memory left for what the pod asks for, scores the rest and picks the best. It records the choice through the API server; it never starts a container.

The kube-controller-manager runs the controllers from lesson 7, each watching one kind of object, such as Deployments or nodes, and closing the gap between desired and actual state. Many controllers are bundled into this one program.

Worker nodes: the branches

A worker node is a machine, physical or virtual, that runs pods. Its branch manager is the kubelet, an agent on every node. It watches the API server for pods assigned to its node, has the container runtime start their containers, restarts containers that crash and reports each pod's status back.

kube-proxy keeps each node's network rules up to date, for example iptables rules, the Linux kernel's packet-forwarding rules, so that traffic sent to a Service (a stable address in front of changing pods, lesson 11) reaches one of its pods. The kernel forwards the traffic; kube-proxy only writes the rules.

The container runtime from lesson 1 creates the containers. Common choices are containerd, which Docker Engine also uses underneath, and CRI-O. The kubelet talks to the runtime through the Container Runtime Interface (CRI). Kubernetes 1.24 dropped built-in support for Docker Engine, yet images built with Docker still run, because they follow the OCI standard from lesson 2.

Tools such as kind, which you install in lesson 17, run the control plane itself as pods. You can list them, with kube-proxy, in the kube-system namespace, a named group of objects inside a cluster (lesson 16). Read along for now; the -n flag picks the namespace:

kubectl get pods -n kube-system

On a one-node kind cluster named lab, you should see something like this. It is trimmed: two coredns pods (the cluster's DNS server) and a kindnet pod (pod networking) are left out, and your suffixes and ages will differ.

NAME                                        READY   STATUS    RESTARTS   AGE
etcd-lab-control-plane                      1/1     Running   0          12m
kube-apiserver-lab-control-plane            1/1     Running   0          12m
kube-controller-manager-lab-control-plane   1/1     Running   0          12m
kube-proxy-9vz7c                            1/1     Running   0          12m
kube-scheduler-lab-control-plane            1/1     Running   0          12m

The kubelet and containerd are missing because they are not pods: they run as ordinary services on each node, since something has to start pods in the first place. With no cluster to talk to, kubectl ends with The connection to the server localhost:8080 was refused instead.

The control loop: observe, compare, act

Every controller, and the kubelet too, runs a control loop: observe the actual state, compare it with the desired state, act to close any gap, then start again, like a thermostat. Nobody hands a controller orders. Each one watches the API server, which streams it a notice whenever an object it cares about is created, changed or deleted.

In the flowchart, a controller keeps three copies of a pod. It acts only by creating or deleting Pod objects through the API server; the kubelets then start or stop the containers. Because it compares instead of following a script, it closes the gap however it arose: a lost node, a deleted pod or an extra copy.

Flowchart of one control loop for a controller that should keep 3 pods. Step 1, read the goal: desired 3 pods, from the API server. Step 2, observe: how many pods exist right now. Step 3, compare: how many? Too few, 2 of 3: create 1 Pod object, and the scheduler places it. Too many, 4 of 3: delete 1 Pod object, and its kubelet stops it. Equal, 3 of 3: do nothing until something changes. All three paths lead to step 4, repeat on the next change, back to step 1. Footer: acting means creating or deleting objects through the API server; kubelets on the nodes then start or stop the containers
A controller only edits objects; containers follow when the kubelets see the change.

Follow one request from kubectl to a container

Say app.yaml holds the web Deployment from lesson 7, asking for three nginx pods, and you run kubectl apply -f app.yaml. As in lesson 7, it prints deployment.apps/web created almost at once. Here is what happens around that reply:

  1. kubectl sends the file over HTTPS to the API server named in your kubeconfig, the file that tells kubectl which cluster to use and how to log in.
  2. The API server checks who you are and what you may do, and validates the YAML.
  3. It saves the Deployment in etcd and replies. Nothing runs yet.
  4. The Deployment controller creates a ReplicaSet (lesson 10), whose controller creates three Pod objects with no node.
  5. The scheduler picks a node for each pod and saves the choice; kubectl get pods -o wide shows it in the NODE column.
  6. On each chosen node, the kubelet sees its new pod and has containerd pull the image and start the container.
  7. The kubelet reports the pod as Running, and the API server saves that in etcd.

In the diagram, notice that kubectl's reply comes after step 3, before any pod exists. kube-proxy plays no part yet; it acts once a Service points at these pods.

Step-by-step diagram in seven numbered boxes. 1, you run kubectl apply -f app.yaml, which sends the file over HTTPS. 2, the API server checks you and your rights and validates the YAML. 3, etcd stores the Deployment as the desired state. A note: kubectl gets its reply after step 3, before any pod exists. 4, controllers turn the Deployment into a ReplicaSet and 3 pods with no node yet. 5, the scheduler picks a node for each pod and saves it through the API server. 6, the kubelet sees a pod for its node and asks containerd to start it. 7, running: the container has started and the kubelet reports Running to the API server. A second note: steps 4, 5 and 6 never call each other; each watches the API server for its work and writes its results back through it
Each part does one job and hands over through the API server, never directly.

Managed Kubernetes: someone else runs the head office

Running a control plane well is real work: redundant API servers, etcd backups, certificates and upgrades. Managed Kubernetes services such as Amazon Elastic Kubernetes Service (EKS), Google Kubernetes Engine (GKE) and Azure Kubernetes Service (AKS) do it for you. The provider runs the control plane on its own machines and hands you its API server address.

You still own the workloads: Deployments, Services, settings and access rules. You usually manage the worker nodes too, their size, count and upgrades, though managed node groups and modes such as GKE Autopilot take over some or all of that. It is the shared responsibility model applied to a cluster.

So on a managed cluster, kubectl get nodes lists only worker nodes, and kube-system has no API server or etcd pods. The provider also runs an optional fifth control plane part, the cloud-controller-manager, which talks to the cloud's API, for example to create load balancers.

Check yourself

Your cluster's control plane goes offline for ten minutes. What happens to running pods, and what stops working?

They keep serving: the kubelet and runtime on each node keep existing containers going on their own. What stops is anything needing a decision or a record: kubectl commands fail, no new pods are scheduled, and pods on a node that dies are not replaced until the control plane returns.

A new pod was accepted but has no node, so it never starts. Which component decides its next step, and what could hold it up?

The kube-scheduler, because placing new pods is its job, and no kubelet starts a pod that is not assigned to its node. If no node passes the filters, for example none has enough CPU or memory left for what the pod asks for, the pod waits; lesson 20 shows how to read the reason.

The scheduler crashes and restarts. Why does the cluster lose no information?

Because all cluster state lives in etcd behind the API server, not inside the scheduler. The restarted scheduler reads the current pods again and carries on placing any that still have no node; controllers recover the same way.

Go deeper