Sub topic 12
Limits of StatefulSets and when to use an operator
This chapter assumes you can read StatefulSet manifests, know how headless Services and volumeClaimTemplates work, and have seen the failure and update behavior from earlier chapters. It covers what a StatefulSet does not do, and how to decide between a plain StatefulSet and an operator.
What a StatefulSet guarantees
A StatefulSet gives each Pod a stable name, a stable DNS record, and its own PersistentVolumeClaim. It also controls the order of creation, scaling, and updates. These are Kubernetes-level guarantees about Pods and volumes.
What it does not do
A StatefulSet knows nothing about the application inside the container. It has no idea whether your database is a primary or a replica, or whether replication is caught up. The gaps below follow from that.
- No replication setup. Nothing configures primaries, replicas, or quorum. Your image or init logic must do it.
- No automatic failover. If the primary's Pod dies, Kubernetes recreates the Pod with the same name. It does not promote a replica.
- No backups or restores. A PersistentVolumeClaim is not a backup. Deleting the volume or the storage backend loses the data.
- No application-aware upgrades. A rolling update restarts Pods in order. It does not check replication lag or demote a primary before restarting it.
- No safe resizing. The
volumeClaimTemplatesfield cannot be changed on an existing StatefulSet. Growing volumes means expanding each PVC by hand, if the storage class allows it. - Readiness is the only health signal. With the default
OrderedReadypolicy, a Pod that never becomes Ready blocks later Pods and can stall a rollout.
Operators fill the gap
An operator is a controller that encodes operational knowledge for one application. It watches a custom resource, such as a PostgresCluster, and acts on it the way a database administrator would. Many operators create a StatefulSet underneath and manage everything the StatefulSet cannot.
A typical operator handles:
- Initial cluster bootstrap and replica joining.
- Promoting a replica when the primary fails.
- Scheduled backups, point-in-time recovery, and restores.
- Upgrades in an application-safe order, such as replicas first and primary last.
- Volume resizing and configuration changes.
Deciding which to use
| Situation | Better fit |
|---|---|
| Stateful app with no replication, or easy to rebuild (a cache, a single-node tool) | Plain StatefulSet |
| App that handles its own clustering and needs only stable names and disks | Plain StatefulSet |
| Learning, testing, or throwaway environments | Plain StatefulSet |
| Production database where data loss or downtime is costly | Operator |
| You need automated failover, backups, or point-in-time recovery | Operator |
| A mature operator already exists for your database | Operator |
Consider these costs of an operator before adopting one:
- It adds custom resource definitions and a controller that you must install, upgrade, and monitor.
- Its quality varies. Check release history, how active the project is, and whether it has been tested against your Kubernetes version.
- You depend on its design choices, so read how it handles failover and data safety.
A managed database service outside the cluster is a third option. It moves the operational work to a provider, at the price of cost and less control.
Checking what you have
You can see whether a cluster has operators by listing its custom resource definitions.
kubectl get crd
kubectl get statefulset -A
kubectl get pods -l app.kubernetes.io/managed-by -AIf an operator owns a StatefulSet, change the custom resource, not the StatefulSet. The operator reconciles the StatefulSet back to the state it wants, so direct edits are usually overwritten.
Exercise
A team runs a three-replica PostgreSQL StatefulSet. The primary's node fails. The Pod is recreated, but writes stay down until an engineer promotes a replica by hand. Why did the StatefulSet not handle this, and what would you add?
Show answer
A StatefulSet restores Pod identity and storage but has no knowledge of PostgreSQL roles, so it cannot promote a replica. Add an operator (or an in-image failover tool) that monitors the primary and performs promotion automatically, along with backup and upgrade handling.
Exercise
You need to run a small, rebuildable Redis cache that only needs stable DNS names and a disk per Pod. Do you need an operator? Give one reason.
Show answer
No. A plain StatefulSet is enough because the data is easy to rebuild, so automated failover, backups, and application-aware upgrades add cost without much benefit.