Kubernetes operator

Also known as: operator pattern

On this page 7

A Kubernetes operator is a custom controller paired with a custom resource that encodes the operational knowledge of a human administrator, so that a complex application can be deployed, configured, upgraded, backed up and repaired automatically inside Kubernetes. The approach, also called the operator pattern, extends the platform’s declarative model beyond stateless workloads to software such as databases, message brokers and monitoring stacks.

In the Cloud subject, operators matter because they turn runbook procedures into code. Instead of a person following a checklist to fail over a database replica, the operator watches the cluster and performs the same steps whenever the observed state drifts from the desired state.

How a Kubernetes operator works

An operator works by running a reconciliation loop that continuously compares the desired state declared in a custom resource with the actual state of the cluster and then acts to close the gap. The user writes a short manifest, for example a PostgresCluster object requesting three replicas and nightly backups, and the operator translates that intent into Pods, StatefulSets, Services, Secrets and scheduled jobs.

The mechanism rests on three building blocks. A Custom resource definition (CRD) registers a new API type with the Kubernetes API server, so the new object is stored in etcd like any built-in resource. A controller process, usually running as a Deployment inside the cluster, subscribes to change events through watches and informers. Finally, the domain logic inside the controller decides which concrete actions bring reality in line with the specification.

The following table maps each component to its role in the loop.

ComponentRoleWhere it lives
Custom resource definitionDefines the schema of the new API typeKubernetes API server and etcd
Custom resourceDeclares the desired state for one instanceNamespace of the application
ControllerWatches events and reconciles statePod managed by a Deployment
RBAC rulesGrant the controller permission to actServiceAccount, Role, ClusterRole
Status subresourceReports observed state and conditionsField on the custom resource
Core components of the operator pattern

Reconciliation must be idempotent and level-based. The controller should reach the same result whether it runs once or a hundred times, and it should react to the current state rather than to the exact sequence of events, because events can be dropped, merged or delivered late.

Operator capability levels

Operators differ widely in how much operational work they automate, and the community commonly describes this with a five-level capability model used by the Operator Framework. A basic operator only installs software, while a mature one tunes performance and heals failures without human help.

LevelNameWhat the operator automates
Level 1Basic installProvisioning and configuration from a custom resource
Level 2Seamless upgradesPatch and minor version upgrades of the application
Level 3Full lifecycleBackup, restore, failure recovery and storage management
Level 4Deep insightsMetrics, alerts, log processing and workload analysis
Level 5AutopilotHorizontal and vertical scaling, auto-tuning, anomaly detection
Capability levels from basic installation to autopilot

Most production-grade database operators reach level 3 or level 4. Level 5 claims deserve scrutiny, because automatic tuning of a stateful system is hard to verify and can mask underlying capacity problems.

An operator differs from Helm and from built-in controllers because it manages the entire lifecycle continuously, not only the initial installation. Helm renders templates and applies them once per release, whereas an operator stays running and responds to change at any time.

ApproachMain strengthMain limitation
Kubernetes operatorDay-2 automation with domain logicCode to write, test and maintain
Helm chartSimple packaging and versioned releasesNo runtime reaction after install
StatefulSet aloneStable identities and ordered rolloutNo knowledge of replication or backups
GitOps controllerSyncs manifests from a Git repositoryApplies state but has no application logic
External scriptsQuick to write for one taskRun outside the cluster, often fragile
Operator versus neighboring approaches

These tools are complementary rather than competing. A common setup installs the operator itself with a Helm chart, stores custom resources in Git, and lets Argo CD or a similar GitOps tool apply them, while the operator handles everything that happens afterward.

Building and choosing an operator

Teams typically build operators with an SDK that generates scaffolding, CRD manifests and test harnesses, so engineers can concentrate on reconciliation logic. The choice of toolkit depends on language skills and on how much control the application requires.

ToolkitLanguageBest suited for
KubebuilderGoFull-featured operators built on controller-runtime
Operator SDKGo, Ansible, HelmTeams reusing existing playbooks or charts
KopfPythonPython teams and lighter automation
Java Operator SDKJavaOrganizations standardized on the JVM
Common ways to implement an operator

Before writing a new operator, check whether a maintained community operator already exists for the software. Evaluate it on upgrade history, test coverage, backup and restore support, the permissions it requests and how quickly security issues receive fixes. An unmaintained operator becomes a liability, because it holds broad cluster rights and sits in the critical path of a stateful service.

Limitations and common mistakes

The main risk of an operator is that it concentrates powerful automation in a single process, so a logic error can damage every instance it manages at once. Operational experience points to recurring failure patterns.

  • Overly broad permissions: a ClusterRole with wildcard verbs turns a controller bug into a cluster-wide incident; grant only the resources the operator actually manages.
  • Edge-triggered logic: code that assumes every event arrives exactly once breaks under restarts; always recompute from the current state.
  • Missing status conditions: without clear conditions in the status field, operators of the platform cannot tell whether reconciliation succeeded or stalled.
  • Unsafe CRD changes: removing or renaming schema fields without API versioning and conversion webhooks can break existing resources.
  • Hidden coupling to cluster versions: an operator tested on one Kubernetes release may fail after an upgrade that deprecates an API it uses.

Operators also add cognitive and maintenance cost. For a simple stateless service, a Deployment, a Horizontal Pod Autoscaler and a Helm chart are usually sufficient, and writing an operator only makes sense when the application has genuine day-2 complexity.

The short version

Kubernetes can keep simple apps running on its own, but complicated software such as a database needs an expert who knows how to back it up, fix it and upgrade it safely. A Kubernetes operator is that expert written as a program. It sits inside the cluster and watches.

You tell it what you want in a few lines, such as three copies of a database with a backup every night. The operator then does the work and keeps checking. If a copy fails at three in the morning, it repairs the setup without waiting for a person. In short, it turns a team’s know-how into automatic, repeatable actions.

Where it makes a difference

A mid-sized online retailer runs its order database on a Kubernetes cluster with a PostgreSQL operator at capability level 3. The platform team declares one custom resource that requests three replicas spread across availability zones, continuous write-ahead log archiving to object storage and a full backup every night at 02:00.

During a regional network fault, the node hosting the primary replica becomes unreachable. Within about thirty seconds the operator detects the failed health checks, promotes the most up-to-date standby, updates the Service so that applications connect to the new primary and schedules a replacement replica on a healthy node.

The checkout service sees a brief spike of connection retries but no lost orders. Before adopting the operator, the same failover required a paged engineer and roughly forty minutes of manual work; afterward, the team reviews the event the next morning from the status conditions and logs.