How Persistent Volumes Work

Persistent Volumes are part of the Kubernetes storage infrastructure, designed to decouple storage management from the application lifecycle. They are defined by the cluster administrator and provide a way to abstract underlying storage resources, such as:

Persistent Volumes are created and managed by Kubernetes and can be used by pods through a feature called Persistent Volume Claims (PVCs).

Key Concepts

  1. Persistent Volume (PV):
    • A PV is a resource in the cluster that represents a piece of storage. It can be manually created by an administrator or dynamically provisioned when a pod requests it.
    • The PV encapsulates storage attributes, such as size, access mode (read-only or read-write), and storage class, specifying the type of storage backend.
  2. Persistent Volume Claim (PVC):
    • A PVC is a request made by a pod for a specific type of storage. Pods use PVCs to access the Persistent Volumes without needing to know the details of the storage infrastructure.
    • The PVC specifies the amount of storage and access mode required, and Kubernetes matches the claim to an available PV that meets the criteria.
  3. Storage Classes:
    • A StorageClass defines the types of storage available in a Kubernetes cluster, such as SSDs or HDDs, and the parameters needed for provisioning them.
    • Storage Classes are used when dynamic provisioning is enabled, allowing Kubernetes to automatically create PVs as needed when a PVC is made.

Lifecycle of a Persistent Volume

  1. Provisioning:
    • Persistent Volumes can be provisioned statically (manually created by an administrator) or dynamically (automatically created when a PVC is made, based on a StorageClass).
  2. Binding:
    • When a pod makes a Persistent Volume Claim, Kubernetes looks for an available PV that meets the request. If it finds one, it binds the PV to the PVC, making the storage available to the pod.
  3. Using the Volume:
    • Once bound, the PVC is linked to the pod, and the pod can read and write data to the PV. The PV remains bound to the PVC until it is explicitly released.
  4. Reclaiming:
    • When a PVC is deleted, the PV enters a reclaim phase, where the administrator can decide whether to keep the data, delete it, or make the volume available to new claims. The reclaim policies can be set to:
      • Retain: Keeps the data, but the PV will not be available for new claims until manually released.
      • Delete: Automatically deletes the PV and all its data once the PVC is deleted.
      • Recycle: Clears the data on the volume, making it available for new claims.

Example Use Case

Imagine you are running a database inside a Kubernetes pod. Since databases need persistent storage to retain data even if the pod is restarted or replaced, you would use a Persistent Volume. You would create a PVC specifying the size and type of storage you need, and Kubernetes would either provision a new PV or match your PVC to an existing PV. The database pod can then store its data on this persistent volume, ensuring that the data is not lost if the pod stops running.

Conclusion

Persistent Volumes in Kubernetes provide a reliable way to manage storage that outlives the pods using them. By abstracting storage resources, they enable developers to focus on applications without worrying about the underlying infrastructure. Through PVCs and Storage Classes, Kubernetes ensures that storage management is flexible, scalable, and efficient, making it easier to build stateful applications within the cluster.

References

  1. Kubernetes Documentation: Persistent Volumes
  2. Kubernetes Documentation: Storage Classes
  3. Kubernetes Blog: Dynamic Provisioning in Kubernetes
  4. AWS Documentation: Persistent Storage with Amazon EBS
  5. Google Cloud Documentation: Persistent Disk for Kubernetes

FAQ: Persistent Volumes in Kubernetes

1. Should I create a PV or PVC first?

It depends on your provisioning method. If you are using static provisioning, the PV must be created first, and then a PVC can be made to claim it. With dynamic provisioning, you can create a PVC directly, and Kubernetes will automatically create a suitable PV.

2. What is the difference between PV and PVC?

3. Can one PV have multiple PVCs?

4. How is PVC linked to PV?

5. Can you create a PVC without a PV?

6. What happens to PVC when a pod is deleted?

7. Does deleting PVC delete PV?