Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 250 of 8024. Kubernetes Architecture and Core ObjectsFree lesson

Monitoring State Changes via API Watchers

You will learn how Kubernetes controllers continuously monitor the API server for changes to ensure the current state matches the desired specification.

In this lesson, you will learn how Kubernetes controllers use API Watchers to listen for real-time updates and ensure your cluster stays in the desired state.

The "Watch" Pattern

In previous lessons, we discussed how the Controller Manager maintains your desired state by constantly comparing it to the actual state. But how does it know when something changes without constantly asking the etcd database over and over again? It uses API Watchers.

Instead of performing expensive "polling" (asking "is it changed yet?" every second), the controller establishes a long-lived connection to the Kubernetes API Server. The controller tells the API server, "Keep this connection open and notify me immediately if any changes happen to these specific objects."

How it Works

When you submit a manifest via kubectl, the API server updates etcd. Because the controller is "watching," the API server immediately pushes an event notification to the controller. The controller then wakes up, evaluates the change, and performs the necessary actions (like spinning up a new pod or updating a configuration) to reconcile the state.

Think of it like a subscription service:

  1. The Controller (Subscriber): Tells the API Server, "I want to watch all Deployment resources."
  2. The API Server (Publisher): Maintains the connection.
  3. The Event: You update a deployment image. The API server sends a "Modified" event payload to the controller.
  4. The Action: The controller receives the payload and starts the update process.

A simplified conceptual view of the watch event payload looks like this:

{
  "type": "MODIFIED",
  "object": {
    "kind": "Deployment",
    "metadata": { "name": "web-server" },
    "spec": { "replicas": 3 }
  }
}

By using these watchers, Kubernetes achieves event-driven orchestration. This design is what makes the system feel "self-healing." It doesn't wait for a manual trigger; it reacts to every change at the API level the moment it occurs, ensuring the cluster is always working toward the goal you defined in your object manifests.

Key Takeaway: API Watchers allow Kubernetes controllers to react to state changes in real-time through an event-driven mechanism, eliminating the need for inefficient polling while ensuring immediate reconciliation.