StatefulSets
Introduction to StatefulSets
While Deployments
are ideal for stateless applications, StatefulSet
s are designed for applications that require stable, unique network identifiers, stable persistent storage, and ordered deployment and scaling. They are ideal for stateful applications like databases, distributed caches, and messaging systems.
Key Features of StatefulSets
- Stable, Unique Network Identifiers: Each pod in a StatefulSet gets a predictable hostname based on the StatefulSet name and an ordinal index (0, 1, 2, etc.).
- Ordered Deployment and Scaling: Pods are created and terminated in a defined order, starting from index 0.
- Stable Storage: Each pod can be associated with its own persistent volume that follows it even if it’s rescheduled to another node.
- Stable Network Identity: Services can route traffic to specific pods based on their stable hostnames.
When to Use StatefulSets
- Database clusters (MySQL, PostgreSQL, MongoDB)
- Messaging systems with persistent queues (RabbitMQ, Kafka)
- Distributed caches (Redis)
- Any application requiring stable identity and ordered operations
Example StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
spec:
serviceName: "postgres"
replicas: 3
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:13
ports:
- containerPort: 5432
name: postgres
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: password
volumeMounts:
- name: postgres-data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: postgres-data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: "managed-premium"
resources:
requests:
storage: 10Gi
In this example:
- The
StatefulSet
creates 3 PostgreSQL pods with predictable names:postgres-0
,postgres-1
, andpostgres-2
- Each pod gets its own 10GB persistent volume for data storage
- The pods are created and terminated in order
- We’re using Azure’s managed-premium storage class for better performance
StatefulSets vs Deployments
Feature | Deployment | StatefulSet |
---|---|---|
Use Case | Stateless applications | Stateful applications |
Pod Names | Random | Predictable, ordered |
Pod Creation | Parallel | Sequential |
Persistent Storage | Optional, shared | Per-pod, stable |
Network Identity | Service load balancing | Stable individual identities |
Scaling | Simple horizontal scaling | Ordered scaling |
AKS Considerations for StatefulSets
When using StatefulSet
s in AKS:
- Use Azure managed disks or Azure Files for persistent storage
- Consider zone redundancy for high availability
- For databases, evaluate managed services (Azure Database) vs. self-managed
StatefulSet
s - Be aware of the performance characteristics of different storage classes (see later section on storage)
StatefulSet Best Practices
- Consider using a headless service (
clusterIP: None
) to enable stable DNS names for pods - Create appropriate
PodDisruptionBudgets
to ensure high availability during cluster operations - Implement proper backup and restore procedures for stateful data
- Consider using Helm charts for complex
StatefulSet
deployments - Carefully test scaling operations, as they can impact distributed stateful applications