Kubernetes · Chapter 21 of 28

Volumes and storage

By the end of this chapter you can say where a pod's files live and for how long, share files between two containers of a pod with an emptyDir, keep data across pod restarts with a PersistentVolumeClaim, read kubectl get pv,pvc, find why a claim stays Pending, and say what happens to the data when a claim is deleted. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: More workloads and traffic
  • 4 tasks
  • Kubernetes
  • In the all labs plan

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.

Why you need this

The cart keeps every open basket in Redis: on Dhanteras evening about 18,000 of them, each a customer halfway through an order. Last week Tushar changed Redis's memory setting, the Deployment rolled out a new pod, and every basket in Lucknow was empty at once. Bhola's stores noticed first: 300 customers rang to ask where their carts had gone. Bhola also needs the day's invoices kept in an archive that outlives any pod.

What you know so far cannot keep data. A container's files belong to that container: a restart starts from the image again, and a new pod (after a rollout, a node problem, a kubectl delete pod) starts from nothing. The Deployment controller happily replaces pods because pods are cattle (chapter 3), and that is exactly what wipes the baskets. ConfigMaps and Secrets (chapters 9 and 10) bring files in, but read-only and written by you, not by the app. Redis needs a disk that is not part of any pod, that Kubernetes finds again for whichever pod runs Redis next.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    redis.yaml: Redis keeps its data in a PVC

    Make Redis keep its data when its pod is deleted.

    1. Open redis.yaml. Add a PersistentVolumeClaim named redis-data: 1Gi, ReadWriteOnce, class standard.

    2. Change the Deployment redis so its container mounts that claim at /data. Apply the file and wait for the rollout.

    3. In the new redis pod, use redis-cli to SET the key basket:ritika to atta,ghee,diyas.

    4. Delete that pod with kubectl delete pod. Then GET the key in the new pod.

    To pass: the claim redis-data is Bound. The redis pod running now uses the claim, is newer than your last SET, and says atta,ghee,diyas for basket:ritika. Set the key only after the pod with the volume runs.

  2. Task 2

    receipts.yaml: two containers share an emptyDir

    Run one pod where one container writes a file and the other serves it.

    1. Open receipts.yaml. Make a pod named receipts with one emptyDir volume.

    2. Add a container printer (busybox:1.37). It mounts the volume at /out. When it starts, it writes the line receipt JP-LKO-10231 into /out/index.html, then sleeps.

    3. Add a container web (nginx:1.29). It mounts the same volume at /usr/share/nginx/html.

    4. Apply the file. Read the file inside the web container with kubectl exec and -c web.

    To pass: the pod receipts is Running with both containers ready. Inside web, /usr/share/nginx/html/index.html has the line receipt JP-LKO-10231.

  3. Task 3

    invoices.yaml: why is the PVC Pending?

    Bhola's pod invoice-archive is Pending, and so is its PersistentVolumeClaim invoices. Find why and fix it.

    1. Describe the claim invoices. List the StorageClasses of the cluster.

    2. Open pending.txt. Below the lines that start with #, write the cause in your own words. Name the class the claim asked for, and say what was wrong with it.

    3. Delete the claim invoices. Fix its class in invoices.yaml to standard. Keep 2Gi and ReadWriteOnce.

    4. Apply invoices.yaml again. Wait for the pod to start.

    To pass: pending.txt names the class and the problem. The claim invoices is Bound with the class standard, 2Gi, ReadWriteOnce. The pod invoice-archive runs with the claim mounted at /archive. A claim's class cannot be changed in place: delete it first.

  4. Task 4 · interview question

    Interview: deleting a PVC

    Interview question: what happens to the data when a PVC is deleted?

    1. Open answer.txt. Write below the lines that start with #.

    2. In a sentence or two of your own, name the setting that decides it.

    3. Say what happens to the volume and its data under Delete, and under Retain.

    To pass: your answer mentions the reclaim policy, and says what happens to the data with Delete and with Retain. Lines that start with # do not count. Ammar will ask: "Tushar deleted the redis-data claim by mistake. Are the baskets gone?"

What you will learn

  • A container's own files: gone when the container restarts.
  • emptyDir: a folder for a pod's containers to share, gone with the pod.
  • PersistentVolume and PersistentVolumeClaim: a disk, and a request for one.
  • StorageClass and dynamic provisioning: disks made on demand (the lab's standard).
  • Access modes and the reclaim policy: who may mount it, and what deleting the claim does.
  • A claim that stays Pending, and hostPath, with why not to use it.

More in More workloads and traffic

Build "Volumes and storage" yourself

Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.