Kubernetes Volumes Explained: How Pods Store and Share Data

Question: “How do Pods keep their data persistent in Kubernetes?”
Containers are ephemeral by design — when a container restarts, its data is lost.

To solve this, Kubernetes provides Volumes, a mechanism for Pods to store and share data reliably.

In this post, you will learn:

  • What Volumes are and why they’re needed
  • Different types of Kubernetes Volumes
  • How to configure PersistentVolumes (PV) and PersistentVolumeClaims (PVC)
  • Practical YAML examples for Pod storage

Table of Contents


1. What Is a Volume in Kubernetes?

A Volume is a storage abstraction that can be mounted to one or more containers inside a Pod.

Key points:

  • Volumes can outlive individual container restarts (depending on type).
  • They enable data sharing between containers in the same Pod.
  • They can connect to external storage like NFS, AWS EBS, GCP PD, Ceph, and more.

2. Why Do Pods Need Volumes?

Q: “Can’t we just store data inside the container?”
A: Containers are designed to be stateless and immutable. When they restart, everything inside the container filesystem resets.

Benefits of Volumes:

  • Retain logs, cache, or database data after restarts.
  • Allow multiple containers to share the same data.
  • Enable persistent storage through PV/PVC objects.

3. Types of Kubernetes Volumes

(1) emptyDir

  • A temporary directory created when a Pod starts.
  • Data is deleted when the Pod is removed.
  • Ideal for cache or temporary data.
1
2
3
volumes:
  - name: cache-volume
    emptyDir: {}

  • Mounts a directory from the Node’s filesystem into the Pod.
  • Useful for local development, not recommended in production.
1
2
3
4
volumes:
  - name: host-volume
    hostPath:
      path: /data/logs

  • Provides cluster-wide persistent storage.
  • PVC acts as a request for storage, while PV represents the actual storage resource.
  • Can use cloud storage like AWS EBS, GCP Persistent Disk, or NFS.

4. Mounting Volumes to a Pod

Example: Using emptyDir

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
apiVersion: v1
kind: Pod
metadata:
  name: volume-pod
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: cache-volume
          mountPath: /usr/share/nginx/html
  volumes:
    - name: cache-volume
      emptyDir: {}

This mounts a temporary directory at /usr/share/nginx/html.


5. PersistentVolume & PVC Example

(1) Define a PV

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-example
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-example
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
apiVersion: v1
kind: Pod
metadata:
  name: pvc-pod
spec:
  containers:
    - name: app
      image: nginx
      volumeMounts:
        - mountPath: "/app/data"
          name: storage
  volumes:
    - name: storage
      persistentVolumeClaim:
        claimName: pvc-example

6. Volume Access Modes

Access ModeDescription
ReadWriteOnceRead/write by a single Node
ReadOnlyManyRead-only by multiple Nodes
ReadWriteManyRead/write by multiple Nodes

7. FAQ (Answer Engine Optimization)

Q1. What’s the difference between emptyDir and hostPath? A. emptyDir is tied to the Pod lifecycle and is removed when the Pod is deleted, while hostPath mounts a Node’s local directory into the Pod.

Q2. Can I use a PV without a PVC? A. No, Pods typically use a PVC to request storage. PVC acts as a binding between the Pod and the PV.

Q3. What storage types are best for cloud environments? A. Cloud-specific volumes like AWS EBS, GCP Persistent Disk, and Azure Disk are recommended for persistence and scalability.


8. Key Takeaways

ConceptDescription
VolumeStorage unit attached to a Pod
PV (PersistentVolume)Cluster-wide storage resource
PVC (PersistentVolumeClaim)A Pod’s request for storage
emptyDirTemporary Pod storage
hostPathNode-local storage (development only)
Access ModesDefines how storage is mounted and accessed

Final Thoughts

Persistent data is essential for stateful workloads. By understanding Volumes, PV, and PVC, you can configure Pods to retain and share data across