Skip to content

How do controllers use reconciliation loops to enforce desired state?

The mechanics of the reconciliation loop, is the heartbeat of Kubernetes' declarative model.

In Kubernetes, controllers are the engines that drive the cluster's "actual state" toward the "desired state" defined in your manifests. This process is not a one-time execution but a continuous, non-terminating control loop, conceptually similar to a thermostat regulating room temperature.

1. The Core Mechanism: Spec vs. Status

To understand reconciliation, you must understand the structure of Kubernetes objects. Almost every object consists of two nested fields that govern this process:

  • .spec (Desired State): This is the description you provide (e.g., via YAML). It declares the characteristics you want the resource to have, such as "run 3 replicas of Nginx".
  • .status (Current State): This describes the actual state of the system at this moment. It is supplied and updated by the Kubernetes system and its components.

The reconciliation loop effectively calculates the "diff" between .spec and .status and performs operations to eliminate that difference.

2. The Reconciliation Workflow

Controllers typically operate using the Controller Pattern, which follows a distinct cycle of observing, analyzing, and acting.

A. Observation (The Watch)

Controllers do not constantly poll the API server in a heavy loop. Instead, they establish a Watch on specific resource types. When a change occurs (e.g., a user updates a Deployment's image, or a Pod crashes), the API server notifies the controller.

B. Comparison and Logic

Once triggered, the controller checks the spec of the object and compares it against the current reality.

  • Internal State: It checks resources inside the cluster (e.g., counting running Pods).
  • External State: Some controllers, like the Cloud Controller Manager, check the state of external infrastructure (e.g., verifying if a cloud load balancer exists).

C. Actuation (Driving State)

If the current state does not match the desired state, the controller takes action.

  • Indirect Action: Most built-in controllers do not execute the work themselves. Instead, they send messages to the API server to create or update other resources.
    • Example: The Job Controller does not run containers. When it sees a new Job task, it creates Pod objects via the API server. The Scheduler then assigns those Pods to nodes, and the Kubelet (another controller) actually starts the containers.
  • Direct Action: Some controllers interact directly with external systems. For example, a controller ensuring nodes exist might call a cloud provider API to provision a new VM.

D. Status Update

Finally, the controller reports the new state back to the API server by updating the object's .status field. For example, once a Job is done, the controller updates the Job object to mark it Finished.

3. Benefits of this Architecture

This design eliminates the need for traditional "orchestration" where a system executes a defined workflow (Step A $\rightarrow$ Step B $\rightarrow$ Step C). Instead, Kubernetes relies on independent, composable control processes.

  • Self-Healing: If a change occurs outside of the controller's action (e.g., a node crashes and Pods disappear), the controller observes this deviation from the spec during its next sync and creates replacements automatically.
  • Resilience: Because controllers continuously drive toward the desired state, it does not matter how the system reached its current state. If a controller crashes and restarts, it simply reads the current state and resumes work; it does not need to "remember" where it was in a complex workflow.

4. Custom Controllers (Operators)

This reconciliation pattern is extensible. You can write Custom Controllers (often called Operators) that pair with Custom Resource Definitions (CRDs). This allows you to encode specific domain knowledge (like how to backup a database) into a reconciliation loop that manages your custom application just as native controllers manage Pods.

Based on Kubernetes v1.35 (Timbernetes). Changelog.