Managing storage for stateful applications is a crucial aspect of container orchestration. Kubernetes Persistent Volume (PV) is a fundamental feature that addresses this need by providing a mechanism to manage storage resources independently from the lifecycle of individual pods. In this article, we’ll dive into the concept of Kubernetes Persistent Volume, its features, and best practices for using it effectively in your Kubernetes clusters.
A Kubernetes Persistent Volume (PV) is a storage resource in a Kubernetes cluster that abstracts the underlying storage technology. Unlike temporary storage, which is tied to a pod's lifecycle and disappears when the pod is terminated, PVs provide a way to manage and allocate persistent storage to applications. This ensures data remains intact even if the pods accessing it are terminated or rescheduled.
Using persistent volumes provides benefits such as:
Kubernetes Persistent Volumes offer several key features that make them powerful and adaptable for diverse storage needs:
Storage classes act like templates that define different tiers or categories of storage available in a cluster. They can specify characteristics like performance (e.g., SSD vs. HDD), capacity, durability, and cost. This allows administrators to provision storage tailored to the specific requirements of applications.
Access modes define how pods can interact with the storage provided by a PV. Here's a breakdown of the common modes:
Reclaim policies dictate how Kubernetes handles a PV after it's no longer bound to a pod. The main policies include:
Kubernetes leverages volume plugins to bridge the gap between the platform and various storage providers. These plugins enable Kubernetes to understand and interact with diverse storage backends, including local storage, network attached storage (NAS) solutions like NFS or iSCSI, and cloud provider-specific storage services like AWS EBS or GCE Persistent Disk. This plugin architecture provides significant flexibility in choosing and integrating storage solutions with your Kubernetes cluster.
Kubernetes offers two primary methods for provisioning Persistent Volumes (PVs): static provisioning and dynamic provisioning. Each approach caters to different use cases and provides distinct advantages.
In static provisioning, cluster administrators manually create and configure PVs beforehand. This approach provides fine-grained control over storage configuration and is suitable for scenarios with well-defined storage requirements that don't change frequently. To provision a PV statically:
1. Define the storage details: Determine the storage capacity, access modes (ReadWriteOnce, ReadOnlyMany, ReadWriteMany), reclaim policy (Retain, Recycle, Delete), and volume plugin specifics (e.g., server address for NFS).
2. Create the PV object using YAML: Use a YAML manifest file to define the PV configuration. Here's an example utilizing NFS storage:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv-nfs
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
nfs:
path: /path/to/nfs/share
server: nfs-server.example.com
3. Apply the YAML manifest: Use the kubectl apply -f command to create the PV object in your Kubernetes cluster.
Dynamic provisioning leverages StorageClasses to automate PV creation. Administrators define StorageClasses that specify desired storage characteristics and let Kubernetes handle PV creation on demand when a PersistentVolumeClaim (PVC) requests storage. To use dynamic provisioning:
1. Define a StorageClass: Create a StorageClass YAML manifest specifying the provisioner, storage type (e.g., SSD, HDD), and any additional parameters required by the provisioner. Here's an example for AWS EBS:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard-ebs
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
2. Apply the StorageClass manifest: Use kubectl apply -f to create the StorageClass object in your cluster.
Binding a PV to a pod enables applications to leverage persistent storage. This binding is typically achieved through a PersistentVolumeClaim (PVC).
A PVC acts as a storage request submitted by a pod. It specifies the storage requirements a pod has, including:
There are two primary methods for binding PVs to pods:
Here's an example of a pod named "pod-example" that utilizes a PVC named "pvc-example":
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: app
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: pvc-storage
volumes:
- name: pvc-storage
persistentVolumeClaim:
claimName: pvc-example
In this example, the pod mounts the PVC named "pvc-example" at the path "/usr/share/nginx/html" within the container. This allows the pod to access and manage its persistent data stored in the underlying PV.
It is imperative to manage your Persistent Volumes (PVs) effectively to maintain an efficient and scalable storage utilization in your Kubernetes cluster. Some key aspects of PV management include:
Kubernetes v1.11 introduced the ability to resize PVs. This allows you to dynamically adjust the storage capacity allocated to a PV, catering to growing storage requirements of your applications.
To resize a PV, update the PVC's storage request: Edit the PVC that references the PV and modify the storage value within the resources.requests section of the PVC's YAML specification. For instance, to increase the size of a PVC named "pvc-example" to 20Gi, update the YAML manifest:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-example
spec:
resources:
requests:
storage: 20Gi
Once you update the PVC's storage request, Kubernetes attempts to resize the underlying storage provisioned for the PV. Resizing a PV is generally a one-way operation (expanding storage). Shrinking the size of a PV is not supported due to potential data loss concerns.
To delete a PV, ensure it's not currently bound to any PVCs. You cannot delete a PV in use by a PVC to prevent data loss. Here's how to delete a PV:
kubectl delete pv pv-example
Replace pv-example with the actual name of the PV you want to delete.
While the core storage capacity cannot be directly modified within a PV object, you can update certain metadata fields of a PV, such as labels or annotations. These labels and annotations can be used to better organize, identify, and manage your PVs within the cluster.
The following are some of the best practices you should follow to get the most out of Kubernetes persistent volumes:
Kubernetes Persistent Volumes (PVs) offer a fundamental mechanism for managing storage in stateful applications. By leveraging PVs, you can ensure data persistence, high availability, and efficient storage utilization within your Kubernetes cluster. Solutions like Portworx by Everpure provide an intuitive way of harnessing the benefits of Kubernetes' persistent volumes. With features like high availability, disaster recovery, and backup that uses artificial intelligence for improved efficiency, Portworx is the ideal Kubernetes persistent storage solution for containerized applications.
Mark your calendars. Registration opens in February.
Access on-demand videos and demos to see what Everpure can do.
Charlie Giancarlo on why managing data—not storage—is the future. Discover how a unified approach transforms enterprise IT operations.
Modern workloads demand AI-ready speed, security, and scale. Is your stack ready?