How to Create Local Persistent Volume and Bind PVC in Kubernetes
Introduction
Managing storage in Kubernetes involves creating Persistent Volumes (PV) and Persistent Volume Claims (PVC) to ensure that your applications have access to the required storage resources. In this guide, we'll explore how to create a local persistent volume and bind it to a PVC without relying on dynamic volume provisioning.
Create StorageClass
A StorageClass in Kubernetes defines the properties of the storage, including the provisioner and volume binding mode. In our case, we're using local storage with the WaitForFirstConsumer
binding mode.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
This StorageClass, named local-storage
, sets the foundation for our local persistent volume.
Create Persistent Volume
Now, let's create a Persistent Volume using the local storage defined in the StorageClass. This example creates a PV named prometheus-server-pv
with a capacity of 8Gi and specific node affinity.
apiVersion: v1
kind: PersistentVolume
metadata:
name: prometheus-server-pv
spec:
capacity:
storage: 8Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /data/prometheus/server
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- ml-server
Ensure to replace the node selector values (ml-server
) with the appropriate node information.
Pitfalls
Pending PVC due to Missing StorageClass or PersistentVolume
When a PVC is pending due to missing StorageClass or PersistentVolume, you can solve this problem by adding claimRef
in the PV's spec field. This associates the PVC with the appropriate PV.
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: prometheus-server
namespace: monitoring
Include this claimRef
in the PV definition, replacing the values with your specific PVC details.
Conclusion
Creating local persistent volumes and binding PVCs in Kubernetes requires careful configuration to ensure seamless storage management. By following the steps outlined in this guide, you can effectively provision local storage for your applications and avoid common pitfalls.
Feel free to customize these configurations based on your specific requirements and infrastructure.