Capstone: recover stateful data
Restore a deliberately deleted file and prove that the restored data persists after the consumer Pod is replaced.
What you will understand
- Distinguish deletion inside a persistent volume from loss of a Pod.
- Validate a host-side backup before copying it back.
- Compare the recovered bytes through the original and replacement Pods.
Builds on: Termination, disruption, and drain, Backup, restore, and disaster recovery, Capstone: recover a stateless app
Distinguish Pod replacement from a deleted file
A worker writes an order record to /data/orders.csv on a persistent volume. Later the file is deleted by mistake. Recreating the Pod with the same claim leaves the data in its current state, including the missing file. Your task is to take a small backup before the loss, restore the data, and prove that a new Pod sees the recovered bytes. Crash and replacement recovery exercise different mechanisms from recovering an application record deleted on disk.
The PVC below requests 64 MiB. The lab’s writer mounts that claim and writes a known fixture. The first scene lets you compare replacement with deletion inside the volume. Before running the loss step, record the expected value and the backup’s checksum.
Why does a deleted record stay deleted after Pod replacement?
The claim preserves changes on disk, including deletion. The lab’s independent host copy supplies the record needed to restore it.
Write the order record
- m20-data PodPVC m20write complete record
The single writer finishes the CSV record before a backup is captured.
Capture a verified host copy
- PVC m20Private host backupstream quiescent file
The script captures the bounded file in a private host directory and verifies its exact bytes before deleting the live file.
Remove the original order file
- Logical deletionPVC m20remove current file
The script removes the file from the mounted PVC and verifies absence; a Pod restart cannot undo this deletion.
The CSV is quiescent during copy; this does not model concurrent database transaction consistency.
Explore another case
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: m20
namespace: kv-lab
labels:
lab: m20
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 64MiRestore known bytes from a host backup
The lab uses a single small fixture with no concurrent writer while the backup is captured. The script copies it out to a private host directory and checks the exact expected bytes. It deletes the file only after that check. A real database can have open transactions and internal buffers, so choose a database-supported backup procedure and test the recovery point before relying on it.
The restore uses kubectl cp with a container image that has tar. It verifies the copied bytes against the host backup before removing the original Pod. The replacement mounts the same PVC, and the script reads the file again from inside that Pod, comparing length and checksum. Follow both checkpoints in the restore scene.
Verify the restored file before and after replacement
Copy the host backup into the mounted volume, validate the content, and read it again from a new Pod using the same claim.
- Host backupCopy orders.csv to the mounted directoryWriter PodThe expected bytes are present on the claimed volume.
- Writer PodCheck the restored checksumRecovery recordThe copied file matches the backup before replacing the consumer.
- Replacement PodMount the same claim and read the filePersistent volumeThe expected bytes are still accessible from the new process.
Copy the host backup into the mounted volume, validate the content, and read it again from a new Pod using the same claim.
The actual lab has one completed CSV write and tests file restoration, not database recovery or node-loss resilience.
Explore another case
Verify the file before and after replacement
Use all files from the stateful capstone. Run the staged procedure if you want to observe and diagnose each boundary. Record the original Pod UID and the recovered Pod UID, confirm the claim stayed bound, and compare file bytes at both checkpoints. The read-only follow-up commands display the synthetic fixture. In a real incident, prefer integrity evidence you can safely share over exposing private records.
# Run in the downloaded m20 directory.
bash capstone-stateful.sh
# Verify the backup is retained on the host and the restored value is mounted.
backup_dir=$(python3 backup-path.py m20)
cat "$backup_dir/orders.csv"
kubectl --context kind-kv-lab -n kv-lab exec m20-data -- cat /data/orders.csv
kubectl --context kind-kv-lab -n kv-lab get pvc m20 -o wide
kubectl --context kind-kv-lab -n kv-lab get pod m20-data -o jsonpath='{.metadata.uid}{"\n"}'Test the recovery your application needs
A new Pod name or a ready status does not tell you whether it restored the intended data. Verify records that matter to the application and the time period you need. This exercise gives direct evidence for file recovery and persistence on the same claim. Recovering from a lost node, a lost volume, or an inconsistent database requires additional scenarios.
Check your understanding
The checksum matched in the first Pod just after restore. What does the second check add?
It proves that a newly started consumer can mount the same claim and read the restored bytes. That helps catch a copy sent to the wrong directory or a value that existed only in the original container’s writable layer.
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise