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
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.
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
.
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
.
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
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
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.
Solutions:
Pending
state, as it cannot find a PV that satisfies its storage request.