Understanding Kubernetes Persistent Volumes
Kubernetes Persistent Volumes (PVs) are a crucial component in the Kubernetes ecosystem, providing a way to abstract storage from the underlying infrastructure and make it available to Pods. Persistent Volumes are a cluster-level resource, independent of the Pods that use them, and can be provisioned in various ways, such as through a cloud provider, a network storage system, or a local disk.
The basic concept of a Persistent Volume is that it represents a piece of storage that has been provisioned by an administrator or dynamically provisioned using a Storage Class. Pods can then request storage by creating a Persistent Volume Claim (PVC), which is a request for storage resources. The Kubernetes scheduler will then match the PVC to an available PV, and the Pod can use the storage.
graph LR
A[Persistent Volume] --> B[Persistent Volume Claim]
B --> C[Pod]
Here's an example of how to create a Persistent Volume and Persistent Volume Claim in a Kubernetes cluster:
## Persistent Volume
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/my-pv
---
## Persistent Volume Claim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
In this example, we create a Persistent Volume with a capacity of 5 GiB and an access mode of ReadWriteOnce
, which means the volume can be mounted as read-write by a single node. The Persistent Volume is backed by a host path on the node. We then create a Persistent Volume Claim that requests a 5 GiB volume with the same access mode.
When a Pod is created and references the Persistent Volume Claim, Kubernetes will automatically bind the Persistent Volume to the Persistent Volume Claim, and the Pod can then use the storage.