Skip to content

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: None that 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, or Parallel for 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

  1. CRD-Driven Management:
  2. Define a CR (e.g., Database) with fields like storageClassName, backupPolicy, and replicaCount.
  3. The operator watches for changes to these CRs and reconciles the actual state.

  4. Storage Lifecycle Automation:

  5. Operators can dynamically provision PVCs, configure storage classes, and manage snapshots.
  6. Example: An operator for PostgreSQL might create PVCs, set up replication, and handle backups via Velero.

  7. Event-Driven Reconciliation:

  8. 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-provisioner for 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.