Skip to main content
K8sCalc

kubernetes

Kubernetes StatefulSet Generator

Generate a Kubernetes StatefulSet YAML with a PVC template and headless Service. Ready for databases, queues, and other stateful workloads.

Kubernetes StatefulSets

StatefulSets provide three guarantees Deployments don't: stable network identities, stable persistent storage, and ordered startup/shutdown.

Pod Identity

my-app-0   — always pod 0, always uses PVC data-my-app-0
my-app-1   — always pod 1, always uses PVC data-my-app-1
my-app-2   — always pod 2, always uses PVC data-my-app-2

DNS for Each Pod

<pod>.<headless-svc>.<namespace>.svc.cluster.local
my-app-0.my-app-headless.default.svc.cluster.local

volumeClaimTemplates

Each pod gets its own PVC. The template generates:

  • data-my-app-0 for pod 0
  • data-my-app-1 for pod 1

PVCs outlive pods — data persists through pod restarts and rescheduling.

Common Stateful Workloads

AppReplicasStorage
PostgreSQL (CNPG)1 primary + N replicasLonghorn or cloud SSD
Redis Sentinel3 (1 primary + 2 replicas)Longhorn
Kafka3+Fast local NVMe
Zookeeper3Standard SSD

Frequently Asked Questions

When should I use a StatefulSet vs a Deployment?

Use a StatefulSet when your pods need stable network identities (predictable pod names like my-app-0, my-app-1), stable persistent storage (each pod gets its own PVC that follows it), or ordered startup/shutdown. Databases (PostgreSQL, MongoDB, Redis Sentinel), message queues (Kafka, RabbitMQ), and distributed caches are all StatefulSet use cases. Use a Deployment for stateless apps where any pod is interchangeable.

What is a headless Service and why does a StatefulSet need one?

A headless Service (clusterIP: None) doesn't get a virtual IP. Instead, DNS returns individual pod IPs directly. StatefulSets require a headless Service to create stable DNS entries for each pod: my-app-0.my-app-headless.default.svc.cluster.local. This allows pods to discover and communicate with specific replicas — essential for clustering protocols (Raft, Paxos, Galera).

Can I scale a StatefulSet down safely?

Yes, but with caution. Kubernetes scales down StatefulSets in reverse order (my-app-2 first, then my-app-1, etc.) and respects PodDisruptionBudgets. The PVC is NOT deleted when you scale down — the pod's data persists. If you scale back up, the same PVC is reattached. To delete PVCs you must do it manually. Never scale below quorum for clustered apps (e.g. don't scale an odd-node etcd from 3→2).

How do I update a StatefulSet?

StatefulSets support RollingUpdate (default) and OnDelete strategies. With RollingUpdate, pods are updated one at a time in reverse ordinal order — my-app-2 first. This ensures the cluster always has running nodes during an upgrade. Set partition: N to do a staged rollout where only pods with ordinal ≥ N are updated, letting you test on one pod before rolling to all.

Related Calculators

Related Guides