Deploying Persistent Volumes in Your Cluster
Static Provisioning of Persistent Volumes
Persistent Volumes can be provisioned statically by a cluster administrator. This involves creating a Persistent Volume object in the Kubernetes API and defining its configuration, such as the storage capacity, access modes, and reclaim policy.
Here's an example of a Persistent Volume definition:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/my-pv
In this example, a Persistent Volume named my-pv
is created with a storage capacity of 5 gigabytes, the ReadWriteOnce
access mode, and a hostPath
storage type that points to a local directory on the Kubernetes node.
Dynamic Provisioning of Persistent Volumes
Persistent Volumes can also be provisioned dynamically by the Kubernetes system. This is done using a StorageClass object, which defines the parameters for dynamically provisioned Persistent Volumes.
Here's an example of a StorageClass definition:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: my-storage-class
provisioner: kubernetes.io/gce-pd
parameters:
type: pd-standard
fstype: ext4
encrypted: "true"
In this example, a StorageClass named my-storage-class
is created, which uses the kubernetes.io/gce-pd
provisioner to create Google Persistent Disk volumes. The volumes will have a pd-standard
type, an ext4
file system, and will be encrypted.
To use this StorageClass, you can create a Persistent Volume Claim (PVC) that references the StorageClass:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: my-storage-class
resources:
requests:
storage: 3Gi
When this PVC is created, Kubernetes will automatically provision a new Persistent Volume with the specified parameters.
Monitoring Persistent Volume Usage
You can monitor the usage of Persistent Volumes in your cluster using Kubernetes commands or tools like Prometheus. For example, you can use the kubectl get pv
command to list all the Persistent Volumes in your cluster and their current status.
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
my-pv 5Gi RWO Retain Available my-storage-class 1m
This output shows that the Persistent Volume my-pv
has a capacity of 5 gigabytes and is currently available for use.