We assume that we are deploying a stateful application that requires persistent storage and needs to be resilient to voluntary disruptions.
Steps:
Create a namespace for the application (optional, but good practice).
Create a PersistentVolume (PV) or use a StorageClass to dynamically provision PersistentVolumeClaims (PVC).
We'll assume dynamic provisioning for simplicity, but if you have existing PVs, you can create them statically.
Create a PersistentVolumeClaim (PVC) that will be used by the stateful workload.
Create a PodDisruptionBudget (PDB) to ensure availability during voluntary disruptions (like node draining).
Create a Deployment (or StatefulSet, but the question says "stateful workload" and Deployment can be used with PVCs, however for true stateful applications with stable network identifiers and ordered scaling, StatefulSet is better. But the question also mentions "rollbacks", and Deployment has built-in rollback. StatefulSet does not have built-in rollback as of now, but we can use canary updates with StatefulSet. However, the question says "workload", so we can choose either. Since the question mentions rollbacks and Deployment is more straightforward for that, I'll use Deployment. But note: for a truly stateful application with multiple replicas that need stable storage and network, StatefulSet is recommended. We'll use a Deployment with a headless service and PVCs, but note that the Deployment does not provide stable network IDs for each pod. Alternatively, we can use a StatefulSet and handle rollbacks manually by updating the image and using the StatefulSet's update strategy. Since the question asks for rollbacks, I'll stick to Deployment for simplicity in rollbacks.)
However, note: if we use a Deployment with multiple replicas and PVCs, each pod will get its own PVC (if we use a volumeClaimTemplate in a StatefulSet) or we can share a PVC (but that is not recommended for multiple pods because of readWriteOnce). So for a stateful application with multiple replicas, we typically use StatefulSet with a volumeClaimTemplate so that each pod gets its own PVC. But Deployment does not support volumeClaimTemplate. Therefore, for a stateful application with multiple replicas that each need their own persistent storage, we must use StatefulSet.
Given the requirement for rollbacks and the fact that StatefulSet does not have built-in rollback (but we can use kubectl rollout undo for StatefulSet? Actually, as of Kubernetes 1.21, StatefulSet does not support kubectl rollout undo because it doesn't have a revision history like Deployment. So we cannot rollback a StatefulSet in the same way. Therefore, if rollback is a critical requirement, we might have to use Deployment and manage the PVCs separately? But then we lose the per-pod stable storage.
Alternatively, we can use a StatefulSet and manage rollbacks by keeping the old PVCs and updating the StatefulSet's pod template to the previous version? But note that StatefulSet does not support rollback natively. We would have to manually scale down, update the pod template, and scale up? That is risky.
Considering the trade-offs:
Option 1: Deployment with a shared PVC (readWriteMany) if the application supports it, then we can have multiple pods using the same volume. But that is not common for stateful applications that require exclusive access.
Option 2: Use a StatefulSet and accept that rollbacks are not straightforward. We can use a canary strategy and if something goes wrong, we manually revert the StatefulSet's pod template and then update the StatefulSet, but note that the StatefulSet will update each pod in order and if the new version is broken, we have to wait for each pod to be recreated. Also, the PVCs are already created and will be reused, so if the new version is incompatible with the old data, we are stuck.
Given the complexity, and since the question asks for a playbook that includes rollbacks, I will assume:
The application is stateful but we are deploying a single replica. Then we can use a Deployment with a single replica and a PVC. Then rollback is easy.
However, if we need multiple replicas and each with its own persistent storage, then we must use StatefulSet and handle rollbacks with caution, possibly by taking snapshots of the PVCs and restoring? That is beyond the scope of a simple playbook.
Since the question does not specify the number of replicas, I will assume we are deploying a single replica for simplicity and to demonstrate the concepts.
PDB (with maxUnavailable: 0 and minAvailable: 1 for a single replica)
Deployment (with 1 replica, using the PVC, and with a specific image tag for easy rollback)
Show how to rollback.
Alternatively, if we want to be prepared for multiple replicas in the future, we might design for a StatefulSet, but then rollback becomes tricky.
Let me re-read: "stateful workload" - it might imply multiple replicas with stable storage per pod. But the requirement of rollbacks makes it more compatible with Deployment if we can manage the PVCs in a way that each pod gets its own. However, Deployment does not have a built-in way to create a PVC per pod. We could create multiple PVCs manually and then use a Deployment with a selector that matches a specific PVC? That is not standard.
Standard practice for stateful applications with multiple replicas is StatefulSet with volumeClaimTemplates.
So, let's do two versions:
Version 1: Single replica with Deployment (easy rollback, but not scalable to multiple replicas with per-pod storage).
Version 2: Multiple replicas with StatefulSet (no built-in rollback, but per-pod storage).
Since the question says "stateful workload", I think we should go with StatefulSet and then handle rollbacks by manually updating the StatefulSet's pod template and then rolling update? But note that StatefulSet does not have a rollback history. We can use kubectl rollout status and kubectl rollout undo for StatefulSet? Actually, as of Kubernetes 1.21, StatefulSet does not support rollback. So we have to do it manually.
Alternatively, we can use a Deployment with a headless service and manually manage PVCs? That is not recommended.
Given the complexity, and because the question asks for a playbook that includes rollbacks, I will assume we are allowed to use a Deployment and we are deploying a single replica. If we need multiple replicas, we might have to adjust.
But wait, the question says "disruption budgets" - that applies to both. And "persistent volumes" - both can use PVCs. And rollbacks - Deployment has built-in, StatefulSet does not.
I think the safe answer is to use a Deployment for a single replica and then if we need multiple, we can adjust the playbook to use StatefulSet and handle rollbacks by keeping the old image and updating the StatefulSet's pod template to the old image if needed.
However, let me check: the Kubernetes documentation says that StatefulSet does not have a rollback mechanism because it doesn't store the history of revisions. So if we update a StatefulSet and it fails, we have to manually edit the StatefulSet to the previous pod template.
So, for the purpose of this playbook, I will write for a Deployment with a single replica and note that for multiple replicas we would use a StatefulSet and manage rollbacks manually.
But the question says "stateful workload", which typically means multiple replicas with stable network IDs and stable storage. So I'm conflicted.
Let's look at the requirements again:
Disruption budgets: both Deployment and StatefulSet can have PDB.
Persistent volumes: both can use PVCs, but StatefulSet with volumeClaimTemplate is the standard for per-pod storage.
Rollbacks: Deployment has kubectl rollout undo, StatefulSet does not.
Since the requirement is to have rollbacks, and we are building a playbook, I think we have to choose Deployment. But then we lose the per-pod storage for multiple replicas.
Alternatively, we can use a StatefulSet and use a tool like Helm to manage releases and rollbacks? Helm does support rollback for StatefulSet because Helm stores the release history. But that's outside of Kubernetes.
Given that the question is about Kubernetes and not Helm, I think we are expected to use native Kubernetes resources.
So, I will provide two examples:
Example 1: Using Deployment (for single replica or shared storage) with rollback capability.
Example 2: Using StatefulSet (for multiple replicas with per-pod storage) without built-in rollback, but we can show how to manually rollback by updating the pod template.
But the question says "build a stateful workload playbook", so I think we should focus on StatefulSet because that is the standard for stateful workloads.
However, the rollback requirement is problematic.
Let me check the latest Kubernetes documentation: as of 1.26, StatefulSet still does not support rollback. So we have to do it manually.
So, I will write a playbook for a StatefulSet and then show how to manually rollback by editing the StatefulSet's pod template.
But note: when we update a StatefulSet, it updates each pod in order. If we want to rollback, we have to change the pod template back to the old image and then the StatefulSet will update each pod again in order. That might cause downtime if the new version broke the application and we have to wait for each pod to be recreated.
Alternatively, we can scale down the StatefulSet, update the pod template, and then scale up? But note that StatefulSet does not scale down to zero by default? Actually, it does, but when scaling up, it will create new pods with the new template? Wait, if we scale down to zero, then the StatefulSet has zero pods. Then we update the pod template to the old version, then scale up. But then the StatefulSet will create new pods with the old version. However, the PVCs are already there and will be attached. So if the old version is compatible with the existing data, then it's okay.
Steps for manual rollback of StatefulSet:
Note the current image of the StatefulSet (or the previous image you want to rollback to).
Scale down the StatefulSet to 0 (or to a number that allows for safe update? But we want to rollback all pods).
However, scaling down to 0 will delete all pods and then when we scale up, they will be recreated with the new (old) image. But note that the PVCs are not deleted when pods are deleted (because of reclaim policy). So the data will be there.
But wait, if we scale down to 0, we lose all the pods and the application is down. Then we scale up and the new pods (with old image) will attach the existing PVCs. This might be acceptable if we are rolling back because the current version is broken.
Edit the StatefulSet's pod template to use the old image.
Scale up the StatefulSet to the original number of replicas.
However, if we have multiple replicas, we might not want to scale down to 0 because that would cause complete downtime. We can try to update the pod template and then the StatefulSet will update each pod in order, but if the new (old) image is broken, we might get stuck halfway.
So, the safe way is to scale down to 0, update the pod template, and then scale up. But that causes downtime.
Alternatively, we can do a rolling update with the old image, but if the old image is broken for the current data, we might have to rollback each pod individually? That's messy.
Given the complexity, and because the question asks for a playbook, I think we should provide the Deployment example and note that for StatefulSet we have to do manual rollback and the risks.
But the question says "stateful workload", so I think we should use StatefulSet and then in the playbook, we include a step for manual rollback.
Let's decide:
I will write the playbook for a StatefulSet because that is the standard for stateful workloads with multiple replicas and per-pod storage. Then I will include a section on how to manually rollback.
StatefulSet with volumeClaimTemplates (this will create a PVC for each pod)
PodDisruptionBudget for the StatefulSet
Show how to update the StatefulSet (for a new version) and then how to rollback manually.
However, note that the StatefulSet's update strategy can be set to OnDelete or RollingUpdate. By default, it's RollingUpdate. So when we update the pod template, it will update each pod in order. For rollback, we change the pod template back and it will update again in order.
But if we are in the middle of an update and want to rollback, we can edit the StatefulSet's pod template to the old image and it will continue the update with the old image? Actually, if we change the pod template during an update, the StatefulSet will use the new template for the remaining pods. So if we are updating and the new version is broken, we can edit the StatefulSet to the old image and it will update the remaining pods with the old image. But the pods that already updated to the new version will stay on the new version until they are recreated? Actually, the StatefulSet does not recreated pods that are already updated unless we force an update (by changing the pod template and then deleting the pod). So if we change the pod template during an update, the pods that are already updated will not be changed until we delete them. So we might have a mix of old and new.
That is not a clean rollback.
Therefore, the only way to have a clean rollback for a StatefulSet is to:
Scale down the StatefulSet to 0 (or to a number that allows for safe update? But we want to rollback all, so 0 is safe).
Update the pod template to the old image.
Scale up to the original replica count.
But that causes downtime.
Alternatively, we can do:
For each pod, delete it and wait for it to be recreated with the old image? But that is manual and we have to do for each pod.
Given the complexity, I think we should stick to Deployment for rollback convenience.
But the requirement says "stateful workload", so I think we are expected to use StatefulSet.
After re-thinking, I recall that for stateful applications, we often use operators that handle the rollback in an application-specific way. But the question is about Kubernetes resources.
Given the time, I will provide a playbook for a Deployment that uses a PVC (so it's stateful because it has persistent storage) and then show rollback. And then note that for multiple replicas with per-pod storage, we would use StatefulSet and manual rollback.
But let's see: with a Deployment and a single replica, we have one PVC. That is stateful because the data persists across pod restarts.
So, I think that meets the requirement of a stateful workload.
I will proceed with:
Namespace
StorageClass (if needed, else we assume dynamic provisioning)
PVC
PDB (for the Deployment's pods)
Deployment (with 1 replica, using the PVC)
Rollback example
If we want to scale to multiple replicas, we cannot use the same PVC because of access mode (readWriteOnce). So we would need multiple PVCs. And Deployment does not support creating multiple PVCs automatically. So we are forced to use StatefulSet for multiple replicas with per-pod storage.
Since the question does not specify the number of replicas, I will assume 1.
Playbook:
Create namespace (optional)
Create StorageClass (if not existing and we want dynamic provisioning)
Create PVC
Create PDB
Create Deployment
Then, for rollback:
Update the Deployment to a new image (simulate an update)
Rollback to the previous version.
Let's write the YAML.
However, note: the question asks for a "playbook", which might be in the form of Ansible playbook or just a set of kubectl commands and YAML files. Since the context is Kubernetes, I'll provide YAML files and kubectl commands.
I'll structure:
namespace.yaml
storageclass.yaml (if needed, but we might skip if using default)
pvc.yaml
pdb.yaml
deployment.yaml
Then instructions for update and rollback.
But if we are using dynamic provisioning, we might not need a StorageClass if there is a default one.
Let's assume we have a default StorageClass. Then we only need the PVC.
Steps:
Create the namespace (if desired).
Create the PVC.
Create the PDB.
Create the Deployment.
Then, to simulate an update:
Change the image in the deployment and apply.
Rollback.
Let's write the YAML.
However, note: the PDB must match the labels of the Deployment's pods.
We'll set:
namespace: stateful-app
labels for the deployment: app: my-stateful-app
Then the PDB will match that label.
Let's code:
I'll create a comprehensive Kubernetes playbook for managing stateful workloads with disruption budgets, persistent volumes, and rollback strategies. This playbook uses declarative YAML manifests and includes operational procedures.
Kubernetes Stateful Workload Playbook
1. Prerequisites & Assumptions
Kubernetes cluster v1.20+
kubectl configured with cluster access
Persistent storage provider available (CSI driver, cloud provider storage, etc.)
Application is containerized and stored in a container registry
# View rollout history
kubectl rollout history deployment/app-deployment -n stateful-app
# Rollback to previous revision
kubectl rollout undo deployment/app-deployment -n stateful-app
# Rollback to specific revision
kubectl rollout undo deployment/app-deployment --to-revision=2 -n stateful-app
# Check rollout status
kubectl rollout status deployment/app-deployment -n stateful-app
6.2 StatefulSet Manual Rollback Process
StatefulSets don't have built-in rollback. Use this procedure:
bash
# 1. Get current StatefulSet spec
kubectl get statefulset app-statefulset -n stateful-app -o yaml > current-spec.yaml
# 2. Edit image tag manually
# Change image: myregistry/app:v1.0.0 → myregistry/app:v0.9.0
# 3. Update StatefulSet (triggers rolling update)
kubectl apply -f current-spec.yaml
# 4. Monitor rollout
kubectl rollout status statefulset/app-statefulset -n stateful-app
# 5. If update fails mid-way:
# a) Fix issues
# b) Update spec again with stable image
# c) Continue rollout
6.3 Canary Blue-Green with StatefulSet
yaml
# Canary deployment using partition
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: app-statefulset
spec:
replicas: 5
updateStrategy:
type: RollingUpdate
rollingUpdate:
partition: 3 # Pods 0-2 run old version, 3-4 run new version
# ... rest of spec with new image
# For StatefulSet pods (avoid unless absolutely necessary)
kubectl delete pod app-statefulset-0 -n stateful-app --force --grace-period=0
# Warning: May cause data loss if volume not detached properly!
# Check PV status first: kubectl get pv
11.2 Manual PV Reattachment
bash
# 1. Delete pod (StatefulSet will recreate)
kubectl delete pod app-statefulset-2 -n stateful-app
# 2. If new pod doesn't start due to volume issues:
kubectl patch pv <pv-name> -p '{"spec":{"claimRef": null}}'
# 3. Delete/update PVC to bind to different PV
# 4. Update StatefulSet if ordinal changed
12. Security Considerations
yaml
# security-context.yaml (add to pod spec)
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
seccompProfile:
type: RuntimeDefault
# Pod Security Standards
apiVersion: v1
kind: Pod
metadata:
namespace: stateful-app
spec:
securityContext:
runAsNonRoot: true
containers:
- name: app
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true # Mount data volumes separately
13. Validation Checklist
Before applying to production:
StorageClass exists and supports reclaimPolicy: Retain
Documentation: Version control all manifests and procedures
This playbook provides a production-ready foundation. Adjust storage classes, resource limits, and replica counts based on your specific application requirements and infrastructure capabilities.