Kubernetes StatefulSet YAML example with persistent storage
A StatefulSet is for workloads where each Pod needs its own identity and its own disk, such as a database. Pods get stable names (postgres-0, postgres-1) and start in order.
It depends on two other resources: a headless Service, named in serviceName, that gives each Pod a DNS record, and persistent storage that outlives a restart. This manifest shows all of them together, plus a PodDisruptionBudget that keeps two replicas up during node maintenance.
Will traffic reach the Pods?
HTTPRoute db.example.comconnects toService postgres-headless:5432connects toStatefulSet postgres:5432.
What this manifest does
This manifest defines 8 resources across 2 namespaces: the main thing it runs is 1 StatefulSet. Let’s walk through it.
StatefulSet postgres runs 3 Pods, running postgres:16.2.
A Service (postgres-headless) gives these Pods a stable cluster-internal address. Public traffic enters through an HTTPRoute (postgres-route).
Each Pod attaches persistent storage via PersistentVolumeClaim postgres-data (50Gi).
A PodDisruptionBudget (postgres-pdb) protects these workloads during cluster maintenance.
Things to know before using it
No livenessProbeStatefulSet postgres
Container "postgres" has no livenessProbe. Kubernetes can't detect and restart a hung container. Fix: Add a livenessProbe with httpGet, tcpSocket or exec.
No readinessProbeStatefulSet postgres
Container "postgres" has no readinessProbe. Pods may receive traffic before they're ready. Fix: Add a readinessProbe so Services route traffic only when the Pod is healthy.
No topology spread or anti-affinityStatefulSet postgres
StatefulSet "postgres" has 3 replicas but no spread rules. All Pods could land on one node, a single failure domain. Fix: Add topologySpreadConstraints or podAntiAffinity to spread across zones/nodes.
The full manifest
# Stateful workload + Gateway API + persistent storage.
apiVersion: v1
kind: Namespace
metadata:
name: data
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-ssd
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Delete
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-data
namespace: data
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 50Gi
---
apiVersion: v1
kind: Service
metadata:
name: postgres-headless
namespace: data
spec:
clusterIP: None
selector:
app: postgres
ports:
- port: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
namespace: data
spec:
serviceName: postgres-headless
replicas: 3
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:16.2
ports:
- containerPort: 5432
resources:
requests: { cpu: 200m, memory: 512Mi }
limits: { cpu: '1', memory: 2Gi }
securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: { drop: ["ALL"] }
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumes:
- name: data
persistentVolumeClaim:
claimName: postgres-data
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: postgres-pdb
namespace: data
spec:
minAvailable: 2
selector:
matchLabels:
app: postgres
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: edge
namespace: data
spec:
gatewayClassName: envoy
listeners:
- name: https
port: 443
protocol: HTTPS
hostname: api.example.com
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: postgres-route
namespace: data
spec:
parentRefs:
- name: edge
hostnames:
- db.example.com
rules:
- backendRefs:
- name: postgres-headless
port: 5432
Related examples
Stuck on a Kubernetes concept?
Ask anything and get a narrated visual lesson, pitched at your level. Free.
Try an example