StatefulSet Headless Service DNS
Configure StatefulSets with headless services for stable network identities. Understand pod DNS, ordered deployment, and persistent storage patterns.
π‘ Quick Answer: A headless Service (
clusterIP: None) gives each StatefulSet pod a stable DNS name:<pod-name>.<service-name>.<namespace>.svc.cluster.local, enabling direct pod-to-pod communication.
The Problem
Regular Services load-balance across pods randomly. Stateful workloads (databases, message brokers, consensus systems) need:
- Stable, predictable hostnames
- Ordered startup and shutdown
- Persistent storage tied to specific pods
- Direct addressing of individual replicas
The Solution
StatefulSet with Headless Service
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: data
spec:
clusterIP: None
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
namespace: data
spec:
serviceName: postgres
replicas: 3
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:16
ports:
- containerPort: 5432
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: pg-secret
key: password
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: fast-ssd
resources:
requests:
storage: 50GiDNS Resolution
# Each pod gets a predictable DNS name
postgres-0.postgres.data.svc.cluster.local
postgres-1.postgres.data.svc.cluster.local
postgres-2.postgres.data.svc.cluster.local
# Service DNS returns ALL pod IPs (no load balancing)
nslookup postgres.data.svc.cluster.local
# Returns: 10.244.1.5, 10.244.2.8, 10.244.3.2Parallel Pod Management
spec:
podManagementPolicy: Parallel # Don't wait for ordered startup
replicas: 5Update Strategy
spec:
updateStrategy:
type: RollingUpdate
rollingUpdate:
partition: 2 # Only update pods with ordinal >= 2graph TD
subgraph StatefulSet
P0[postgres-0<br/>PVC: data-postgres-0]
P1[postgres-1<br/>PVC: data-postgres-1]
P2[postgres-2<br/>PVC: data-postgres-2]
end
HS[Headless Service<br/>clusterIP: None] --> P0
HS --> P1
HS --> P2
C[Client] -->|postgres-0.postgres.data| P0
C -->|postgres-1.postgres.data| P1Common Issues
Pods stuck in Pending after StatefulSet scale-up VolumeClaimTemplates create new PVCs. Check StorageClass and available PVs:
kubectl get pvc -n data
kubectl describe pvc data-postgres-3DNS not resolving individual pods Ensure serviceName in StatefulSet matches the headless Service name exactly.
PVCs not deleted on scale-down By design β PVCs persist after pod deletion. Manual cleanup required:
kubectl delete pvc data-postgres-2 -n dataPod stuck terminating during updates StatefulSets respect ordering: pod N must be Running before pod N+1 starts. Check pod Nβs readiness probe.
Best Practices
- Always create the headless Service before the StatefulSet
- Use
volumeClaimTemplatesfor per-pod persistent storage - Set
podManagementPolicy: Parallelwhen ordering doesnβt matter - Use
partitionfor canary updates (test on higher-ordinal pods first) - Configure PDB to prevent losing quorum during disruptions
- Use init containers for cluster bootstrap logic (detect first-run vs join)
Key Takeaways
- Headless Service (
clusterIP: None) enables per-pod DNS - Pod names are deterministic:
<statefulset>-<ordinal>(0-indexed) - PVCs from
volumeClaimTemplatessurvive pod deletion and rescheduling - Default ordering: pods created 0βN, deleted Nβ0, updated Nβ0
Parallelmanagement policy removes ordering guarantees for faster scalingpartitionenables rolling updates of a subset of pods

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
