KUBERNETES · KCNA · CHAPTER 7

Storage

Containers are ephemeral; data isn't. This is how you store data so it outlives the pods.

14 min readKCNA domain: Container Orchestration (28%)Level: beginner

From point A to point B

Point A: you know how to deploy, secure and connect applications, but everything a container writes disappears when the container dies. Point B: by the end of this chapter you'll know how Docker stores data and how Kubernetes gives pods storage that outlives them, whether it's prepared by hand or created on demand.

Storage is part of the Container Orchestration domain (28% of the KCNA exam). We start with Docker, because understanding its layers and volumes is the foundation for everything else.

What you'll cover
  1. Image layers and copy-on-write in Docker
  2. Volumes, bind mounts and drivers
  3. CSI, the storage interface
  4. PersistentVolumes and PersistentVolumeClaims
  5. StorageClasses and dynamic provisioning

Storage in Docker

Docker keeps everything under /var/lib/docker: images, containers and volumes. To understand why a container's data gets lost, you need to see how an image is built.

An image is a stack of read-only layers. Each Dockerfile instruction (FROM, RUN, COPY…) creates a new layer with whatever changed from the previous one. Layers are reused: if ten images start from the same base, that base is downloaded and stored only once.

When a container starts, Docker adds its own writable layer on top. Everything the container creates or changes goes there. If the container changes a file that comes from the image, Docker first copies it to that layer and changes it there: this is the copy-on-write mechanism. The image never changes, which is why many containers can share it.

Image layers and copy-on-write A Docker image is a stack of read-only layers: the base system, packages, dependencies, the source code and the entrypoint. Each container adds a thin writable layer on top. When the container changes a file from the image, it first copies it into its own layer. Deleting the container deletes that layer. CONTAINER LAYER · read-write your changes · deleted with the container IMAGE LAYERS · read-only 5 · ENTRYPOINT 4 · COPY source code 3 · RUN pip install -r requirements.txt 2 · RUN apt-get install python3 1 · FROM ubuntu COPY-ON-WRITE the container edits app.py → a copy goes up to its layer → the image stays untouched shared by every container

Image layers are read-only; each container writes to its own layer. Diagram: The House of Routing.

The consequence is direct: when you delete the container, its writable layer is deleted with it. A database that keeps its files there loses everything.

Volumes, bind mounts and drivers

For data to survive, you store it outside the container's layer. Docker offers two ways:

  • Volume: a directory managed by Docker, inside /var/lib/docker/volumes. You create it with docker volume create and mount it in the container.
  • Bind mount: you mount any folder that already exists on the host, for example /srv/mysql. You decide where it is and who else uses it.
docker info | grep "Storage Driver"      # usually overlay2 on Linux
ls /var/lib/docker/                      # images, containers, volumes…
docker history nginx:1.27-alpine         # the layers of an image

docker volume create data                # a volume managed by Docker
docker run -d --mount type=volume,source=data,target=/var/lib/mysql mysql:8.4
docker run -d --mount type=bind,source=/srv/mysql,target=/var/lib/mysql mysql:8.4

Behind this are two kinds of components that are easy to confuse:

  • Storage drivers (such as overlay2): they manage the image layers and copy-on-write. They depend on the operating system, and Docker picks the best one available.
  • Volume drivers: they decide where volumes live. The built-in one is local, on the host's disk, but there are plugins to store them on network storage or in the cloud.

CSI: the storage interface

Kubernetes doesn't want to depend on a specific runtime, network or storage system. That's why it talks to all of them through standard interfaces:

  • CRI (Container Runtime Interface): for container runtimes, such as containerd or CRI-O.
  • CNI (Container Network Interface): for the network plugins you saw in chapter 5.
  • CSI (Container Storage Interface): for storage.
CRI, CNI and CSI Kubernetes talks to three kinds of plugins through standard interfaces: the Container Runtime Interface for runtimes such as containerd and CRI-O, the Container Network Interface for network plugins such as Calico and Cilium, and the Container Storage Interface for storage drivers such as AWS EBS, Ceph or NFS. KUBERNETES kubelet · controllers CRI · RUNTIME containerd CRI-O CNI · NETWORK Calico · Cilium Flannel CSI · STORAGE AWS EBS · Azure Disk GCE PD · Ceph · NFS

Kubernetes talks to runtimes, networks and storage through standard interfaces. Diagram: The House of Routing.

CSI defines a set of common calls, such as creating, deleting, attaching or expanding a volume. Each provider writes its own CSI driver that implements them: AWS EBS, Azure Disk, Google Persistent Disk, Ceph, NetApp, NFS and many more. That way, any storage with a CSI driver works on any orchestrator that speaks CSI, without touching the Kubernetes code.

Volumes in Kubernetes

In Kubernetes, a volume is declared in the pod and mounted in one or more of its containers. There are many types; these are the ones you'll see most:

TypeWhat it isHow long it lasts
emptyDirAn empty directory to share files between the pod's containersAs long as the pod
hostPathA folder on the node where the pod runsAs long as the node
configMap / secretConfiguration or secrets mounted as filesAs long as the object
persistentVolumeClaimPersistent storage, independent of the pod and the nodeUntil you delete it
apiVersion: v1
kind: Pod
metadata:
  name: random-number
spec:
  containers:
    - name: app
      image: busybox:1.36
      command: ["sh", "-c", "shuf -i 0-100 -n 1 >> /opt/number.out; sleep 3600"]
      volumeMounts:
        - name: data
          mountPath: /opt
  volumes:
    - name: data
      hostPath:              # a folder of the node: fine for labs, not for production
        path: /data
        type: DirectoryOrCreate
Be careful with hostPath. The data stays on one specific node. If the pod is rescheduled on another node, it won't find it. It also gives access to the node's file system, which is a security risk. Use it in labs; in production, use persistent storage.

PersistentVolumes

Declaring storage inside every pod doesn't scale: with hundreds of pods, every team would need to know the details of the NFS server or the cloud disk. Kubernetes splits the two responsibilities.

A PersistentVolume (PV) is a piece of cluster storage that the administrator prepares. It's a cluster-level object, not a namespaced one, and it describes its capacity, its access modes, what to do with the data when it's released and where it physically lives.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-data
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  nfs:
    server: nfs.example.com
    path: /exports/data
Access modeShort formMeaning
ReadWriteOnceRWORead and write from a single node
ReadOnlyManyROXRead-only from many nodes
ReadWriteManyRWXRead and write from many nodes
ReadWriteOncePodRWOPRead and write from a single pod

The reclaim policy (persistentVolumeReclaimPolicy) says what happens when the PVC that used it is deleted:

  • Retain: the PV and its data are kept, in the Released state, until an administrator decides what to do.
  • Delete: the PV and the actual storage are deleted. This is the usual setting for dynamically created volumes.

PersistentVolumeClaims

A PersistentVolumeClaim (PVC) is the developer's request: "I need 2Gi with read and write access from several nodes." It lives in a namespace and knows nothing about NFS or disks.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-data
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: manual
  resources:
    requests:
      storage: 2Gi

Kubernetes looks for a PV that meets the request: the same storageClassName, at least the requested size and a compatible access mode. When it finds one, it binds them. The relationship is one-to-one: even if the PV has 5Gi and only 2Gi are requested, nobody else can use the remaining 3Gi. If there's no suitable PV, the PVC stays Pending until one shows up.

PersistentVolumes and PersistentVolumeClaims An administrator creates PersistentVolumes that point to real storage. A developer creates a PersistentVolumeClaim asking for 2Gi with ReadWriteMany. Kubernetes binds the claim to a volume that matches, and the pod mounts the claim by its name. ADMINISTRATOR PV · pv-small 1Gi · RWO PV · pv-data 5Gi · RWX · NFS PV · pv-ssd 50Gi · RWO DEVELOPER PVC · pvc-data asks for 2Gi · RWX POD · web claimName: pvc-data BIND 1:1 same storageClassName, enough size, compatible access mode

The administrator offers PVs, the developer asks with a PVC and Kubernetes binds them. Diagram: The House of Routing.

The pod no longer mentions the actual storage: only the PVC's name.

apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
    - name: nginx
      image: nginx:1.27-alpine
      volumeMounts:
        - name: content
          mountPath: /usr/share/nginx/html
  volumes:
    - name: content
      persistentVolumeClaim:
        claimName: pvc-data
kubectl get pv,pvc            # STATUS: Available, Bound, Released
kubectl describe pvc pvc-data
kubectl delete pvc pvc-data   # stays "Terminating" while a pod uses it
Protection included. If you delete a PVC while a pod is using it, Kubernetes doesn't remove it right away: it leaves it in Terminating until the pod stops using it. That way you don't lose data to an accidental delete.

StorageClasses and dynamic provisioning

With static PVs, someone has to create the disk and the PV before every request. In the cloud, that makes no sense. A StorageClass describes a kind of storage, and when a PVC that names it arrives, its CSI driver creates the disk and the PV on the spot. This is dynamic provisioning, the usual way of working today.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast-ssd
provisioner: ebs.csi.aws.com      # the CSI driver that creates the disks
parameters:
  type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
  • provisioner: the CSI driver that creates the volumes.
  • parameters: provider options, such as the disk type.
  • reclaimPolicy: the policy for the PVs it creates, Delete by default.
  • volumeBindingMode: WaitForFirstConsumer: waits until a pod uses the PVC, so it creates the disk in the same zone as that pod's node.
  • allowVolumeExpansion: lets you expand the volume by editing the PVC.

The PVC only has to name the class. If it doesn't name one, the cluster's default class is used.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: db-data
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: fast-ssd     # no PV needed: the class creates it
  resources:
    requests:
      storage: 20Gi
Dynamic provisioning with a StorageClass A PersistentVolumeClaim names the StorageClass fast-ssd. The class calls its CSI driver, which creates a 20Gi disk in the cloud and the matching PersistentVolume. Kubernetes binds the claim to it. Nobody had to create the PV by hand. PVC · db-data 20Gi · fast-ssd STORAGECLASS fast-ssd CSI DRIVER ebs.csi.aws.com CLOUD DISK gp3 · 20Gi PV created on demand bound automatically to the claim

The PVC names a StorageClass, and its CSI driver creates the disk and the PV. Diagram: The House of Routing.

kubectl get storageclass      # the default class shows "(default)"
kubectl get csidrivers        # CSI drivers installed in the cluster
kubectl get pv                # PVs created on demand by the class
One step further. Databases with several replicas are deployed with a StatefulSet, which uses volumeClaimTemplates to create a separate PVC for each replica, with its own disk.

KCNA-style challenge

Answer before you open the solution. Getting it wrong here is the cheapest way to learn.

1. A container changes a file that comes from its image. Where does the change end up?

In the container's writable layer. With copy-on-write, the file is copied there and the image doesn't change.

2. Which standard lets Kubernetes use storage from any provider?

CSI, the Container Storage Interface. CRI is for runtimes and CNI is for networking.

3. A PVC requests 2Gi and the only available PV has 5Gi, with the same class and access mode. What happens?

They bind, and the extra 3Gi go unused: the relationship between a PV and a PVC is one-to-one.

4. You delete the PVC of a PV with the Retain policy. What happens to the data?

It's kept. The PV moves to Released and isn't reused until an administrator frees it by hand.

5. With dynamic provisioning, who creates the PersistentVolume?

The StorageClass, through its CSI driver, when a PVC that names it arrives.

Key takeaways

  • An image is a stack of read-only layers; each container adds a writable layer that dies with it.
  • Copy-on-write: the container copies what it changes to its own layer, and the image doesn't change.
  • Docker stores data outside the container with volumes or bind mounts.
  • Kubernetes uses standard interfaces: CRI for runtimes, CNI for networking and CSI for storage.
  • emptyDir lasts as long as the pod; hostPath ties the data to one node.
  • The administrator prepares the PV, the developer requests the PVC, and they bind one-to-one.
  • Access modes: RWO, ROX, RWX and RWOP. Reclaim policies: Retain and Delete.
  • A StorageClass creates PVs on demand: that's dynamic provisioning.

This chapter wraps up the Container Orchestration domain. The next chapter begins cloud native architecture: autoscaling, serverless, open standards and community roles.

Official resources

Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's KCNA course (January to April 2026). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details and YAML examples were checked against the official Docker, Kubernetes and CNCF documentation.

Navigate