
Module 6 Exercises: Stateful Storage
Master the persistence layer. Deploy a stateful cluster, explore ordinal naming, and practice disk expansion.
Module 6 Exercises: Stateful Storage
In Module 6, we explored how Kubernetes gives "Memory" to our applications. You learned about ephemeral volumes, the architecture of StatefulSets, and how to automate disks with StorageClasses. These exercises will put that knowledge to the test.
Exercise 1: The "Scratchpad" Sidecar
Create a Pod with two containers: app and helper.
- Requirement: Both containers must share a directory at
/shared-data. - Task: Use an
emptyDirvolume to facilitate this. - Verification:
kubectl execintoappand create a file.kubectl execintohelperand verify you can see it.
- Question: If you restart the
appcontainer (but not the pod), does the data remain? If you delete the Pod, does the data remain?
Exercise 2: Deploying a "Pet" Cluster
Create a StatefulSet named data-store with 3 replicas.
- Image: Use
redis:alpine. - Service: Create a Headless Service named
data-store-svc. - Task: Verify the naming convention of the pods.
- Goal: Find the specific DNS name you would use to ping ONLY the second pod (
data-store-1) from within the cluster.
Exercise 3: Dynamic Disk Scaling
- Creation: Create a PersistentVolumeClaim named
expandable-pvcwith1Giof storage. Ensure yourStorageClasshasallowVolumeExpansion: true. - Action: Update the PVC YAML to request
2Gi. - Verification: After applying the change, use
kubectl describe pvc expandable-pvc. What message do you see in the "Events" section? - Final Check: Does the disk resize immediately, or does the Pod need to be restarted? (Hint: Check the "FileSystemResizePending" condition).
Exercise 4: Snapshot and Clone
(Note: This requires a Snapshot Controller in your cluster).
- Task: Create a
VolumeSnapshotof yourexpandable-pvc. - Action: Create a new PVC named
pvc-cloneand set itsdataSourceto the snapshot you just created. - Observation: Start a new Pod using
pvc-clone. Verify that all files from the original PVC are present in the clone.
Solutions (Self-Check)
Exercise 1 Answer:
- If the container restarts: Yes, the data remains! The
emptyDiris tied to the Pod's lifecycle, not the container's. - If the Pod is deleted: No, the data is purged.
Exercise 2 Solution:
The pods will be named data-store-0, data-store-1, and data-store-2.
The sticky DNS name for the second pod is: data-store-1.data-store-svc.default.svc.cluster.local.
Exercise 3 Hint:
You will see an event like ExternalExpanding followed by FileSystemResizePending.
In modern Kubernetes, for many filesystems (like XFS/Ext4), the resize happens Live while the pod is running! You don't need a restart.
Exercise 4 Logic:
This is the "Golden Path" for testing database updates. You snapshot production, clone it to a new PVC, and run your dangerous migration scripts on the clone first!
Summary of Module 6
You have mastered the most persistent part of Kubernetes.
- You know when to use emptyDir vs PVs.
- You can manage StatefulSets and their sticky identities.
- You understand how StorageClasses automate the cloud.
- You can perform Volume Snapshots for disaster recovery.
In Module 7: Configuration and Secrets Management, we will look at how we manage the "Brains" and "Identity" of these stateful applications.