StatefulSets and DaemonSets: When Stateless Deployments Are Not Enough
StatefulSet vs Deployment vs DaemonSet: stable identity, per-pod volumes, headless Services, ordered updates and one pod per node.

Run PostgreSQL as a three-replica Deployment with a shared PersistentVolumeClaim and the problems arrive quickly. The claim is ReadWriteOnce, so only one node can mount it. Pods get random names, so the replicas cannot find the primary. A rollout replaces pods in any order, so the primary might go first. None of this is a bug in Deployments; they were designed for stateless pods behind a Service, as covered in pods, ReplicaSets and Deployments.
Choosing the controller
| Deployment | StatefulSet | DaemonSet | |
|---|---|---|---|
| Pod names | Random suffix | Stable ordinal: db-0, db-1 |
Random suffix, one per node |
| Storage | Shared claim or none | One claim per pod from volumeClaimTemplates |
Usually hostPath or none |
| Replica count | You choose | You choose | Follows the number of matching nodes |
| Ordering | None | Ordered create, delete and update by default | Node by node during updates |
| Network identity | Through a Service | Per-pod DNS through a headless Service | Often host network or node IP |
| Typical use | Web apps, APIs, workers | Databases, Kafka, ZooKeeper, etcd | Log and metrics agents, CNI, CSI node plugins |
StatefulSets
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: cache
spec:
clusterIP: None # headless: DNS returns pod IPs directly
selector:
app: redis
ports:
- port: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
namespace: cache
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
persistentVolumeClaimRetentionPolicy:
whenDeleted: Retain
whenScaled: Delete
template:
metadata:
labels:
app: redis
spec:
terminationGracePeriodSeconds: 60
containers:
- name: redis
image: redis:7.2-alpine
args: ["--appendonly", "yes"]
ports:
- containerPort: 6379
volumeMounts:
- name: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: fast-ssd
resources:
requests:
storage: 20Gi
Three guarantees come with this manifest.
Stable identity. Pods are named redis-0, redis-1 and redis-2, and keep those names when rescheduled. Through the headless Service, each gets a DNS name such as redis-0.redis.cache.svc.cluster.local, so replicas and clients can address a specific member. The Services article covers how ordinary ClusterIP Services differ.
Per-pod storage. Each pod gets its own claim, data-redis-0 and so on, created from volumeClaimTemplates and the StorageClass described in persistent volumes and StorageClasses. When redis-1 is rescheduled to another node, it reattaches to data-redis-1, not a fresh disk.
Ordering. By default pods are created one at a time from 0 upwards, each waiting for the previous one to be Ready, and deleted in reverse. Rolling updates also go in reverse ordinal order. Clustered software that bootstraps from member 0 relies on this. If your software does not need ordered startup, podManagementPolicy: Parallel creates and deletes pods at once, which speeds up scaling considerably; updates still go one by one.
What StatefulSets do not do
A StatefulSet gives stable names and disks. It does not know which replica is the primary, does not configure replication, does not fail over, does not back up and does not resize a cluster safely. Your application, an init script or an operator has to do all of that. This is the most common misunderstanding: a three-replica StatefulSet of PostgreSQL is three independent PostgreSQL servers until something configures replication between them.
Operational details
- Claims outlive pods by default. Deleting a StatefulSet or scaling it down has always left its claims behind, which protects data but leaks volumes. The
persistentVolumeClaimRetentionPolicyfield, beta and enabled by default since Kubernetes 1.27, makes this explicit. The example keeps claims if the StatefulSet is deleted, but removes them when scaling down. - Volume templates are mostly immutable. You cannot change the size in
volumeClaimTemplateson an existing StatefulSet. To grow disks, expand each PVC directly (if the StorageClass allows expansion), then recreate the StatefulSet object withkubectl delete statefulset redis --cascade=orphanand the new template, which leaves the pods running. - Stuck rollouts. With the default
OrderedReadypolicy, a rollout that produces a broken pod stops there. Fixing the template is not always enough: the broken pod may need to be deleted by hand before the controller recreates it from the corrected spec. - Staged rollouts.
updateStrategy.rollingUpdate.partition: 2updates only pods with ordinal 2 or higher, so you can try a new version on one member before lowering the partition to roll it to the rest. - Disruption budgets. Add a PodDisruptionBudget with
maxUnavailable: 1so node drains never take down two members of a quorum at once.
An operator is usually the better choice
For databases in production, use an operator: CloudNativePG or Crunchy PGO for PostgreSQL, Strimzi for Kafka, or the vendor operators for MongoDB, Elasticsearch and Redis. Operators use StatefulSets or pods underneath, but add the failover, backups, upgrades and replication configuration that a StatefulSet lacks. Also consider whether the database belongs in the cluster at all: a managed service such as RDS or Cloud SQL moves the hardest operations off your team.
DaemonSets
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluent-bit
namespace: logging
spec:
selector:
matchLabels:
app: fluent-bit
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 10%
template:
metadata:
labels:
app: fluent-bit
spec:
serviceAccountName: fluent-bit
priorityClassName: system-node-critical
tolerations:
- operator: Exists # run on every node, including tainted ones
containers:
- name: fluent-bit
image: fluent/fluent-bit:3.1.7
resources:
requests: { cpu: 50m, memory: 64Mi }
limits: { memory: 128Mi }
volumeMounts:
- name: varlog
mountPath: /var/log
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/log
The DaemonSet controller places one pod on every node that matches its node selector and tolerations, adds one when a node joins and removes it when the node leaves. There is no replica count to set.
- Tolerations decide coverage. Without them, DaemonSet pods skip control plane nodes and any node with a taint, such as GPU or dedicated pools.
operator: Existstolerates everything; narrow it if some nodes should be excluded. For taints in detail, see taints and tolerations. - Use a nodeSelector or node affinity to target a subset, such as a GPU driver DaemonSet that only runs on GPU nodes.
- Keep requests small and accurate. A DaemonSet’s requests are multiplied by every node and reserved on each one. 200m of CPU on 100 nodes is 20 cores.
- Priority matters.
system-node-criticalorsystem-cluster-criticalpriority lets agents evict lower-priority pods when a node is full, rather than staying Pending on a busy node. - Updates.
maxUnavailablecontrols how many nodes lose the agent at once during a rollout. For agents where a gap is unacceptable,maxSurgestarts the new pod before the old one stops, as long as the pods do not bind the same host ports.
DaemonSets usually need host access (hostPath mounts, host network or extra capabilities), so their namespaces typically run under the baseline or privileged Pod Security level covered in the Pod Security Standards guide. Keep them in dedicated namespaces so application namespaces can stay restricted.
Frequently Asked Questions
What is the difference between a StatefulSet and a Deployment?
A Deployment treats pods as interchangeable, with random names and shared or no storage. A StatefulSet gives each pod a stable name, a stable DNS entry through a headless Service and its own persistent volume, and creates, deletes and updates pods in order.
When should I use a DaemonSet?
When exactly one copy of a pod must run on every node, or on every node in a group: log collectors, metrics exporters, network and storage plugins, security agents and node-level tools. The number of pods follows the number of matching nodes.
Do StatefulSets delete their volumes?
Not by default. Persistent volume claims created from volumeClaimTemplates are kept when pods or the StatefulSet are deleted. The persistentVolumeClaimRetentionPolicy field can delete them when the StatefulSet is deleted, scaled down or both.
Should I run databases in Kubernetes with StatefulSets?
It is workable, but a StatefulSet alone does not handle replication, failover, backups or upgrades. Use a mature operator for the database, or a managed database service, unless your team has the experience to run it directly.
The right controller for the job
Most workloads should be Deployments. Reach for a StatefulSet when replicas need their own identity and disk, and a DaemonSet when something must run beside every node. Both give scheduling guarantees, not application logic, so pair StatefulSets with an operator for anything that holds important data. The official documentation is worth keeping open: the StatefulSet documentation and the DaemonSet documentation. Related reading on this site: Jobs and CronJobs.
Originally posted on 10 September 2024 and post last modified on 10 September 2024