Difficulty: Advanced
What is the Kubernetes Operator pattern, how does an operator work, and when should you build or use one?
An operator is a custom controller that manages an application through a custom resource. You declare what you want (for example kind: PostgresCluster with 3 instances and daily backups) and the operator runs a reconciliation loop that continuously drives the real world toward that spec, encoding the operational knowledge a human SRE would otherwise apply by hand: provisioning, failover, backups, upgrades and scaling.
PostgresCluster or KafkaTopic.status.This is exactly how built-in controllers work. The Deployment controller reconciles Deployments into ReplicaSets; an operator applies the same pattern to domain-specific objects.
Ready=True, so users and tools like Argo CD can see health.ServiceMonitor objectsCertificate objectsA team running Kafka for order events at a food delivery company would typically use Strimzi rather than hand-written StatefulSets, because broker rolling restarts and partition-safe upgrades are already encoded in it.
Build an operator when an application has real operational logic that must run repeatedly and correctly, and when many instances of it exist. Use Kubebuilder or Operator SDK (Go, built on controller-runtime), or Kopf if your team prefers Python.
Do not build one for a stateless web app; a Deployment plus a Helm chart is enough. Operators are software you must test, upgrade and secure. Common mistakes include non-idempotent reconcile logic, hot loops that hammer the API server, writing to spec instead of status, and granting the operator cluster-admin when it needs a handful of resources.
import kopf
from kubernetes import client
@kopf.on.create("db.example.com", "v1", "postgresclusters")
@kopf.on.update("db.example.com", "v1", "postgresclusters")
def reconcile(spec, name, namespace, logger, **_):
apps = client.AppsV1Api()
sts = build_statefulset(name, spec) # desired child object
kopf.adopt(sts) # owner reference for cleanup
try:
apps.read_namespaced_stateful_set(name, namespace)
apps.patch_namespaced_stateful_set(name, namespace, sts)
except client.ApiException as e:
if e.status != 404:
raise # Kopf retries with backoff
apps.create_namespaced_stateful_set(namespace, sts)
logger.info(f"{name}: want {spec['instances']} instances")
# Returned dict is written under .status.reconcile
return {"desiredInstances": spec["instances"]}
The handler is idempotent (create or patch), adopts its child so it is garbage collected with the parent, and reports progress in status. Go operators built with Kubebuilder follow the same structure in a Reconcile function.
apiVersion: db.example.com/v1
kind: PostgresCluster
metadata:
name: orders-db
spec:
instances: 3
version: "17"
backup:
schedule: "0 2 * * *"
retentionDays: 7
The user declares intent; the operator turns it into StatefulSets, Services, Secrets and backup jobs and keeps them correct.
Operator Pattern, Reconciliation Loop, Custom Controllers, Kubebuilder, Stateful Workload Automation