codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☸️

Kubernetes

11 / 82 topics
9Managing Configuration with ConfigMaps10Handling Secrets in Kubernetes11Persistent Storage with Volumes12Implementing Network Policies
Tutorials/Kubernetes/Persistent Storage with Volumes
☸️Kubernetes

Persistent Storage with Volumes

Updated 2026-05-15
10 min read

Persistent Storage with Volumes

Introduction

In the world of container orchestration, managing data persistence is crucial. Kubernetes provides several mechanisms to handle persistent storage, ensuring that your applications can retain data even if pods are restarted or rescheduled. This tutorial will introduce you to Kubernetes Volumes, which are essential for providing persistent storage to containers.

Concept

A Kubernetes Volume is a way to persist data generated and used by Pods. Unlike ephemeral storage, which is lost when a Pod terminates, data stored in a Volume persists across restarts. Kubernetes supports various types of Volumes, each suited for different use cases:

  • EmptyDir: A simple temporary storage that exists as long as the Pod is running.
  • HostPath: Mounts a file or directory from the host node's filesystem into your Pod.
  • PersistentVolume (PV) and PersistentVolumeClaim (PVC): Provide an abstraction layer for storage, allowing dynamic provisioning and management of storage resources.
  • ConfigMap and Secret: Special types of Volumes used to store configuration data and sensitive information.

Examples

EmptyDir Volume

The simplest type of Volume is EmptyDir. It exists as long as the Pod is running and is deleted when the Pod terminates. Here's an example of how to use an EmptyDir Volume:

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

In this example, the EmptyDir Volume is mounted at /cache inside the container. Any data written to this directory will persist as long as the Pod is running.

HostPath Volume

The HostPath Volume mounts a file or directory from the host node's filesystem into your Pod. This can be useful for accessing node-specific resources. Here's an example:

YAML
1apiVersion: v1
2kind: Pod
3metadata:
4name: test-pod
5spec:
6containers:
7- image: nginx
8 name: test-container
9 volumeMounts:
10 - mountPath: /host-data
11 name: host-volume
12volumes:
13- name: host-volume
14 hostPath:
15 path: /data

In this example, the /data directory on the host node is mounted at /host-data inside the container.

PersistentVolume and PersistentVolumeClaim

For more complex storage needs, Kubernetes provides PersistentVolume (PV) and PersistentVolumeClaim (PVC). A PV represents a piece of storage in the cluster, while a PVC is a request for that storage. Here's an example:

PersistentVolume

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

PersistentVolumeClaim

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

Pod Using PVC

YAML
1apiVersion: v1
2kind: Pod
3metadata:
4name: test-pod
5spec:
6containers:
7- image: nginx
8 name: test-container
9 volumeMounts:
10 - mountPath: "/usr/share/nginx/html"
11 name: website-content
12volumes:
13- name: website-content
14 persistentVolumeClaim:
15 claimName: pvc0001

In this example, a PersistentVolume is created with 5Gi of storage and mounted at /mnt/data. A PersistentVolumeClaim requests 2Gi of storage, and a Pod uses the PVC to mount the storage at /usr/share/nginx/html.

What's Next?

Now that you have a good understanding of Kubernetes Volumes for persistent storage, the next step is to explore Implementing Network Policies. Network policies allow you to control traffic flow between Pods in your cluster, ensuring security and isolation.

By mastering these concepts, you'll be well-equipped to manage data persistence and network access in your Kubernetes clusters.


PreviousHandling Secrets in KubernetesNext Implementing Network Policies

Recommended Gear

Handling Secrets in KubernetesImplementing Network Policies