LibraryPersistent Volumes and Persistent Volume Claims

Persistent Volumes and Persistent Volume Claims

Learn about Persistent Volumes and Persistent Volume Claims as part of Docker and Kubernetes DevOps

Kubernetes Persistent Volumes and Persistent Volume Claims

In Kubernetes, managing data persistence for your applications is crucial. When a Pod restarts or is rescheduled, any data stored in its local filesystem is lost. Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) are Kubernetes abstractions that allow you to manage storage for your applications independently of the Pod lifecycle.

Understanding Persistent Volumes (PVs)

A Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It's a cluster resource, just like a Node is a cluster resource. PVs have a lifecycle independent of any individual Pod that uses the PV. For example, if a Pod is deleted, the PV is not deleted.

PVs decouple storage from Pods.

PVs represent physical or logical storage units, like a network-attached storage (NAS) device or a cloud provider's block storage. They are provisioned independently of the Pods that will consume them.

PVs are defined by a set of properties, such as capacity, access modes (e.g., ReadWriteOnce, ReadOnlyMany, ReadWriteMany), and reclaim policy (e.g., Retain, Recycle, Delete). These properties determine how the volume can be used and what happens to the underlying storage when the PV is released.

Understanding Persistent Volume Claims (PVCs)

A Persistent Volume Claim (PVC) is a request for storage by a user. It is similar to a Pod consuming node resources. Pods request specific amounts and types of storage (via PVCs), and Kubernetes finds a matching PV that fulfills the request. PVCs consume PV resources.

PVCs are requests for storage.

A PVC specifies the desired storage capacity and access modes. Kubernetes then binds the PVC to a suitable PV.

When you create a PVC, you define your storage requirements. Kubernetes then looks for an available PV that matches these requirements. If a suitable PV exists and is available, it is 'bound' to the PVC. If no PV is available, but dynamic provisioning is enabled (via Storage Classes), a new PV can be created automatically to satisfy the claim.

The Binding Process

The core mechanism connecting PVs and PVCs is the binding process. Kubernetes uses selectors and labels to match PVCs to PVs. A PVC can request a specific PV by name, or it can request a PV based on its properties (e.g., storage class, access modes, capacity).

What is the primary role of a Persistent Volume (PV) in Kubernetes?

A PV is a piece of storage provisioned by an administrator or dynamically, representing a cluster resource with a lifecycle independent of Pods.

What is the primary role of a Persistent Volume Claim (PVC) in Kubernetes?

A PVC is a request for storage by a user, specifying storage requirements that Kubernetes matches to a PV.

Access Modes

Access modes define how a volume can be mounted on nodes. Understanding these is key to ensuring your applications can access their data correctly. The common access modes are:

Access ModeDescriptionSupported by most storage types?
ReadWriteOnce (RWO)The volume can be mounted as read-write by a single node.Yes
ReadOnlyMany (ROX)The volume can be mounted read-only by many nodes.Limited (e.g., NFS)
ReadWriteMany (RWX)The volume can be mounted as read-write by many nodes.Limited (e.g., NFS, CephFS)

A single PV can only be bound to a single PVC. However, a single storage system can provide multiple PVs.

Reclaim Policy

The reclaim policy determines what happens to the underlying storage when a PV is released by its bound PVC. This is crucial for data management and preventing orphaned storage.

Loading diagram...

Reclaim Policies Explained

Reclaim policies manage storage after a PVC is deleted.

When a PVC is deleted, Kubernetes applies the PV's reclaim policy to the underlying storage.

The common reclaim policies are:

  • <b>Retain</b>: The administrator must manually reclaim the volume. The underlying storage is not deleted.
  • <b>Recycle</b>: The volume is scrubbed using rm -rf /the/volume. This is deprecated and not recommended.
  • <b>Delete</b>: The underlying storage for the PV is deleted. This is common for cloud provider volumes.

Storage Classes

Storage Classes provide a way for administrators to describe the 'classes' of storage they offer. Different classes might map to quality-of-service levels, backup policies, or arbitrary policies determined by the cluster administrator. PVCs can request a specific Storage Class, which then triggers dynamic provisioning of a PV.

Imagine a library. Persistent Volumes (PVs) are like the actual bookshelves, each with a certain capacity and type of storage (e.g., metal shelves for heavy books, wooden shelves for lighter ones). Persistent Volume Claims (PVCs) are like requests from patrons for a specific type of book or a certain number of books. When a patron requests a book (PVC), the librarian finds an available shelf (PV) that matches the request. If no shelf is available, and the library has a system for acquiring new shelves (dynamic provisioning via Storage Classes), a new shelf might be added. The 'reclaim policy' is like what happens to the shelf after the books are returned – does the library keep it, clean it, or discard it?

📚

Text-based content

Library pages focus on text content

Practical Application: StatefulSets

StatefulSets are Kubernetes controllers designed for stateful applications. They leverage PVs and PVCs to provide stable network identities and persistent storage for each replica of an application. Each Pod in a StatefulSet gets its own stable storage, ensuring data consistency across restarts and rescheduling.

Summary

Persistent Volumes and Persistent Volume Claims are fundamental to managing stateful applications in Kubernetes. PVs represent the storage infrastructure, while PVCs represent the user's request for that storage. Together, they enable robust data persistence, decoupling storage management from application deployment.

Learning Resources

Kubernetes Docs: Persistent Volumes(documentation)

The official Kubernetes documentation provides a comprehensive overview of Persistent Volumes, their lifecycle, and concepts.

Kubernetes Docs: Persistent Volume Claims(documentation)

Detailed explanation of Persistent Volume Claims, how they request storage, and their binding to Persistent Volumes.

Kubernetes Docs: Storage Classes(documentation)

Learn about Storage Classes, which enable dynamic provisioning of Persistent Volumes.

Kubernetes Docs: StatefulSets(documentation)

Understand how StatefulSets utilize Persistent Volumes and Claims for stateful applications.

DigitalOcean: Understanding Kubernetes Persistent Volumes(blog)

A practical guide explaining PVs, PVCs, and Storage Classes with clear examples.

KubeAcademy: Kubernetes Persistent Volumes Explained(blog)

A clear, concise explanation of the concepts behind Kubernetes storage.

Learnk8s: Kubernetes Persistent Volumes and Claims(blog)

Covers the essentials of Kubernetes volumes, including PVs and PVCs, with practical advice.

StackRox (Red Hat): Kubernetes Persistent Volumes: A Deep Dive(blog)

An in-depth look at PVs, PVCs, and their role in Kubernetes storage management.

The New Stack: Kubernetes Persistent Volumes Explained(blog)

A good introduction to PVs and PVCs, focusing on how they work in practice.

Kubernetes Official YouTube: Persistent Volumes(video)

A video explaining the core concepts of Kubernetes Persistent Volumes and their lifecycle.