The Myth: Deployments Lose Data
Most developers assume StatefulSets exist because Deployments can't persist data. That's false. A Deployment paired with a Persistent Volume Claim (PVC) retains data across pod restarts just fine.
Experiment 1: Deployment + PVC
Clone the repo and apply the secrets and ConfigMap:
git clone https://github.com//K8s_with_Pravesh.git
cd K8s_with_Pravesh/part-02-statefulsets/configs
kubectl apply -f secrets-and-config.yml
Apply the Deployment manifest (includes PVC, Deployment, Service):
kubectl apply -f deployment.yml
Connect to MySQL, insert a record:
kubectl exec -it mysql-XXXXXX-XXXX -- mysql -uroot -p
# password: Pravesh
mysql> USE crud_app;
mysql> INSERT INTO users(name,email,password) VALUES('Pravesh','pravesh@example.com','secret');
mysql> exit
Delete the pod:
kubectl delete pod mysql-XXXXXX-XXXX
Wait for the new pod, connect, and query:
kubectl exec -it mysql-XXXXXX-XXXX -- mysql -uroot -p
mysql> USE crud_app;
mysql> SELECT * FROM users;
The data is still there. The PVC preserved it — not the Deployment.
Experiment 2: StatefulSet
Apply the StatefulSet manifest:
kubectl apply -f statefulset.yml
Notice the pod name is predictable: mysql-0. Insert another record, delete the pod, and watch it recreate with the same name mysql-0. The data survives.
The Real Difference
Inspect PVCs:
kubectl get pvc
With StatefulSets, each replica gets its own dedicated PVC (e.g., mysql-data-mysql-0, mysql-data-mysql-1, mysql-data-mysql-2). This creates a stable, one-to-one relationship between pod and storage.
StatefulSet pods are not interchangeable. Each has:
- A stable hostname
- A stable network identity
- Persistent storage tied to that identity
- Ordered deployment, scaling, and termination
When to Use StatefulSets
StatefulSets are for stateful distributed systems like MySQL, PostgreSQL, Kafka, ZooKeeper, Redis Clusters, and Elasticsearch. They need:
- Stable, unique network identifiers
- Stable, persistent storage per replica
- Ordered, graceful deployment and scaling
- Ordered rolling updates
Deployments are for stateless apps: frontends, REST APIs, microservices.
Key Differences Table
| Feature | Deployment | StatefulSet |
|---|---|---|
| Data Persistence | ✅ With PVC | ✅ With PVC |
| Stable Pod Name | ❌ | ✅ |
| Stable Network Identity | ❌ | ✅ |
| Dedicated Storage Per Replica | ❌ | ✅ |
| Ordered Startup/Shutdown | ❌ | ✅ |
Conclusion
Don't fall for the myth. A Deployment with a PVC persists data just as well as a StatefulSet. The real value of StatefulSets is the stable identity and dedicated storage per pod. Choose StatefulSets when your app needs those guarantees. For everything else, Deployments are simpler and sufficient.
Next steps: Try the experiments yourself from the GitHub repo and see the difference firsthand.
