0%
Kubernetes CKA and CKAD Exam Prep: Hands On Task Drills and Mock Exams
How the Exams Work
Administrator (CKA) Skills
Mock Exams
Storage: Volumes, PVs, PVCs and StorageClasses
Storage tasks are worth real points on both exams, and they follow a small number of patterns: give a pod some scratch space, mount a PersistentVolumeClaim, create a PV and PVC that bind to each other, write a StorageClass, fix a claim stuck in Pending, reuse a retained volume, expand a claim, and give a StatefulSet per pod storage. This lesson works through each pattern with the YAML you will type, the field that usually goes wrong, and the commands that prove it worked. The containers course covered why persistent storage is hard; here the goal is speed and correctness.
The first section covers volumes that live inside the pod spec itself. They need no extra objects, and they appear in many tasks that are not really about storage, such as sidecars sharing log files.
The volume and mount pair
Every volume has two halves. The pod spec declares it under spec.volumes with a name and a source. Each container that uses it lists a volumeMounts entry with the same name and a mountPath.
The same volume can be mounted at different paths in different containers, and one container can mount it read only while another writes. The names must match exactly. A volumeMounts entry that names a volume not declared in spec.volumes is rejected by the API server with a clear error, which is at least easy to spot. A declared volume that no container mounts is accepted silently and does nothing, which is harder to spot.
emptyDir
An emptyDir is created empty when the pod is assigned to a node and deleted when the pod is removed from that node. It survives container restarts, because it belongs to the pod, not the container. It does not survive the pod being deleted, evicted or rescheduled.
By default it lives on the node's disk, in the kubelet's directory. Two options change that:
medium: Memory puts it in tmpfs, which is fast and is counted against the container's memory limit, so a full memory backed emptyDir can get the pod OOM killed. sizeLimit caps usage; if a pod writes past it, the kubelet evicts the pod. Use emptyDir for caches, scratch space for sorting or processing, and sharing files between containers, such as a log file that a sidecar ships.
hostPath
A hostPath volume mounts a file or directory from the node's own filesystem into the pod.
The type field validates what exists at the path: Directory and File require it to exist, DirectoryOrCreate and FileOrCreate create it if missing, and Socket, CharDevice and BlockDevice check for special files. An empty type does no check at all.
hostPath is powerful and risky. The data is tied to one node, so a pod rescheduled elsewhere sees a different directory. Writing to host paths can let a container modify the node, which is why Pod Security's baseline level forbids hostPath. Legitimate uses are node agents such as log collectors and monitoring daemons, usually run as DaemonSets. On the exam, hostPath appears mostly as the backing for a simple PersistentVolume in a single node practice cluster, which the next section uses.
configMap, secret, projected and downwardAPI
You met configMap, secret and projected volumes in lesson five. The downwardAPI volume exposes the pod's own metadata, such as labels, annotations and resource limits, as files:
Labels and annotations exposed this way update in place when they change. The same information is available as environment variables through env.valueFrom.fieldRef, for example status.podIP or spec.nodeName, but environment variables do not update. A task that asks a container to know "its own pod name and namespace" is answered with fieldRef to metadata.name and metadata.namespace.
Generic ephemeral volumes
A generic ephemeral volume is a PVC created for the pod and deleted with it. It gives you the features of a real storage class, such as a particular disk type, a larger size than node scratch space or snapshots, with the lifetime of an emptyDir.
The PVC is named PODNAME-VOLUMENAME and is owned by the pod, so garbage collection removes it when the pod goes. This is a newer and less common pattern, but it is worth recognising when a task describes "per pod scratch storage from the fast class".
Verifying a mount
For any volume task, check from inside the container.
df -h shows what is mounted at the path and its size, which distinguishes an emptyDir on disk from tmpfs and from a network volume. describe lists each volume with its type and source. When a pod is stuck in ContainerCreating, the events at the bottom of describe almost always name the failing volume, with a FailedMount or FailedAttachVolume reason. Read them first; they save you from guessing.
Choosing the right volume type
Most volume questions come down to one decision: how long must the data live, and who must see it? Use this short guide when a task describes the need in words rather than naming the type.
| Need | Volume type | Lifetime |
| Share files between containers in one pod | emptyDir | the pod |
| Fast scratch space in memory | emptyDir with medium Memory | the pod |
| Read node files from a node agent | hostPath | the node |
| Inject configuration or credentials | configMap, secret, projected | the source object |
| Expose the pod's own labels or limits | downwardAPI | the pod |
| Per pod scratch from a real storage class | generic ephemeral | the pod |
| Data that must outlive the pod | persistentVolumeClaim | the claim |
The last row is the important boundary. Anything that must survive the pod being deleted, rescheduled or replaced belongs on a PersistentVolumeClaim, which the next four sections cover. If a task says "the data must still be there after the pod is recreated", an emptyDir answer is wrong even if every other field is right.
Init containers and volumes
Init containers can mount the same volumes as the main containers, and they run to completion before the main containers start. That makes them the standard way to prepare a volume: download a file, render a configuration template or set permissions.
If the init container fails, the pod shows Init:Error or Init:CrashLoopBackOff, and kubectl logs POD -c fetch shows why. The main container never starts until every init container succeeds, so the web server above always finds the file in place.
subPath mounts
A subPath mounts a single file or subdirectory of a volume instead of its root, which lets you place one file into a directory that already has other content. Remember the trade off from lesson five: a subPath mount of a ConfigMap or Secret does not receive live updates. Use it when you need a single file in a crowded directory and do not need live updates.
Type spec.volumes and the matching volumeMounts in the same edit and copy the name between them. A volume with no mount is accepted silently and is the most common reason a pod looks right but sees an empty directory.