Skip to content
DevOpsadvanced

What is a Kubernetes operator?

Updated Nov 30, 20263 min readBased on official documentation
What is a Kubernetes operator?

In short

A Kubernetes operator is a custom controller that watches a custom resource and automates the work needed to run an application. It extends Kubernetes without changing Kubernetes itself. The operator follows the same control loop as built-in controllers, comparing the desired state with the actual state and taking steps to close the gap.

The idea behind the pattern

Kubernetes already automates a lot. It schedules Pods, restarts failed containers and keeps the number of replicas you asked for. An operator takes this idea further. It encodes the knowledge a human operator would have about one specific application: how to deploy it, back it up, upgrade it and recover it when something goes wrong.

The operator pattern lets you extend cluster behaviour without modifying Kubernetes code. You do this by linking a controller to one or more custom resources. A custom resource is an API object you define yourself, and it has its own kind, such as a hypothetical SampleDB. The operator is a client of the Kubernetes API that acts as the controller for that kind.

How it works

An operator follows the Kubernetes control loop. The controller reads the desired state from a custom resource, looks at what is actually running, and then makes changes so that reality matches the desired state. It repeats this forever, so small drifts are corrected automatically.

A typical operator is made of a few parts:

  • A CustomResourceDefinition that teaches the API server about the new kind.
  • A Deployment that runs the controller code as an ordinary containerised application, usually outside the control plane.
  • Controller code that queries the API server to find out which custom resources exist and what they ask for.

When you create a new SampleDB resource, the operator can create PersistentVolumeClaims for durable storage, a StatefulSet to run the database and a Job for initial configuration. When you delete it, the operator can take a snapshot and then clean up the StatefulSet and volumes. Backups, which need a Pod that connects to the database, can be scheduled the same way. Connection details and credentials are usually kept in a ConfigMap or a Secret.

Using an operator

Once the operator is running, you use it through the custom resource. You add, change or delete objects of that kind with the normal tools. For example:

kubectl get SampleDB
kubectl edit SampleDB/example-database

The first command lists the configured databases. The second opens one of them for editing. You change a setting, save, and the operator applies the change to the running service. You do not touch the StatefulSet or Pods directly.

When to write your own

Before writing code, check whether an existing operator already does what you need. Many ready-made operators are listed on OperatorHub.io. If none fits, you can write your own. An operator is simply a controller, so you can use any language or runtime that can talk to the Kubernetes API. Several libraries help, including kubebuilder, Operator Framework, the Java Operator SDK, Kopf for Python and kube-rs for Rust.

A simpler alternative is Metacontroller, which lets you implement the sync logic through WebHooks instead of writing a full controller. For very small automation tasks, a plain Job or a CronJob may be enough, and an operator adds more moving parts than you need.

Common mistakes

Operators can hide a lot of behaviour, so a few traps are common. Make the reconcile logic idempotent, so that running it twice gives the same result as running it once. Avoid acting on stale data, because the controller should always read the current state before it changes anything. Be careful with deletion: a finalizer can stop the resource from disappearing until cleanup, such as a final backup, has finished. Finally, remember that the operator needs RBAC permissions to read and change the resources it manages. Give it only what it needs.

If you are new to custom objects, read our page on Kubernetes CustomResourceDefinitions first. It explains the part of the operator that teaches the API server about your new kind.

FAQ

Is an operator the same as a controller?

Not exactly. Every operator contains a controller, but a controller in Kubernetes is any loop that drives the actual state toward the desired state. An operator is a controller that is tied to one or more custom resources and encodes application-specific knowledge.

Do I need to modify Kubernetes to use an operator?

No. The operator pattern is designed to extend cluster behaviour without changing Kubernetes code. You install a CustomResourceDefinition and run the controller as a normal workload in the cluster.

Where does the operator controller usually run?

Usually it runs as a Deployment in the cluster, outside the control plane, like any other containerised application. It talks to the API server as a client.

Can I write an operator in any programming language?

Yes, as long as the language can act as a client for the Kubernetes API. In practice, libraries exist for several languages, which make the work easier.

Sources

This page is based on the official documentation below. Read it for full detail.

Kubernetes operator — official documentation ↗

Kubernetes operator in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.