Sling Academy
Home/DevOps/Using PVCs to Request Storage in Kubernetes: Tutorial & Examples

Using PVCs to Request Storage in Kubernetes: Tutorial & Examples

Last updated: January 30, 2024

Introduction

When working with Kubernetes, one element you will inevitably come into contact with is storage management. A crucial component of this is the Persistent Volume Claim (PVC), which allows pods to mount persistent storage volumes without worrying about the underlying storage infrastructure details. In this tutorial, we will explore how to use PVCs to request storage in Kubernetes, including the necessary theory and hands-on examples that will take you from a basic to an advanced understanding.

Understanding Persistent Volumes (PVs) and PVCs

In Kubernetes, a PV is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. PVCs are requests for storage by a user. They can request specific size and access modes (e.g., read/write once or read-only-many).

Prerequisites

  • Kubernetes cluster
  • kubectl tool installed
  • Basic understanding of Kubernetes concepts

Creating a Persistent Volume Claim

To begin, create a PVC by defining it in a YAML file.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Apply this file to your cluster:

$ kubectl apply -f my-pvc.yaml

You should get a confirmation that the PVC has been created.

Inspecting Your PVC

After creation, you can inspect your PVC to check its status.

$ kubectl get pvc my-pvc

This command should show you the status of your PVC, indicating whether it is bound to a PV or is still pending.

Using PVC in a Pod

Once your PVC is bound, you can use it in a pod. Here’s how:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  volumes:
    - name: my-storage
      persistentVolumeClaim:
        claimName: my-pvc
  containers:
    - name: my-container
      image: nginx
      ports:
        - containerPort: 80
      volumeMounts:
        - mountPath: "/var/www/html"
          name: my-storage

Apply this configuration to your cluster:

$ kubectl apply -f my-pod.yaml

Advanced PVC Configurations

As you become more familiar with PVCs, you might want to take advantage of more advanced features such as setting storage class, using selectors, or requesting specific access modes.

Specifying a Storage Class

spec:
  storageClassName: fast-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

A Storage Class defines a class of storage. When you specify a Storage Class in your PVC, the cluster only binds the PVC to a PV that is provisioned using that class.

Using Selectors to Bind to Specific PVs

spec:
  selector:
    matchLabels:
      release: stable
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

Using selectors, you can control which PVs your PVC can bind to by defining labels on your PVs and then using selectors in your PVC to match those labels.

Requesting Specific Access Modes

spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

You may have a situation where your application requires shared access to a volume. In this case, you may request an Access Mode of ReadWriteMany (RWX) permitting multiple nodes to read/write on a PV simultaneously.

Conclusion

In this tutorial, we looked through the process of requesting storage in Kubernetes using PVCs. Through the practice of utilizing PVs and PVCs, you can manage persistent storage for your applications seamlessly. The examples walked you through from basics to more advanced uses, making PVCs an invaluable tool in your Kubernetes arsenal.

Next Article: Understanding Storage Classes and Dynamic Provisioning in Kubernetes (4 Examples)

Previous Article: Provision Persistent Volumes in Kubernetes: A Developer’s Guide

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide