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.
Section overview
5 resourcesCovers 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
- Exercise 1: Create a PV and PVC with different storage classes. What happens?
- Exercise 2: Create a PV with a storage capacity of 1Gi, then create a PVC requesting 2Gi. What happens?
- Exercise 3: Create a PVC first, then create a matching PV. What happens?
Solutions:
- Solution 1: The PVC will not be able to bind to the PV, as they have different storage classes.
- Solution 2: The PVC will remain in a
Pendingstate, as it cannot find a PV that satisfies its storage request. - 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article