Kubernetes StatefulSet Generator

Build a StatefulSet with its volume claim template and the headless Service reference it needs. The parts that are immutable, and the volumes that are never deleted, are called out beside the output.

Workload
Identity and storage

The parts that make a StatefulSet different from a Deployment.

Resources

statefulset.yaml

updates as you type

    Common mistakes

    These are the ones that fail silently. The config is accepted, nothing raises an error, and the consequence arrives later.

    1. Expecting volumeClaimTemplates to be cleaned up

      PVCs created by a StatefulSet survive scaling down and deleting the StatefulSet itself. They persist deliberately, and they keep costing.

      Instead:Delete them explicitly when you mean to, and know the retention policy fields in newer versions.

    2. Omitting the headless Service

      serviceName must name a headless Service, and without it the stable per-pod DNS names do not exist.

      Instead:Create the headless Service first and reference it.

    3. Assuming ordered startup means ordered readiness downstream

      Pods start in order and each waits for the previous to be Ready, which makes rollouts slow and a stuck pod block everything behind it.

      Instead:Use Parallel pod management where ordering is not actually required.

    What a StatefulSet gives you that a Deployment does not

    Stable identity. Each pod gets an ordinal that survives rescheduling, a DNS name that goes with it, and its own volume that follows it. That is the whole feature, and each part of it has a cost that is not obvious until it bites.

    The headless Service is not optional, and nothing creates it

    spec.serviceName names a Service that must already exist with clusterIP: None. Nothing validates the reference. Get it wrong and the pods still start and look healthy, but the per-pod DNS names never resolve, which is the entire reason to use a StatefulSet rather than a Deployment.

    apiVersion: v1
    kind: Service
    metadata:
      name: api-headless
    spec:
      clusterIP: None          <- headless. Required.
      selector:
        app.kubernetes.io/name: api
      ports:
        - port: 8080
    
    # then each pod resolves at:
    # api-0.api-headless.default.svc.cluster.local

    The volumes outlive everything, on purpose

    volumeClaimTemplates creates one PVC per replica. Scaling down does not delete them. Deleting the StatefulSet does not delete them. That is the safe default, because the data is the point, and it is a recurring surprise on the cloud bill months later.

    scale 3 -> 10   creates data-api-3 .. data-api-9
    scale 10 -> 3   pods go, seven PVCs stay, still billed
    scale 3 -> 10   the old PVCs are reattached, with
                    their old data
    
    kubectl delete sts api   PVCs still there
    
    # 1.27+ has persistentVolumeClaimRetentionPolicy to
    # opt into deletion. It is not the default.

    OrderedReady means one bad pod blocks everything after it

    By default pod N+1 is not created until pod N is Ready. A pod that can never become Ready therefore stops the entire set from progressing, indefinitely, and scaling up appears to do nothing at all. Parallel starts them all at once and is right whenever the workload does not need ordered startup.

    podManagementPolicy: OrderedReady   (default)
      api-0 Ready -> create api-1 -> Ready -> api-2 ...
      api-1 CrashLoopBackOff -> api-2 never created
    
    podManagementPolicy: Parallel
      all pods created at once
    
    # this field is immutable after creation.

    Rolling updates go backwards, and partition is the canary

    Updates proceed from the highest ordinal down to zero, one pod at a time, waiting for each to become Ready. partition stops that descent: only pods at or above the partition are updated. Lower it gradually to roll out, and a forgotten partition looks exactly like a rollout that stalled halfway.

    replicas: 5, partition: 4
      only api-4 is updated. api-0 to api-3 keep the
      old spec indefinitely.
    
    partition: 0   the full rollout, 4 then 3 then 2...
    
    # nothing warns that a partition is still set.

    The volume size is immutable in the template

    Changing storage in volumeClaimTemplates on an existing StatefulSet is rejected. Growing the volumes means editing each PVC directly, if the StorageClass has allowVolumeExpansion. Shrinking is never possible. And with a StorageClass using Immediate binding, a volume can be created in a zone the pod cannot reach, leaving it Pending forever.

    # growing, one PVC at a time:
    kubectl patch pvc data-api-0 -p \
      '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'
    
    # check first:
    kubectl get storageclass -o custom-columns=\
    NAME:.metadata.name,EXPAND:.allowVolumeExpansion,\
    BINDING:.volumeBindingMode

    More kubernetes tools

    Kubernetes YAML Generator Answer a few questions, get a manifest Kubernetes YAML Validator Is this YAML valid, and where is it wrong? Kubernetes Rollout & Probe Timing Restart loops, capacity dips and stuck drains Kubernetes Service Checker Will this Service have any endpoints? Kubernetes Network Policy Viewer What can reach what, and what just broke Kubernetes Manifest Diff What actually changed Kubernetes Resource Calculator What these manifests actually reserve Kubernetes RBAC Viewer Who can actually do what Kubernetes Deployment Generator See the capacity dip before you deploy Kubernetes Service Generator port, targetPort, and which one you meant Kubernetes QoS Class Calculator And where you sit in the eviction order Kubernetes Node Capacity Planner Allocatable is not capacity Kubernetes Max Pods per Node Calculator The ENI limit, not the 110 default Kubernetes Label Selector Tester Which key failed, not just that one did Kubernetes Taint & Toleration Tester Tolerations permit, they do not attract Kubernetes Resource Name Validator RFC 1123 subdomain, label, or 1035 Kubernetes Label & Annotation Validator Why your image tag is not a legal label Kubernetes CrashLoopBackOff Guide The status is a timer, not a cause Kubernetes TLS Certificate Decoder When it expires, and what it covers Kubernetes ServiceAccount Token Decoder Bound or legacy, and when it dies Kubernetes imagePullSecret Generator The registry URL nobody gets right Kubernetes cert-manager Certificates Issuer scope, and the rate limit that costs a week Kubernetes Flux HelmRelease Generator With the source it cannot work without Kubernetes Helm Chart Generator Chart.yaml, values, and working templates Kubernetes Argo CD Application Generator The defaults that are off, turned on Kubernetes Ingress Generator pathType, and the TLS secret that fails quietly Kubernetes HPA Generator Why it says unknown and never scales Kubernetes SecurityContext Generator Pod level, container level, and which wins Kubernetes Probe Generator How long it gets to start before liveness kills it Kubernetes PVC Generator Access modes your storage can actually do Kubernetes ResourceQuota Generator And the LimitRange that stops it breaking deploys Kubernetes CronJob Generator The fields that decide whether it runs Kubernetes NetworkPolicy Generator With the DNS rule already in it Kubernetes RBAC Generator And what it actually grants Kubernetes PodDisruptionBudget Generator Can this budget ever be satisfied? Kubernetes API Deprecation Checker What breaks on the next upgrade Kubernetes Kubeconfig Viewer Read it without handing it to anyone Kubernetes Quantity Parser What 512Mi, 100m and 1e3 actually mean Kubernetes Multi-Document YAML Splitter One file per object, in apply order Kubernetes .env to ConfigMap & Secret Split the credentials out on the way Kubernetes Secret Decoder See what is actually in there Kubernetes Ingress to Gateway API Which annotations silently stop working Kubernetes Manifest Visualizer What points at what, and what points at nothing Kubernetes Patch Tester Which --type, and what it deletes Kubernetes kubectl JSONPath Tester kubectl's dialect, not generic JSONPath Kubernetes Node Affinity Tester Required excludes, preferred only scores Kubernetes Helm Values Diff A missing key is a third value Kubernetes Pod and Service CIDR Fixed at cluster creation Kubernetes Secret Encoder base64 is not encryption Kubernetes API Version Checker Will this apply on that cluster Kubernetes Go Template Tester Go truthiness is not JavaScript's Kubernetes Cluster Cost Estimator At your rates, not a price table Kubernetes Kustomize Build Transformers run in kustomize's order Kubernetes Helm Chart Validator version is SemVer, appVersion is not

    Elsewhere on the site