Module 6 Exercises: Stateful Storage

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 emptyDir volume to facilitate this.
  • Verification:
    1. kubectl exec into app and create a file.
    2. kubectl exec into helper and verify you can see it.
  • Question: If you restart the app container (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

  1. Creation: Create a PersistentVolumeClaim named expandable-pvc with 1Gi of storage. Ensure your StorageClass has allowVolumeExpansion: true.
  2. Action: Update the PVC YAML to request 2Gi.
  3. Verification: After applying the change, use kubectl describe pvc expandable-pvc. What message do you see in the "Events" section?
  4. 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).

  1. Task: Create a VolumeSnapshot of your expandable-pvc.
  2. Action: Create a new PVC named pvc-clone and set its dataSource to the snapshot you just created.
  3. 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 emptyDir is 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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn