MongoDB
Designing a Kubernetes Operator
Learn what Kubernetes operators are, how they extend the Kubernetes API, and how to design a custom operator for stateful application management.
S
srikanthtelkalapally888@gmail.com
Designing a Kubernetes Operator
Kubernetes Operators encode operational knowledge into software — automating deployment, scaling, and management of complex stateful applications.
What is an Operator?
An Operator = Custom Resource Definition (CRD) + Custom Controller
Built-in K8s:
Deployment → Controller ensures N pods running
Operator:
PostgreSQLCluster → Controller ensures DB cluster healthy
ElasticsearchCluster → Controller manages shards/replicas
Custom Resource Definition (CRD)
Extend Kubernetes API with custom types:
apiVersion: db.example.com/v1
kind: PostgreSQLCluster
metadata:
name: my-postgres
spec:
replicas: 3
version: "16"
storage: "50Gi"
backup:
schedule: "0 2 * * *"
destination: "s3://backups/"
Controller Reconciliation Loop
func (r *Reconciler) Reconcile(req Request) (Result, error) {
// 1. Fetch current state
cluster := &PostgreSQLCluster{}
r.Get(ctx, req.NamespacedName, cluster)
// 2. Calculate desired state
desired := r.buildDesiredState(cluster)
// 3. Reconcile diff
if diff := calculateDiff(current, desired); diff != nil {
r.applyChanges(diff)
}
// 4. Update status
cluster.Status.Replicas = current.ReadyReplicas
r.Status().Update(ctx, cluster)
return Result{RequeueAfter: 30 * time.Second}, nil
}
Operator Capabilities
Level 1: Basic install
Level 2: Upgrades
Level 3: Lifecycle management
Level 4: Deep insights
Level 5: Auto-pilot (self-healing, auto-tuning)
Example Behaviors
PostgreSQL Operator:
- Automatic leader election
- Streaming replication setup
- Automatic failover
- Scheduled backups
- Point-in-time recovery
- Configuration management
Tools
Operator SDK (Red Hat/Mask)
Kubebuilder (official)
COPY (cluster-api)
Conclusion
Operators are the pattern for managing stateful workloads on Kubernetes. They encode operational expertise that would otherwise require manual SRE intervention.