CodeBlanks Logo Icon CodeBlanks

What is a PersistentVolume?

Learn that a PV is cluster-level storage.

What is a PersistentVolume?

A PersistentVolume (PV) is a piece of storage in the cluster: a cloud disk (EBS, Persistent Disk), an NFS share, a local disk. It lives independently of pods; even if the pod is deleted, the data remains. PVs are cluster-scoped and are created either by hand by an administrator or automatically with a StorageClass.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 50Gi
  accessModes: [ReadWriteMany]
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 10.0.0.20
    path: /disa/paylasim

Reclaim policy

persistentVolumeReclaimPolicy determines what happens when the claim using the PV is deleted:

  • Retain: the PV and data stay; an administrator cleans up by hand. The safe choice for important data.
  • Delete: the underlying disk is deleted too. Often the default for dynamically created PVs.
$ kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM
nfs-pv     50Gi       RWX            Retain           Available
pvc-3f2a…  10Gi       RWO            Delete           Bound       default/data-db-0

Practice An NFS PV

Write a PersistentVolume YAML: name nfs-pv; capacity.storage: 50Gi; accessModes: [ReadWriteMany]; persistentVolumeReclaimPolicy: Retain; under nfs, server: 10.0.0.20 and path: /disa/paylasim.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 50Gi
  accessModes: [ReadWriteMany]
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 10.0.0.20
    path: /disa/paylasim

Expected output

persistentvolume/nfs-pv created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main difference between emptyDir and a PersistentVolume?

  • A PV is read-only
  • emptyDir is lost when the pod is deleted; a PV lives independently of the pod Correct answer
  • They are the same
  • emptyDir is bigger

Explanation: emptyDir is for temporary sharing, a PV for persistent data.

2 Keep the data when the claim is deleted.

Question
spec:
  persistentVolumeReclaimPolicy: ____
Answer
spec:
  persistentVolumeReclaimPolicy: Retain

Explanation: Retain keeps the data and the PV.

3 Match.

Term Meaning
PersistentVolume Piece of storage
Retain Data stays
Delete Disk is deleted
Available Not yet bound

All lessons in Advanced