Concept: Update Strategies (RollingUpdate vs. OnDelete).
Concept: Partitioning (Canarying stateful sets).
Concept: Rollback strategies (kubectl rollout undo vs. manual restoration from snapshots).
Actionable YAML examples.
Section 4: Operational Checklist/Troubleshooting: A quick reference table or list.
Drafting Content - Deep Dive:
Storage:
Must mention StatefulSet. This is non-negotiable for stateful workloads in K8s.
volumeClaimTemplates is the killer feature here. I need to show how it creates pvc-<statefulset-name>-<ordinal>.
StorageClass: Mention binding modes (WaitForFirstConsumer) to ensure scheduling works right.
Disruption Budgets:
This is about voluntary disruption (node drains).
The math: If I have 3 replicas, minAvailable: 1 means I can kill 2, but usually, we want minAvailable: 2 (kill 1 at a time) or percentage based.
Crucial for databases (Postgres, Mongo, etc.). Quorum must be maintained.
Rollbacks:
This is tricky for stateful apps.
Code rollback: kubectl rollout undo statefulset/my-app. This changes the container image back.
Data rollback: This is the hard part. Kubernetes doesn't automatically rollback data. I need to mention VolumeSnapshots. If you upgrade a DB schema and it breaks, kubectl rollout undo fixes the binary, but the data is still migrated/broken. I need to highlight this distinction.
Update Strategies:
RollingUpdate: The default. Good, but can be dangerous if version N+1 isn't compatible with version N (in distributed dbs).
OnDelete: Manual control. Good for strict requirements.
Partition: Allows canarying. Update pods 3 and 4, keep 0, 1, 2 on old version.
Refining the Output:
I will structure this as a "Playbook" with distinct "Plays" or sections. I'll use code blocks for YAML and CLI commands.
Self-Correction during drafting: I initially thought about just standard Deployments, but the prompt says "Stateful Workload," so I must stick strictly to StatefulSet. If I use Deployment, I lose the sticky identity and the volumeClaimTemplates.
Adding Polish: I should include a section on "Day 2 Operations" or "The Golden Rules" to summarize the philosophy.
Plan:
Header: Title and Scope.
Play 1: Storage Strategy (StatefulSets & PVCs).
Play 2: Resilience (PodDisruptionBudgets).
Play 3: Deployment Strategy (Rolling Updates & Partitions).
Play 4: Disaster Recovery (Rollbacks & Snapshots).
Summary Checklist.
Let's write the content.
Play 1: Focus on volumeClaimTemplates. Explain the naming convention (sticky identity).
Play 2: Explain the PDB logic. Use minAvailable for quorum-based apps.
Play 3: Explain the partition field in StatefulSet. It's often overlooked but vital for stateful canary deployments.
Play 4: Distinguish between Application Rollback (K8s native) and Data Rollback (Snapshotting). This is the most common pitfall.
Okay, ready to generate the response.
This playbook outlines the operational strategies for deploying and managing stateful applications (databases, queues, caches) on Kubernetes.
Unlike stateless workloads, stateful apps require strict ordering, stable network identities, and most importantly, data persistence and protection during node maintenance or upgrades.
Play 1: Guarding Availability with Pod Disruption Budgets (PDBs)
Objective: Ensure high availability during voluntary disruptions (e.g., Node upgrades, cluster autoscaling).
In a StatefulSet, Kubernetes defaults to updating pods one by one (ordinal order). However, during node maintenance, the cluster might try to evict multiple pods simultaneously. A PDB prevents this, ensuring your application maintains a minimum level of operation (or quorum).
The Strategy
Analyze Quorum: If you run a 3-node database, you likely need 2 nodes to maintain a quorum (reads/writes).
Set minAvailable: usually set to N-1 (where N is replica count) or specifically to the quorum number.
Avoid maxUnavailable for Quorum Apps: While valid, minAvailable is safer for databases where losing one node might break the cluster.
The Manifest
yaml
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: postgres-cluster-pdb
spec:
# CRITICAL: Never drop below 2 healthy pods (maintains quorum for a 3-node cluster)
minAvailable: 2
selector:
matchLabels:
app: postgres
role: database
Operational Command
Before draining a node, check if the eviction is allowed:
bash
# Check the status of the PDB
kubectl get pdb postgres-cluster-pdb
# Allowed disruptions: 1 (Meaning Kubernetes can kill 1 pod, but not 2 simultaneously)
Play 2: Persistent Storage & StatefulSets
Objective: Ensure data survives pod restarts and that pods reattach to the correct data upon rescheduling.
Standard Deployments cannot handle persistent state reliably because replicas are interchangeable. You must use a StatefulSet.
The Strategy
Stable Identifiers: Use headless services to give pods a DNS name (postgres-0, postgres-1) so they can find each other.
VolumeClaimTemplates: This is the defining feature. It dynamically creates a Persistent Volume (PV) for every replica, ensuring postgres-0 always gets its specific disk back.
Objective: Deploy new versions without causing downtime and revert broken changes effectively.
StatefulSets have two main update strategies: OnDelete (manual) and RollingUpdate (automated). For production, RollingUpdate with partitions is the safest path.
Scenario A: The "Canary" Rollout (Partitioning)
You want to update only postgres-2 (the last replica) to test the new version, keeping 0 and 1 safe.
Action: Set the partition value.
If partition: 2, only pods with index >= 2 are updated.
If a deployment fails (e.g., schema migration incompatibility), you must rollback the application image.
WARNING: Kubernetes rollbacks revert the Pod specification (Image version, ConfigMap). They do NOT revert the Data (Volume).
1. The "Soft" Rollback (If Change is Backward Compatible)
Use kubectl rollout undo. This switches the image back immediately.
bash
# Check history
kubectl rollout history statefulset/postgres
# Rollback to previous revision
kubectl rollout undo statefulset/postgres
# Watch the pods restart in reverse order (2 -> 1 -> 0)
kubectl rollout status statefulset/postgres
2. The "Hard" Rollback (If Data was Modified)
If your new app version migrated the database schema (e.g., added a column) and the old app version crashes when seeing that column, kubectl rollout undo will result in a CrashLoopBackOff.
The Manual Playbook:
Scale to 0: Bring the application down to stop further writes.
bash
kubectl scale statefulset postgres --replicas=0
Restore Data: You must restore your PV from a Volume Snapshot (AWS EBS Snapshot, Portworx Snapshot, etc.) or perform a SQL restore.
Force Image Reversion: Modify the StatefulSet manifest to point to the old image tag.