Docker Certified Associate Exam Course

Kubernetes

Persistent Volumes

Persistent Volumes (PVs) decouple storage management from Pod lifecycle, providing a cluster-wide pool of storage resources that administrators provision and developers consume via Persistent Volume Claims (PVCs). This approach centralizes configuration, improves security boundaries, and simplifies updates.

Why Use Persistent Volumes?

When you embed volume definitions in every Pod spec, any change to storage (capacity, filesystem, reclaim policy) requires updating all manifests. PVs solve this by:

  • Centralizing storage configuration in a single object
  • Allowing administrators to manage capacity, access modes, and reclaim policy
  • Letting developers request storage without knowing backend details

The image illustrates the concept of Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) in a Kubernetes environment, showing the relationship between data volumes and claims.

PersistentVolume Object Overview

FieldDescription
spec.capacity.storageTotal volume size (e.g., 1Gi, 10Gi)
spec.accessModesHow Pods can mount the volume:<br/>- ReadWriteOnce (RWO)<br/>- ReadOnlyMany (ROX)<br/>- ReadWriteMany (RWX)
spec.persistentVolumeReclaimPolicyAction when a PVC is deleted:<br/>- Retain<br/>- Delete<br/>- Recycle
spec.<storageBackend>Backend-specific settings (e.g., hostPath, awsElasticBlockStore, nfs)

1. Creating a HostPath PersistentVolume

Below is a minimal PV definition that uses a node’s local filesystem (hostPath). This is helpful for testing but not recommended for production.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-hostpath-1
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp/data
  persistentVolumeReclaimPolicy: Retain

Warning

The hostPath backend binds storage to a specific node’s filesystem. For highly available or multi-node clusters, use cloud volumes or networked storage solutions.

Save this manifest as pv-hostpath.yaml and apply:

kubectl apply -f pv-hostpath.yaml

Verify creation:

kubectl get pv

Expected output:

NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   AGE
pv-hostpath-1    1Gi        RWO            Retain           Available           10s

2. Creating a Cloud-Backed PersistentVolume

Replace the hostPath section with cloud provider settings. Example: AWS EBS

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-ebs-1
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  awsElasticBlockStore:
    volumeID: <aws-volume-id>
    fsType: ext4
  persistentVolumeReclaimPolicy: Delete

Apply and verify as before:

kubectl apply -f pv-ebs.yaml
kubectl get pv

Note

Adjust the backend section for other cloud providers (GCE, Azure) or network filesystems (NFS, CSI drivers) by consulting the Kubernetes Storage Concepts.

Next Steps

Once PVs are available, developers create Persistent Volume Claims (PVCs) to request specific capacity and access modes. Kubernetes binds PVCs to matching PVs, making storage consumption seamless within Pod specs.


Watch Video

Watch video content

Previous
Volumes in Kubernetes