Kubernetes / Kubernetes Storage and Persistent Volumes

Working with Persistent Volumes and PVCs

This tutorial is about understanding and working with Kubernetes' Persistent Volumes and PVCs. We will go through creating, using, and deleting PVs and PVCs.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers persistent storage options and volume management in Kubernetes.

Working with Persistent Volumes and PVCs

1. Introduction

This tutorial will guide you through the process of understanding, creating, using, and deleting Kubernetes' Persistent Volumes (PVs) and Persistent Volume Claims (PVCs).

By the end of this tutorial, you will learn:
- What PVs and PVCs are and how they work in Kubernetes
- How to create, use, and delete PVs and PVCs

Prerequisites:
- Basic understanding of Docker and Kubernetes
- A Kubernetes cluster up and running
- kubectl installed and configured

2. Step-by-Step Guide

PVs and PVCs are Kubernetes' way of managing storage resources. A PV is a piece of storage in the cluster, it is a resource in the cluster just like a node is a cluster resource. A PVC is a request for storage by a user.

When a PVC requests storage, it will be matched with a PV that fits its requirements. If a fitting PV is found, the PVC will bind to the PV.

Creating a Persistent Volume

Here is an example of how to create a PV:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: my-storage-class
  hostPath:
    path: "/mnt/data"

This example creates a PV named my-pv, with a storage capacity of 1Gi, a ReadWriteOnce access mode, a reclaim policy of Retain, a storage class named my-storage-class, and a host path of /mnt/data.

Creating a Persistent Volume Claim

And here is an example of how to create a PVC:

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

This example creates a PVC named my-pvc, which requests storage of 1Gi, has a ReadWriteOnce access mode, and a storage class of my-storage-class.

3. Code Examples

Example 1: Creating a PV and PVC

Here is a detailed example of creating a PV and PVC:

# pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: my-storage-class
  hostPath:
    path: "/mnt/data"
# pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: my-storage-class

To create the PV and PVC, you would use kubectl apply:

kubectl apply -f pv.yaml
kubectl apply -f pvc.yaml

You should see output indicating that the PV and PVC were created:

persistentvolume/my-pv created
persistentvolumeclaim/my-pvc created

Example 2: Deleting a PV and PVC

To delete a PV and PVC, you would use kubectl delete:

kubectl delete -f pv.yaml
kubectl delete -f pvc.yaml

You should see output indicating that the PV and PVC were deleted:

persistentvolume "my-pv" deleted
persistentvolumeclaim "my-pvc" deleted

4. Summary

In this tutorial, you learned about Kubernetes' Persistent Volumes (PVs) and Persistent Volume Claims (PVCs), and how to create, use, and delete them. PVs and PVCs are Kubernetes' way of managing storage resources in a cluster.

The next step is to learn how to use PVs and PVCs with Pods and Deployments.

5. Practice Exercises

  1. Exercise 1: Create a PV and PVC with different storage classes. What happens?
  2. Exercise 2: Create a PV with a storage capacity of 1Gi, then create a PVC requesting 2Gi. What happens?
  3. Exercise 3: Create a PVC first, then create a matching PV. What happens?

Solutions:

  1. Solution 1: The PVC will not be able to bind to the PV, as they have different storage classes.
  2. Solution 2: The PVC will remain in a Pending state, as it cannot find a PV that satisfies its storage request.
  3. Solution 3: Once the PV is created, it will bind to the PVC, as long as they have matching specifications.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Backlink Checker

Analyze and validate backlinks.

Use tool

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help