Day - 18 Kubernetes StatefulSets β Managing Stateful Applications
πΉ Introduction
So far in Kubernetes, we've deployed applications using Deployments.
Deployments work perfectly for:
Web Applications
APIs
Microservices
Stateless Workloads
But what about applications like:
MySQL
PostgreSQL
MongoDB
Cassandra
Elasticsearch
These applications need:
β Persistent Storage
β Stable Network Identity
β Ordered Deployment and Scaling
This is where StatefulSets come into the picture.
πΉ What is a Stateful Application?
A Stateful Application is an application that stores data and maintains identity.
Examples:
Databases
Message Queues
Distributed Storage Systems
Unlike stateless applications, stateful applications must remember information even after restarts.
Stateless vs Stateful
| Stateless | Stateful |
|---|---|
| No data stored | Stores data |
| Any Pod can replace another | Each Pod has unique identity |
| Easy scaling | Requires ordered scaling |
| Deployment used | StatefulSet used |
πΉ What is a StatefulSet?
A StatefulSet is a Kubernetes workload resource used to manage stateful applications.
It provides:
β Stable Pod Names
β Persistent Storage
β Ordered Deployment
β Ordered Scaling
β Ordered Deletion
πΉ Why Not Use Deployments?
Imagine running a database with Deployments.
Pods may get names like:
mysql-a8d4f
mysql-k7t2x
mysql-b9s1c
After restart:
mysql-z4y8p
Identity changes every time.
For databases, this can cause problems.
πΉ StatefulSet Pod Naming
StatefulSets provide predictable names.
Example:
mysql-0
mysql-1
mysql-2
Even after restart:
mysql-0
mysql-1
mysql-2
The identity remains the same.
πΉ StatefulSet Architecture
StatefulSet
β
ββββββΌβββββ
β β β
βΌ βΌ βΌ
mysql-0
mysql-1
mysql-2
β β β
PVC PVC PVC
β β β
PV PV PV
Each Pod gets its own storage.
πΉ StatefulSet Example
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8
πΉ Headless Service
StatefulSets usually work with a Headless Service.
Example:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
clusterIP: None
selector:
app: mysql
Notice:
clusterIP: None
This makes the Service Headless.
πΉ Why Headless Service?
It provides direct DNS records for Pods.
Example:
mysql-0.mysql.default.svc.cluster.local
mysql-1.mysql.default.svc.cluster.local
mysql-2.mysql.default.svc.cluster.local
Each Pod gets a stable hostname.
πΉ Ordered Deployment
When Kubernetes creates Pods:
mysql-0
β
mysql-1
β
mysql-2
Pods start one by one.
πΉ Ordered Scaling
Scaling from 3 β 5 replicas:
mysql-3
β
mysql-4
Created sequentially.
πΉ Ordered Deletion
Deleting Pods:
mysql-4
β
mysql-3
β
mysql-2
Removed in reverse order.
πΉ Persistent Storage with StatefulSets
Each Pod automatically receives its own Persistent Volume Claim.
Example:
mysql-0 β PVC-0
mysql-1 β PVC-1
mysql-2 β PVC-2
Data survives Pod recreation.
πΉ Real-World Example
Imagine an e-commerce company using MySQL.
Requirements:
β Database data persistence
β Stable hostnames
β Reliable failover
StatefulSets provide all these capabilities.
πΉ Benefits of StatefulSets
Stable Identity
Pods keep the same names.
Persistent Storage
Data survives restarts.
Ordered Operations
Safer deployment and scaling.
Database Friendly
Perfect for stateful workloads.
πΉ Common Interview Questions
What is a StatefulSet?
A Kubernetes resource for managing stateful applications.
Difference Between Deployment and StatefulSet?
| Deployment | StatefulSet |
|---|---|
| Stateless Apps | Stateful Apps |
| Dynamic Pod Names | Stable Pod Names |
| Shared Behavior | Unique Identity |
| Web Apps | Databases |
Why Do StatefulSets Need Headless Services?
To provide stable DNS identities for Pods.
Can StatefulSets Use Persistent Volumes?
Yes.
Each Pod gets its own Persistent Volume Claim.
πΉ Best Practices
β Use StatefulSets for databases
β Use Persistent Volumes
β Configure backups
β Use Headless Services
β Monitor storage usage
πΉ Key Insight
Stateful applications require more than just containers.
They need:
Stable identities
Persistent storage
Ordered operations
StatefulSets provide all three.
πΉ Conclusion
StatefulSets are essential when running databases and other stateful workloads in Kubernetes.
They ensure:
β Stable Pod Identity
β Persistent Data
β Reliable Scaling
β Predictable Operations
"Containers may be temporary, but stateful applications require permanence."
π Next Blog
Day 19: Kubernetes DaemonSets β Running Pods on Every Node