In this lesson, you will learn how Kubernetes uses the reconciliation loop to automatically detect and replace failed Pods, ensuring your applications stay available without human intervention.
The "Desired State" vs. "Actual State"
In Kubernetes, you define a Desired State—for example, "I want three instances of my web server running." The system’s job is to make the Actual State match that definition at all times. This is the essence of self-healing.
When you use a ReplicaSet to manage your Pods, you are essentially setting a contract. The ReplicaSet controller constantly watches the API server, comparing the number of running Pods to the number you requested in your object definition.
How Self-Healing Works in Practice
Imagine you have a ReplicaSet configured for three Pods. If one Pod crashes due to a software bug or the node it resides on experiences a hardware failure, the following "reconciliation" process occurs:
- Detection: The API watcher notices that one Pod has moved to a
FailedorTerminatedstate. - Comparison: The controller sees that the current count of healthy Pods is now 2, while your desired state is 3.
- Action: The controller immediately issues an instruction to the API server to create a new Pod.
- Scheduling: The scheduler assigns the new Pod to an available node, and the
Kubeletpulls the container image to start the service.
This happens automatically and almost instantly. You don't get a page at 3:00 AM to manually restart a process; the system recognizes the deviation from your intent and corrects it.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-app
spec:
replicas: 3 # Desired state
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx
If you delete a Pod manually or if a container crashes, the ReplicaSet sees the "missing" resource and triggers a replacement. By continuously observing the environment and acting to close the gap between reality and your configuration, Kubernetes provides a robust layer of self-healing infrastructure.
Key Takeaway: Self-healing is achieved because the controller loop constantly monitors the cluster, automatically re-provisioning Pods whenever the actual state fails to match your declared desired state.