0
0
Kubernetesdevops~10 mins

Database operators example in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Database operators example
Create Custom Resource Definition (CRD)
Deploy Database Operator
Create Database Custom Resource
Operator Watches CR
Operator Creates/Manages DB Pods
Database Ready and Running
Operator Monitors and Updates DB
End
This flow shows how a Kubernetes database operator manages database lifecycle by watching custom resources and controlling pods.
Execution Sample
Kubernetes
apiVersion: databases.example.com/v1
kind: PostgresCluster
metadata:
  name: my-postgres
spec:
  instances: 3
This YAML creates a custom resource for a Postgres database cluster with 3 instances, which the operator will manage.
Process Table
StepActionResource StateOperator ReactionResult
1Apply CRD for PostgresClusterCRD createdOperator recognizes new resource typeReady to manage PostgresCluster resources
2Deploy Postgres OperatorOperator pod runningOperator starts watching PostgresCluster resourcesOperator active
3Create PostgresCluster resource with 3 instancesPostgresCluster resource createdOperator detects new resourceStarts creating 3 Postgres pods
4Operator creates Postgres pod 1Pod 1 Pending -> RunningOperator monitors pod statusPod 1 ready
5Operator creates Postgres pod 2Pod 2 Pending -> RunningOperator monitors pod statusPod 2 ready
6Operator creates Postgres pod 3Pod 3 Pending -> RunningOperator monitors pod statusPod 3 ready
7All pods runningCluster readyOperator ensures cluster healthDatabase cluster operational
8Update PostgresCluster spec to 4 instancesSpec updatedOperator detects changeCreates 4th pod
9Operator creates Postgres pod 4Pod 4 Pending -> RunningOperator monitors pod statusPod 4 ready
10All 4 pods runningCluster readyOperator maintains clusterDatabase cluster scaled
11Delete PostgresCluster resourceResource deletedOperator deletes all podsCluster removed
12All pods terminatedNo pods runningOperator stops managing clusterCleanup complete
💡 PostgresCluster resource deleted, operator cleans up pods and stops managing cluster
Status Tracker
VariableStartAfter Step 3After Step 7After Step 10Final
PostgresCluster.spec.instancesundefined334undefined
Postgres Pods Running00340
Operator StateNot runningRunningRunningRunningRunning but idle
Key Moments - 3 Insights
Why does the operator create pods after the PostgresCluster resource is created?
Because the operator watches for new PostgresCluster resources (see execution_table step 3) and reacts by creating pods to match the requested instances.
What happens when the number of instances in the spec changes?
The operator detects the spec update (step 8) and adjusts the number of pods accordingly by creating or deleting pods to match the new count.
How does the operator clean up resources when the PostgresCluster is deleted?
When the resource is deleted (step 11), the operator deletes all associated pods and stops managing the cluster, completing cleanup (step 12).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the operator first create a Postgres pod?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Check the 'Operator Reaction' and 'Result' columns around steps 3 and 4.
According to the variable tracker, how many pods are running after step 7?
A3
B0
C4
DUndefined
💡 Hint
Look at the 'Postgres Pods Running' row under 'After Step 7' column.
If the PostgresCluster spec.instances was changed from 3 to 5 instead of 4, what would the operator do?
ADo nothing
BDelete pods to reduce to 3
CCreate 2 more pods to reach 5
DDelete all pods
💡 Hint
Refer to the behavior in steps 8-10 where the operator adjusts pods to match spec.instances.
Concept Snapshot
Database Operator in Kubernetes:
- Deploy operator and CRD first
- Create custom resource (e.g., PostgresCluster)
- Operator watches resource and creates/manages pods
- Changes in spec trigger operator to scale pods
- Deleting resource triggers cleanup
- Operator automates DB lifecycle management
Full Transcript
This visual execution shows how a Kubernetes database operator manages a Postgres database cluster. First, the Custom Resource Definition (CRD) is created so Kubernetes knows about the new resource type. Then the operator is deployed and starts watching for PostgresCluster resources. When a PostgresCluster resource is created with a spec requesting 3 instances, the operator reacts by creating 3 Postgres pods. It monitors their status until all are running and the cluster is ready. If the spec changes to request 4 instances, the operator creates an additional pod to scale up. When the PostgresCluster resource is deleted, the operator deletes all pods and cleans up. Variables like the number of running pods and operator state change step-by-step as shown. Key moments include understanding how the operator reacts to resource creation, spec changes, and deletion. The quizzes test understanding of when pods are created, pod counts at steps, and operator scaling behavior. This example demonstrates how operators automate database lifecycle in Kubernetes.