Prompt
How do I deploy stateful services on Kubernetes?
Latest observation
Deploying stateful services on Kubernetes is very doable, but you should use the right building blocks so your data survives pod restarts, rescheduling, and scaling.
Core Kubernetes objects for stateful apps
1. PersistentVolume (PV)
Represents actual storage in the cluster or cloud backend.
2. PersistentVolumeClaim (PVC)
A request for storage made by your app. Pods mount PVCs, not PVs directly.
3. StorageClass
Defines how storage is provisioned dynamically, including performance and reclaim policy.
4. StatefulSet
Best controller for stateful workloads like databases, queues, and distributed systems.
Compared to a Deployment, a StatefulSet gives:
- Stable pod names:
app-0,app-1, etc. - Stable network identity
- Ordered startup/shutdown
- Pod-specific persistent storage via
volumeClaimTemplates
Typical approach
Use a StatefulSet when:
- Each replica needs its own disk
- Pods must keep identity across restarts
- The app depends on stable network names
- You need ordered rollout/termination
Examples:
- PostgreSQL
- MySQL
- MongoDB
- Redis cluster
- ZooKeeper
- Kafka
Use a Deployment when:
- The service is stateless
- All replicas are interchangeable
- Data lives outside the pod
Example: StatefulSet with persistent storage
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
clusterIP: None
selector:
app: mydb
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mydb
spec:
serviceName: mydb
replicas: 3
selector:
matchLabels:
app: mydb
template:
metadata:
labels:
app: mydb
spec:
containers:
- name: db
image: postgres:16
ports:
- containerPort: 5432
name: db
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: standard
resources:
requests:
storage: 10Gi
What this does
- Creates a headless Service for stable DNS
- Creates pods like
mydb-0,mydb-1,mydb-2 - Gives each pod its own PVC:
data-mydb-0data-mydb-1data-mydb-2
Important design considerations
1. Replication and consistency
If your app stores data, you need to think about:
- primary/replica roles
- leader election
- quorum
- failover
Kubernetes provides the pods and storage, but your database software usually handles replication and consensus.
2. Storage performance
Pick storage based on workload:
- SSD-backed volumes for databases
- IOPS-sensitive workloads may need provisioned disks
- Network storage can add latency
3. Access modes
Common access modes:
ReadWriteOnce— one node mounts the volume at a timeReadWriteMany— multiple nodes can mount simultaneously, if supported by your storage backend
Most databases use ReadWriteOnce.
4. Backups and recovery
StatefulSet alone is not backup strategy. Use:
- database-native backups
- snapshots
- off-cluster backups
- restore drills
5. Pod disruption and scheduling
Use:
- PodDisruptionBudgets
- anti-affinity / topology spread
- node affinity for storage locality
This helps keep replicas on separate nodes and reduces outage risk.
6. Upgrades
Be careful with version changes:
- use rolling updates
- confirm app supports live schema changes
- test restore and rollback procedures
Best practices
- Prefer managed databases if possible
- Use Operators for complex stateful systems
- Keep data on persistent volumes, not container filesystems
- Set resource requests/limits
- Add readiness/liveness probes carefully
- Use backups and disaster recovery
- Avoid running singleton stateful apps without persistence
When to use an Operator
For many production stateful services, an Operator is better than hand-writing YAML.
Operators automate:
- cluster bootstrap
- replication setup
- failover
- backups
- upgrades
- scaling
Examples:
- Crunchy Postgres Operator
- Percona Operator
- Strimzi for Kafka
- MongoDB operators
Minimal deployment checklist
Before deploying a stateful service, verify:
- Persistent storage class exists
- Volume size and performance are adequate
- StatefulSet is used instead of Deployment
- Headless Service is configured
- Backups are in place
- Probes are defined
- Resource requests/limits are set
- Anti-affinity/topology spread is configured
- Recovery/failover is tested
If you want, I can also give you:
- a PostgreSQL StatefulSet example,
- a production-ready StatefulSet template, or
- help you deploy a specific service like MongoDB, Redis, or MySQL.