Sub topic 8
What an operator is and when to build one
This chapter assumes you know Pods, Deployments and Services, can write YAML and use kubectl, and understand the control loop from earlier chapters. It also assumes you have seen a CustomResourceDefinition (CRD) and created a custom resource.
The pattern in one sentence
An operator is a custom resource plus a controller that watches it and acts on it. The CRD defines a new API type, such as WebApp. The controller encodes the operational knowledge a human would otherwise apply by hand: how to create, configure, upgrade, heal and clean up the thing that type describes.
A plain controller, like the Deployment controller, ships with Kubernetes and manages built-in types. An operator uses the same mechanism for a domain you define. The difference is scope: an operator captures application-specific knowledge.
What the operator does for you
For the WebApp example, the user writes only image and replicas. The operator turns that into a Deployment, keeps it in sync, and reports readyReplicas in status. Users never write the Deployment's selector or labels. They work with a small, validated API that fits their problem.
Real operators go further. A database operator might take backups, promote a replica on failure, run schema-safe upgrades, and remove external storage on deletion.
When to build one
Build an operator when all of these hold:
- You manage something with a lifecycle beyond create and delete, such as upgrades, failover, backups or scaling rules.
- The work must run continuously and react to change, not once at deploy time.
- You want users to drive it through the Kubernetes API, with RBAC, kubectl and GitOps tools working as usual.
When not to build one
| Situation | Better choice |
|---|---|
| You only need to template manifests | Helm or Kustomize |
| A one-off or scheduled task | Job or CronJob |
| Simple validation or defaulting | CEL validation rules or an admission policy |
| An existing operator already covers it | Install and configure that one |
| Nobody on the team can maintain Go code or on-call for it | A simpler tool |
An operator is software you must test, secure, version and run. It runs with cluster permissions, so a bug can affect many workloads. Treat it as a product, not a script.
A quick check on what you have
You can see an operator's two halves with kubectl:
kubectl get crd webapps.example.com
kubectl get deployments -n operator-system
kubectl get webappsThe first command shows the API type exists. The second shows the controller is running as an ordinary Deployment. The third shows the instances it manages.
Exercise
A team wants to stamp out the same Deployment and Service for 20 microservices with different image names. Should they build an operator? Give your reasoning.
Show answer
Probably not. This is templating, with no ongoing lifecycle logic such as failover or backups. Helm or Kustomize fits better and avoids the cost of writing, securing and running a controller. An operator makes sense only if the services need continuous, application-specific management.