Bird
Raised Fist0
Kubernetesdevops~5 mins

PersistentVolume (PV) definition in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes applications need storage that stays even if the app restarts or moves. PersistentVolume (PV) in Kubernetes provides a way to keep data safe and available independently of the app's life cycle.
When you want to store database files that must not be lost if the database pod restarts.
When you need to share files between multiple pods without losing data.
When you want to keep logs or backups safe even if the pod is deleted.
When you want to separate storage management from pod management for better control.
When you want to use cloud storage or network storage with your Kubernetes apps.
Config File - persistentvolume.yaml
persistentvolume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  hostPath:
    path: /mnt/data

This file defines a PersistentVolume named example-pv.

capacity sets the storage size to 5 gigabytes.

accessModes means the volume can be mounted as read-write by a single node.

persistentVolumeReclaimPolicy set to Retain means the data stays after the volume is released.

storageClassName groups this volume for matching with PersistentVolumeClaims.

hostPath uses a directory on the node's filesystem for storage (good for learning, not production).

Commands
This command creates the PersistentVolume in the Kubernetes cluster using the definition file.
Terminal
kubectl apply -f persistentvolume.yaml
Expected OutputExpected
persistentvolume/example-pv created
This command checks the status and details of the PersistentVolume named example-pv to confirm it was created.
Terminal
kubectl get pv example-pv
Expected OutputExpected
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE example-pv 5Gi RWO Retain Available manual 10s
This command shows detailed information about the PersistentVolume, including capacity, access modes, and reclaim policy.
Terminal
kubectl describe pv example-pv
Expected OutputExpected
Name: example-pv Labels: <none> Annotations: <none> Finalizers: [kubernetes.io/pv-protection] StorageClass: manual Status: Available Claim: Reclaim Policy: Retain Access Modes: RWO Capacity: 5Gi Node Affinity: <none> Message: Source: Type: HostPath (bare host directory volume) Path: /mnt/data Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: PersistentVolumes provide stable storage that lives beyond pod lifetimes and must be defined before use.

Common Mistakes
Not setting the correct accessModes for the workload
The pod may fail to mount the volume if access modes do not match its requirements.
Choose accessModes like ReadWriteOnce, ReadOnlyMany, or ReadWriteMany based on how your app uses the storage.
Using hostPath volumes in production
hostPath ties storage to a single node and can cause data loss if pods move to other nodes.
Use networked storage solutions like NFS, cloud volumes, or storage classes designed for production.
Forgetting to match storageClassName with PersistentVolumeClaims
Claims may not bind to the PV if storage classes do not match, causing pod startup failures.
Ensure storageClassName in PV and PVC are the same or use default storage class.
Summary
Create a PersistentVolume YAML file defining storage size, access modes, and storage location.
Apply the PersistentVolume to the cluster with kubectl apply.
Verify the PersistentVolume status with kubectl get pv and kubectl describe pv.

Practice

(1/5)
1. What is the main purpose of a PersistentVolume (PV) in Kubernetes?
easy
A. To manage container images
B. To define network policies for pods
C. To schedule pods on specific nodes
D. To provide storage that can persist beyond the lifecycle of a pod

Solution

  1. Step 1: Understand the role of PersistentVolume

    A PersistentVolume provides storage resources independent of pod lifecycle.
  2. Step 2: Compare with other options

    Network policies, pod scheduling, and image management are unrelated to PV.
  3. Final Answer:

    To provide storage that can persist beyond the lifecycle of a pod -> Option D
  4. Quick Check:

    PV = persistent storage [OK]
Hint: PV stores data beyond pod life, not network or scheduling [OK]
Common Mistakes:
  • Confusing PV with pod scheduling
  • Thinking PV manages network policies
  • Assuming PV handles container images
2. Which of the following is the correct minimal YAML snippet to define a PersistentVolume with 5Gi storage and ReadWriteOnce access mode?
easy
A. apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-example\nspec:\n capacity:\n storage: 5GB\n accessModes:\n - ReadOnlyMany\n hostPath:\n path: /mnt/data
B. apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-example\nspec:\n capacity:\n storage: 5Gi\n accessModes:\n - ReadWriteOnce\n hostPath:\n path: /mnt/data
C. apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-example\nspec:\n size: 5Gi\n accessModes:\n - ReadWriteMany\n hostPath:\n path: /mnt/data
D. apiVersion: v1\nkind: PersistentVolumeClaim\nmetadata:\n name: pvc-example\nspec:\n storage: 5Gi\n accessModes:\n - ReadWriteOnce

Solution

  1. Step 1: Check correct kind and apiVersion

    PersistentVolume uses kind: PersistentVolume and apiVersion: v1.
  2. Step 2: Validate capacity and accessModes syntax

    Capacity must be under spec.capacity.storage with unit Gi, and accessModes is a list with ReadWriteOnce.
  3. Final Answer:

    YAML with capacity 5Gi, accessModes ReadWriteOnce, kind PersistentVolume -> Option B
  4. Quick Check:

    Correct YAML keys and values = apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-example\nspec:\n capacity:\n storage: 5Gi\n accessModes:\n - ReadWriteOnce\n hostPath:\n path: /mnt/data [OK]
Hint: PV uses spec.capacity.storage with units like Gi, not size or GB [OK]
Common Mistakes:
  • Using PersistentVolumeClaim instead of PersistentVolume
  • Wrong key 'size' instead of 'capacity.storage'
  • Incorrect storage unit like GB instead of Gi
  • Wrong access mode or misspelled keys
3. Given this PersistentVolume YAML snippet, what is the reclaim policy of the PV?
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-sample
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /mnt/data
medium
A. Delete the volume after release
B. Recycle the volume automatically
C. Retain the volume after release
D. No reclaim policy set

Solution

  1. Step 1: Locate the reclaim policy field

    The field persistentVolumeReclaimPolicy is set to Retain in the spec.
  2. Step 2: Understand reclaim policy meaning

    Retain means the volume is kept after the claim is released, not deleted or recycled.
  3. Final Answer:

    Retain the volume after release -> Option C
  4. Quick Check:

    persistentVolumeReclaimPolicy = Retain [OK]
Hint: Look for persistentVolumeReclaimPolicy field value [OK]
Common Mistakes:
  • Confusing Retain with Delete or Recycle
  • Ignoring the reclaim policy field
  • Assuming default policy without checking
4. You defined this PersistentVolume YAML but get an error when applying it:
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-error
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: /mnt/data
  storageClassName: fast
What is the likely cause of the error?
medium
A. hostPath volumes cannot have storageClassName set
B. accessModes value ReadWriteOnce is invalid
C. Missing volumeMode field
D. persistentVolumeReclaimPolicy Delete is not allowed

Solution

  1. Step 1: Check hostPath volume restrictions

    hostPath volumes do not support storageClassName because they are local paths.
  2. Step 2: Validate other fields

    accessModes and reclaim policy values are valid; volumeMode is optional.
  3. Final Answer:

    hostPath volumes cannot have storageClassName set -> Option A
  4. Quick Check:

    hostPath + storageClassName = error [OK]
Hint: hostPath volumes must not set storageClassName [OK]
Common Mistakes:
  • Assuming volumeMode is mandatory
  • Thinking accessModes ReadWriteOnce is invalid
  • Believing Delete reclaim policy is disallowed
5. You want to create a PersistentVolume that allows multiple pods to read and write simultaneously and automatically deletes the volume when released. Which YAML snippet correctly defines these requirements?
hard
A. apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-multi\nspec:\n capacity:\n storage: 15Gi\n accessModes:\n - ReadWriteMany\n persistentVolumeReclaimPolicy: Delete\n nfs:\n path: /exports/data\n server: 10.0.0.1
B. apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-single\nspec:\n capacity:\n storage: 15Gi\n accessModes:\n - ReadWriteOnce\n persistentVolumeReclaimPolicy: Retain\n hostPath:\n path: /mnt/data
C. apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-multi\nspec:\n capacity:\n storage: 15Gi\n accessModes:\n - ReadOnlyMany\n persistentVolumeReclaimPolicy: Delete\n nfs:\n path: /exports/data\n server: 10.0.0.1
D. apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-multi\nspec:\n capacity:\n storage: 15Gi\n accessModes:\n - ReadWriteMany\n persistentVolumeReclaimPolicy: Retain\n nfs:\n path: /exports/data\n server: 10.0.0.1

Solution

  1. Step 1: Identify access mode for multiple pods read/write

    ReadWriteMany allows multiple pods to read and write simultaneously.
  2. Step 2: Check reclaim policy for automatic deletion

    persistentVolumeReclaimPolicy: Delete deletes the volume after release.
  3. Step 3: Confirm storage type supports ReadWriteMany

    NFS supports ReadWriteMany; hostPath does not.
  4. Final Answer:

    YAML with ReadWriteMany, Delete reclaim policy, and NFS volume -> Option A
  5. Quick Check:

    ReadWriteMany + Delete + NFS = apiVersion: v1\nkind: PersistentVolume\nmetadata:\n name: pv-multi\nspec:\n capacity:\n storage: 15Gi\n accessModes:\n - ReadWriteMany\n persistentVolumeReclaimPolicy: Delete\n nfs:\n path: /exports/data\n server: 10.0.0.1 [OK]
Hint: Use ReadWriteMany + Delete + NFS for multi-pod writable PV [OK]
Common Mistakes:
  • Using hostPath for multi-pod access
  • Choosing ReadWriteOnce instead of ReadWriteMany
  • Setting reclaim policy to Retain instead of Delete
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard