Introduction to Kubernetes Storage Volumes and Their Types
This article provides an overview of Kubernetes storage options, describing temporary and persistent volume types such as EmptyDir, HostPath, ConfigMap, Secret, PersistentVolume, PersistentVolumeClaim, StorageClass, and NFS, and includes practical YAML examples for creating and using each storage solution in a cluster.
Kubernetes (K8s) offers a variety of storage types to satisfy different application needs. The main categories include temporary storage, persistent storage, ConfigMap and Secret, and the special EmptyDir volume.
Temporary Storage – short‑lived and volatile, usually created on local storage and automatically removed when the Pod terminates.
Persistent Storage – survives Pod termination and can be provisioned by plugins such as NFS, GlusterFS, or Ceph. It is requested via a PersistentVolumeClaim (PVC) and supplied by a PersistentVolume (PV).
ConfigMap and Secret – special volume types that let you inject configuration files or encrypted data into containers, either by mounting them as volumes or exposing them as environment variables.
EmptyDir – a temporary directory created on each node for a Pod; it is recreated on a new node when the Pod moves.
Kubernetes supports many volume types that can be mounted into Pods. Common examples include:
EmptyDir – temporary, node‑local, deleted with the Pod.
HostPath – maps a host directory into a container (useful for testing, not recommended for production).
PersistentVolume (PV) and PersistentVolumeClaim (PVC) – cluster‑wide persistent storage resources.
ConfigMap and Secret – inject configuration or sensitive data.
StorageClass – defines a class of storage with specific parameters and behavior.
Example: Using EmptyDir to store log files
<span><span>apiVersion</span>: <span>v1</span></span></code><code><span><span>kind</span>: <span>Pod</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>logging-pod</span></span></code><code><span><span>spec</span>:</span></code><code><span> <span>containers</span>:</span></code><code><span> <span>-</span> <span>name: log-generator</span></span></code><code><span> <span>image</span>: <span>busybox</span></span></code><code><span> <span>command</span>:</span></code><code><span> <span>-</span> <span>sh</span></span></code><code><span> <span>-</span> <span>"-c"</span></span></code><code><span> <span>-</span> <span>"while true; do echo 'Logging message' >> /data/log.txt; sleep 1; done"</span></span></code><code><span> <span>volumeMounts</span>:</span></code><code><span> <span>-</span> <span>mountPath: /data</span></span></code><code><span> <span>name</span>: <span>log-data</span></span></code><code><span> <span>volumes</span>:</span></code><code><span> <span>-</span> <span>name: log-data</span></span></code><code><span> <span>emptyDir</span>: <span>{}</span></span>This pod creates a temporary emptyDir volume at /data where the container continuously writes log messages.
Example: Injecting a ConfigMap into a Pod
<span><span>apiVersion</span>: <span>v1</span></span></code><code><span><span>kind</span>: <span>ConfigMap</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>my-configmap</span></span></code><code><span><span>data</span>:</span></code><code><span> <span>app.properties</span>: <span>|</span></span></code><code><span> <span>username</span>=<span>admin</span></span></code><code><span> <span>password</span>=<span>secret</span></span></code><code><span><span>---</span></span></code><code><span><span>apiVersion</span>: <span>v1</span></span></code><code><span><span>kind</span>: <span>Pod</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>app-pod</span></span></code><code><span><span>spec</span>:</span></code><code><span> <span>containers</span>:</span></code><code><span> <span>-</span> <span>name: app-container</span></span></code><code><span> <span>image</span>: <span>my-app</span></span></code><code><span> <span>envFrom</span>:</span></code><code><span> <span>-</span> <span>configMapRef:</span></span></code><code><span> <span>name</span>: <span>my-configmap</span></span>The ConfigMap provides configuration data that the container consumes as environment variables.
Example: PersistentVolume and PersistentVolumeClaim
<span><span>apiVersion</span>: <span>v1</span></span></code><code><span><span>kind</span>: <span>PersistentVolume</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>pv0001</span></span></code><code><span><span>spec</span>:</span></code><code><span> <span>capacity</span>:</span></code><code><span> <span>storage</span>: <span>5Gi</span></span></code><code><span> <span>accessModes</span>:</span></code><code><span> <span>-</span> <span>ReadWriteOnce</span></span></code><code><span><span>nfs</span>:</span></code><code><span> <span>path</span>: <span>/exports/my-nfs</span></span></code><code><span> <span>server</span>: <span>my-nfs-server.example.com</span></span></code><code><span><span>---</span></span></code><code><span><span>apiVersion</span>: <span>v1</span></span></code><code><span><span>kind</span>: <span>PersistentVolumeClaim</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>pvc-claim0001</span></span></code><code><span><span>spec</span>:</span></code><code><span> <span>accessModes</span>:</span></code><code><span> <span>-</span> <span>ReadWriteOnce</span></span></code><code><span> <span>resources</span>:</span></code><code><span> <span>requests</span>:</span></code><code><span> <span>storage</span>: <span>5Gi</span></span>The PV is backed by an NFS server, and the PVC requests 5 GiB of that storage for use by a Pod.
Example: HostPath Volume
<span>mkdir -p /data/hostpath</span></code><code><span>echo "Hello from host file system!" > /data/hostpath/hello.txt</span> <span><span>apiVersion</span>: <span>apps/v1</span></span></code><code><span><span>kind</span>: <span>Deployment</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>hostpath-test</span></span></code><code><span><span>spec</span>:</span></code><code><span> <span>replicas</span>: <span>1</span></span></code><code><span> <span>selector</span>:</span></code><code><span> <span>matchLabels</span>:</span></code><code><span> <span>app</span>: <span>hostpath-test</span></span></code><code><span> <span>template</span>:</span></code><code><span> <span>metadata</span>:</span></code><code><span> <span>labels</span>:</span></code><code><span> <span>app</span>: <span>hostpath-test</span></span></code><code><span> <span>spec</span>:</span></code><code><span> <span>containers</span>:</span></code><code><span> <span>-</span> <span>name: test-container</span></span></code><code><span> <span>image</span>: <span>busybox:latest</span></span></code><code><span> <span>command</span>: <span>["sh", "-c", "cat /mnt/data/hostpath/hello.txt && tail -f /dev/null"]</span></span></code><code><span> <span>volumeMounts</span>:</span></code><code><span> <span>-</span> <span>mountPath: /mnt/data/hostpath</span></span></code><code><span> <span>name</span>: <span>hostpath-vol</span></span></code><code><span> <span>volumes</span>:</span></code><code><span> <span>-</span> <span>name: hostpath-vol</span></span></code><code><span> <span>hostPath</span>:</span></code><code><span> <span>path</span>: <span>/data/hostpath</span></span>This deployment mounts the host directory into the container, allowing the container to read the file created on the host.
Example: NFS Volume
<span><span>sudo</span> <span>apt-get install nfs-kernel-server</span></span></code><code><span><span>sudo</span> <span>mkdir -p /nfs/shared</span></span></code><code><span><span>sudo</span> <span>chmod -R 777 /nfs/shared/</span></span></code><code><span><span>sudo</span> <span>nano /etc/exports</span></span></code><code><span>/nfs/shared *(rw,sync,no_subtree_check)</span></code><code><span><span>sudo</span> <span>service nfs-kernel-server restart</span></span> <span><span>apiVersion</span>: <span>v1</span></span></code><code><span><span>kind</span>: <span>PersistentVolume</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>nfs-volume</span></span></code><code><span><span>spec</span>:</span></code><code><span> <span>capacity</span>:</span></code><code><span> <span>storage</span>: <span>1Gi</span></span></code><code><span> <span>accessModes</span>:</span></code><code><span> <span>-</span> <span>ReadWriteMany</span></span></code><code><span><span>nfs</span>:</span></code><code><span> <span>path</span>: <span>/nfs/shared</span></span></code><code><span> <span>server</span>: <span>YOUR_NFS_SERVER_IP</span></span> <span><span>apiVersion</span>: <span>v1</span></span></code><code><span><span>kind</span>: <span>PersistentVolumeClaim</span></span></code><code><span><span>metadata</span>:</span></code><code><span> <span>name</span>: <span>nfs-claim</span></span></code><code><span><span>spec</span>:</span></code><code><span> <span>accessModes</span>:</span></code><code><span> <span>-</span> <span>ReadWriteMany</span></span></code><code><span> <span>resources</span>:</span></code><code><span> <span>requests</span>:</span></code><code><span> <span>storage</span>: <span>1Gi</span></span>After creating the PV and PVC, a Pod can mount the NFS share to share data across nodes.
StorageClass
<span>apiVersion: storage.k8s.io/v1</span></code><code><span>kind: StorageClass</span></code><code><span>metadata:</span></code><code><span> name: gp2</span></code><code><span>provisioner: kubernetes.io/aws-ebs</span></code><code><span>parameters:</span></code><code><span> type: gp2</span>A PVC that references this StorageClass requests 5 GiB of storage:
<span>apiVersion: v1</span></code><code><span>kind: PersistentVolumeClaim</span></code><code><span>metadata:</span></code><code><span> name: ebs-claim</span></code><code><span>spec:</span></code><code><span> accessModes:</span></code><code><span> - ReadWriteOnce</span></code><code><span> storageClassName: gp2</span></code><code><span> resources:</span></code><code><span> requests:</span></code><code><span> storage: 5Gi</span>The PVC can then be mounted into a Pod to provide persistent storage backed by AWS EBS.
Other Volume Types
Kubernetes also supports GlusterFS, Ceph, iSCSI, Azure Disk, Local Volume, CSI drivers, and Ephemeral volumes. Each can be defined via a PersistentVolume and claimed with a PVC, allowing flexible storage solutions for various workloads.
Overall, understanding the different volume types, their use‑cases, and how to configure them with YAML manifests is essential for building reliable, stateful applications on Kubernetes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
