Working with Persistent Volumes and PVCs

Tutorial 1 of 5

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.