liveBook · Manning

Kubernetes controller

As explained in Kubernetes in Action, Second Edition

A Kubernetes controller is a control loop that observes cluster state and takes actions to move it toward a specified desired state.

A Kubernetes controller is a control loop that keeps part of a cluster aligned with a desired state. You create or change an object through the Kubernetes API; its controller notices the change and acts on it. You specify the result you want, rather than every step needed to reach it. section 14.3.1 section 3.3.3

The loop has three parts: observe the current state, compare it with the state specified by an owning object, and change dependent objects when they differ. Most API object types have an associated controller. Controllers run in the control plane; worker-node components run the application containers. section 14.3.1 section 1.2.3

Figure 14.6 The ReplicaSet controller’s reconciliation loop
figure

Follow the ReplicaSet controller’s loop from observing pods and the requested replica count to creating or deleting pods. section 14.3.1

Keep three pods running

Set a ReplicaSet’s desired replica count to three. If a managed pod is deleted, the ReplicaSet controller creates a replacement; if too many matching pods exist, it deletes the excess. The controller uses the ReplicaSet’s label selector to decide which pods count—not their names. To stop replacements, change or remove the object that specifies the desired count rather than repeatedly deleting its pods. section 14.3.1 section 14.3.2 section 5.6.3

Controllers can form a chain. A Deployment controls a ReplicaSet, and that ReplicaSet controls pods. For example, run `kubectl scale deploy kiada --replicas 5`: the Deployment controller changes the ReplicaSet’s desired count, and the ReplicaSet controller creates the pods. Changing the owned ReplicaSet’s count directly may not stick—the Deployment controller can restore the value specified by the Deployment. section 15.1 section 15.1.2

A controller’s responsibility has limits. The ReplicaSet controller maintains the number of matching pods, but updating its pod template does not immediately update existing pods. Updating a Deployment’s pod template does replace pods; its configured update strategy determines how. A pod that remains present but fails readiness also need not be replaced merely to satisfy the ReplicaSet’s count. section 14.5 section 15.2 section 14.3.2

To see what happened, inspect an object with `kubectl describe` or list Event objects with `kubectl get events`. ReplicaSet events can report pods created or deleted by its controller. section 14.3

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains the reconciliation loop through ReplicaSet pod counts, selectors, ownership, and controller handoffs; stops short of controller implementation code.
Examples
Uses deleted and excess pods, a scaled Deployment, controller Events, and a failed readiness probe to show controller behavior and limits.
What it assumes
Introduces controllers without requiring prior container, Docker, or Linux experience, then builds on API objects, pods, and Deployments.
What you can do after
Diagnose/debug with it by checking why pods reappear, why an owned ReplicaSet change is reversed, and which controller acted.
How it connects
Connects controllers to the Kubernetes API, control plane, ReplicaSets, Deployments, pods, label selectors, Events, and update strategies.

Also explained in

Compare how 2 books explain Kubernetes controller →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.