Stateful Apps
Stateful applications require persistent storage, stable network identities, and ordered deployment/termination. Kubernetes provides StatefulSets for managing such workloads, while operators offer a pattern for automating complex lifecycle management tasks. This section explores how to deploy stateful applications using StatefulSets and how operators can abstract storage workflows for custom stateful systems.
StatefulSets for Stateful Applications¶
StatefulSets ensure stable, unique network identities and persistent storage for stateful workloads. Unlike Deployments, they:
- Assign stable DNS hostnames to pods.
- Maintain ordered deployment and scaling.
- Provide persistent storage per pod via PVCs.
Key Concepts¶
- Headless Service: A Service with
clusterIP: Nonethat provides DNS records for each pod. - PersistentVolumeClaims (PVCs): Each pod gets a dedicated PVC, often bound to a PersistentVolume (PV) with a storage class.
- Pod Management Policy:
OrderedReady(default) ensures pods are created in sequence, orParallelfor simultaneous creation.
Example: Deploying a Stateful Application¶
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumes:
- name: data
persistentVolumeClaim:
claimName: my-pvc
volumeClaimTemplates:
- metadata:
name: my-pvc
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
storageClassName: standard
Operators for Stateful Workloads¶
Operators are custom controllers that manage stateful applications by abstracting complex operations like provisioning storage, backups, and upgrades. They use Custom Resource Definitions (CRDs) to define application-specific resources.
Operator Patterns¶
- CRD-Driven Management:
- Define a CR (e.g.,
Database) with fields likestorageClassName,backupPolicy, andreplicaCount. -
The operator watches for changes to these CRs and reconciles the actual state.
-
Storage Lifecycle Automation:
- Operators can dynamically provision PVCs, configure storage classes, and manage snapshots.
-
Example: An operator for PostgreSQL might create PVCs, set up replication, and handle backups via Velero.
-
Event-Driven Reconciliation:
- Operators respond to events like PVC creation, node failures, or CR updates to ensure the system remains in a desired state.
Example: Operator Skeleton with Operator SDK¶
# Initialize a new operator project
operator-sdk init --domain=example.com --repo=github.com/example/db-operator
# Create a CRD for a custom stateful application
operator-sdk add api --api-version=database.example.com/v1alpha1 --kind=Database
Best Practices for Stateful Applications¶
- Use dedicated storage classes for stateful workloads (e.g.,
local-path-provisionerfor local storage). - Implement backups with tools like Velero or cloud-native backups (e.g., AWS Backup).
- Monitor and alert on PVC failures or storage quota exhaustion.
- Leverage operators for complex workflows like multi-node clustering or automated scaling.
Key takeaways¶
- StatefulSets provide stable storage and network identities for stateful apps via PVCs and headless services.
- Operators abstract storage and lifecycle management, enabling automation for complex stateful workflows.
- Combine StatefulSets with operators to handle tasks like dynamic provisioning, backups, and upgrades.
- Prioritize persistent storage class selection and backup strategies to ensure data resilience.