Kubernetes PVC: What is it, and How Does It Work?

kubernetes

Kubernetes has separate mechanisms to manage storage and compute resources. 

A Kubernetes pod must have a PersistentVolumeClaim (PVC) mounted as a volume to access persistent storage.  

The PVC allows your deployment application to store its data within an external location, so when a pod fails, it can get replaced with a new pod and keep accessing the externally stored data like an outage never happened. 

Continue reading to learn more about Kubernetes PVC and how it works. 

Persistent Volume Claim: An overview

A Kubernetes (K8s) PersistentVolume (PV) is an object that lets pods access persistent storage on storage devices. It is defined through a K8s StorageClass. 

However, unlike regular volumes that are transient in nature, PVs support stateful app use cases since they are persistent.

PVs are resource objects within a Kubernetes cluster that continue to exist after the Pods using it are destroyed. 

In simple terms, a Kubernetes PVC is a request for storage by users and works like a Pod. 

While Pods consume node resources, PVCs consume PV resources. Pods can request certain levels of resources, such as Memory and CPU, but PVCs request particular size and access models. 

For instance, PVCs can be mounted ReadOnlyMany, ReadWriteOnce, or ReadWriteMany. 

PVCs don’t specify a certain PV but the StorageClass the pod needs. 

As an admin, you can define StorageClasses indicating storage device properties, such as service levels, back-end policies, and performance. 

One of the main advantages of the PVC pattern in K8s is that it lets you request storage resources dynamically without learning the underlying storage devices’ implementation. 

PVC is a complex mechanism often the cause of several Kubernetes issues that can be challenging to diagnose and address. 

Not understanding how Kubernetes PVC works is like having that one tool missing from your DevOps stack. You’ll be hard-pressed to use PVCs properly and troubleshoot issues effectively.

Static vs. dynamic provisioning in PVCs

Binding a pod to a PV requires pods containing a volume mount and a PVC. The declarations allow you to mount PVs in pods without learning the underlying storage equipment’s details. 

The two options to mount PVs to a pod are:

  • Static configuration. Static provisioning involves (administrators) manually creating PVs and defining a StorageClass matching the PVs criteria. A Pod gains access to one of the static PVs when it uses a PVC that specifies a StorageClass. 
  • Dynamic configuration. Dynamic provisioning takes place in the absence of static PV that matches the PVC. In cases like this, the K8s cluster provisions a new PV according to the StorageClass definitions

Configure a Pod to Use PVC for storage

To set up a Pod to use PVC for storage, you’ll need to:

  • Create a PV backed by physical storage (as a cluster administrator) without associating the volume with any Pod
  • Take the role of a cluster user or developer and create a PVC that is automatically bound to a suitable PV
  • Make a Pod that uses the mentioned PVC for storage 

However, you’ll need to do the following before you start:

  • Have a K8s cluster with only one node. The kubectl command-line tool must also be set up to communicate with your cluster. 
  • Be familiar with the terms and components associated with Persistent Volumes in Kubernetes. 

Below is a quick rundown of the basic steps for creating and binding a PVC to a PV. 

Step 1: Set up a node.

Configure a K8s cluster with a single node. Ensure the kubectl common line connects to the control plane.

Then, create a directory on the node and an index.html file within the directory. 

Step 2: Create a PV.

Make a YAML file to define a PersistentVolume and run the command below to create the PV on the node. 

kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml

Step 3: Create a PVC and bind it to a PV.

Make a PVC requiring a PV—which is subject to 3GB (or more) storage capacity and enable read/write access to match the PersistentVolume you created.

Then, create a YAML file for the PersistentVolumeClaim.

When you’re done, run the command below to apply the PVC.

kubectl apply -f https://k8s.io/examples/pods/storage/pv-claim.yaml

The K8s control plane finds the right PV when you create a PVC. If found, the control plane binds the PVC to the PV. 

Next, check the previously created PV’s status by running this command:

kubectl get pv task-pv-volume

Step 4: Create a Pod and mount the PVC.

The last step is to make a Pod that uses the PVC. 

Run the Pod using the NGINX image and specify the PersistentVolumeClaim you created within the Pod specification’s relevant section. 

Then, install curl in your Pod using a bash command. 

Run the command below: 

curl http://localhost/

The output should show the index.html file’s contents (see Step 1). It should display that the new Pod can assess data within the PV through the PVC.  

PVC errors: Common causes and troubleshooting tips

The usual errors associated with PVCs include FailedAttachVolume and FailedMount. 

The two errors can indicate that a Pod failed to mount a PV. 

FailedAttachVolume happens when a volume can’t detach from a previous node, and FaileMount occurs when a volume doesn’t mount on the required path. 

The possible causes of these two errors include too many disks attached to new nodes, new node failure, storage device failures on the previous node, and network positioning errors.

Run the command describe pod <name> and look in the Events section to find the message that indicates the error. Doing so allows you to diagnose the potential causes of FailedAttachVolume and FailedMount issues. 

The message should also include details about the error’s cause. 

You’ll need to deal with the errors manually since Kubernetes can’t resolve the FailedAttachVolume and FailedMount issues automatically. 

If the error’s cause is Failure to Detach, use the storage device’s interface to detach the volume manually. 

If the issue is Failure to Attach or Mount, check for an incorrect network path or partitioning issue. 

If the issue isn’t any of these, try to get K8s to schedule the Pod on a different node. You can also investigate and address the problem on the new node. 

Learn the nuts and bolts of K8s PVC

Understanding Kubernetes PVC and how it works allow you to implement it correctly and address potential issues appropriately. 

Cover Image Source: Pexels

Leave a Reply