0
0
Kubernetesdevops~10 mins

Operator pattern overview in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Operator pattern overview
User defines Custom Resource (CR)
Operator watches CR changes
Operator reads desired state
Operator compares desired vs actual state
Operator takes actions to reconcile
Kubernetes cluster state updated
Operator continues watching for changes
The Operator pattern watches custom resources, compares desired and actual states, and acts to keep the system in the desired state.
Execution Sample
Kubernetes
apiVersion: example.com/v1
kind: MyApp
metadata:
  name: example-app
spec:
  replicas: 3
This YAML defines a custom resource 'MyApp' with 3 replicas, which the Operator will manage.
Process Table
StepActionOperator ReadsComparison ResultOperator ActionCluster State Change
1Operator detects new CR 'example-app'replicas=3No actual state yetCreate 3 pods3 pods created
2Operator checks cluster statereplicas=3Actual pods=3 matches desiredNo action neededState unchanged
3User updates CR replicas to 5replicas=5Actual pods=3 less than desiredCreate 2 more pods5 pods running
4Operator detects pod failure, actual pods=4replicas=5Actual pods=4 less than desiredCreate 1 pod5 pods running
5User deletes CR 'example-app'CR missingDesired state goneDelete all podsPods deleted
6Operator sees no CRNo CRNo desired stateNo actionCluster idle
💡 Operator stops actions when no custom resource exists or desired state matches actual state.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
Desired replicasundefined3355undefinedundefined
Actual pods0335500
CR existsfalsetruetruetruetruefalsefalse
Key Moments - 3 Insights
Why does the Operator create pods even if some pods already exist?
Because the Operator compares desired replicas with actual pods (see Step 3 and 4 in execution_table). If actual pods are fewer than desired, it creates more to match.
What happens when the custom resource is deleted?
The Operator detects no desired state (Step 5), so it deletes all related pods to clean up the cluster.
Does the Operator act if the actual state matches the desired state?
No, as shown in Step 2, when actual pods equal desired replicas, the Operator takes no action.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the actual number of pods after Step 3?
A5 pods
B3 pods
C4 pods
D0 pods
💡 Hint
Check the 'Actual pods' column in variable_tracker after Step 3.
At which step does the Operator detect the custom resource has been deleted?
AStep 4
BStep 5
CStep 6
DStep 2
💡 Hint
Look at the 'CR exists' variable in variable_tracker and the 'Action' column in execution_table.
If the desired replicas were changed to 4 instead of 5 at Step 3, what would the Operator do?
ADelete 1 pod
BCreate 2 more pods
CCreate 1 more pod
DNo action
💡 Hint
Compare desired vs actual pods in execution_table Step 3 and think about the difference.
Concept Snapshot
Operator pattern overview:
- User creates a Custom Resource (CR) defining desired state.
- Operator watches CR changes continuously.
- Operator compares desired state with actual cluster state.
- Operator takes actions to reconcile differences.
- When desired and actual states match, Operator waits.
- Deleting CR triggers cleanup by Operator.
Full Transcript
The Operator pattern in Kubernetes works by watching custom resources that users create to define how they want their applications to run. The Operator continuously checks the desired state from these resources and compares it with the actual state in the cluster. If there is a difference, such as fewer pods running than desired, the Operator takes action to fix it by creating or deleting pods. When the desired state matches the actual state, the Operator does nothing and waits for changes. If the user deletes the custom resource, the Operator cleans up by removing all related resources. This cycle repeats to keep the system stable and as the user wants.