In Kubernetes, a Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. A Persistent Volume Claim (PVC) is a request for storage by a user. The PVC references the PV, and the PV is bound to a specific node.
Understanding Persistent Volumes (PV)
Persistent Volumes are resources in Kubernetes that provide storage implementations, such as NFS, iSCSI, or cloud-specific storage systems like AWS EBS, GCE Persistent Disk, and Azure Disk. A PV has a lifecycle independent of any individual Pod that uses the PV. This means the data remains intact, even when no Pod is using it.
What is a Persistent Volume Claim (PVC)?
A PVC is a request for storage by a user. It's similar to a Pod: while Pods consume node resources, PVCs consume PV resources. Once a PVC is bound to a PV, it can be mounted to a Pod and utilized as a regular disk.
Managing Persistent Storage: A Step-by-Step Guide
Creating a Persistent Volume:
Here's a basic example of a PV that provides 10Gi of storage on a local node:
apiVersion: v1 kind: PersistentVolume metadata: name: my-pv spec: capacity: storage: 1Gi volumeMode: Filesystem accessModes: - ReadWriteOnce hostPath: path: "/mnt/data"
kubectl apply -f pv.yaml
Creating a Persistent Volume Claim:
A PVC will claim storage from the available PVs. Here's an example:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 500Mi
Once the above PVC is created, Kubernetes will bind it to a suitable PV.
Using PVC in a Pod:
Once your PVC is bound, you can use it in a Pod:
apiVersion: v1 kind: Pod apiVersion: apps/v1 kind: Deployment metadata: name: mysql namespace: mysql labels: app: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:8 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secret key: password - name: MYSQL_DATABASE valueFrom: configMapKeyRef: name: mysql-config key: MYSQL_DB volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: my-pvc
Deleting PVCs and PVs:
When you delete a PVC, the bound PV will become available again, but it won't be automatically deleted, ensuring data safety. If you need to delete a PV, make sure you back up the data if required and then use:
kubectl delete pvc my-pvc kubectl delete pv my-pv
Best Practices:
Storage Classes: For dynamic provisioning of storage, consider using StorageClasses. It allows PVs to be dynamically created when a PVC requests it.
Backup Regularly: While PVs provide persistence, they are not immune to data loss, especially when using local storage. Ensure you have backup and recovery processes in place.
Consider Access Modes: Understand the differences between the access modes (
ReadWriteOnce
,ReadOnlyMany
,ReadWriteMany
) and choose the right one based on your application needs.Monitor and Scale: As with any resource, keep an eye on the utilization of your PVs. Scale up or out when necessary.
Conclusion:
Persistent Volumes and Persistent Volume Claims are the cornerstones of managing stateful applications in a Kubernetes environment. By understanding how to set up, utilize, and manage these resources, you can ensure that your applications remain stateful, even in the transient world of Kubernetes. With best practices in hand, you can harness the power of Kubernetes without sacrificing data persistence.