Kubernetes - Using Persistent Volumes
Storage in Kubernetes
Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. This guide provides an understanding of Persistent Volumes, a crucial component of Kubernetes storage for managing data persistence.
Key Points:
- Persistent Volumes (PVs) provide storage resources that exist independently of pods.
- Persistent Volume Claims (PVCs) are requests for storage by users and are used to bind to PVs.
- PVs and PVCs enable stateful applications by providing persistent storage that survives pod rescheduling.
What is a Persistent Volume?
A Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. PVs are resources in the cluster just like nodes and are independent of the lifecycle of any individual pod that uses the PV.
# Example of a PersistentVolume definition
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /mnt/data
What is a Persistent Volume Claim?
A Persistent Volume Claim (PVC) is a request for storage by a user. It is similar to a pod in that pods consume node resources and PVCs consume PV resources. PVCs can request specific size and access modes (e.g., can be mounted once read/write or many times read-only).
# Example of a PersistentVolumeClaim definition
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: example-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Binding Persistent Volumes and Claims
When a PVC is created, Kubernetes looks for a PV that satisfies the claim's request. If a suitable PV is found, it binds the PV to the PVC. Once bound, the PV is exclusively used by the PVC and cannot be bound to another PVC.
# Example of a Pod using a PersistentVolumeClaim
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: mycontainer
image: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: mypvc
volumes:
- name: mypvc
persistentVolumeClaim:
claimName: example-pvc
Persistent Volume Reclaim Policy
The persistentVolumeReclaimPolicy
field specifies what happens to the PV after the PVC is deleted. The available policies are:
- Retain: The PV is retained and must be manually reclaimed.
- Recycle: The PV's data is scrubbed and the volume is made available again. (Deprecated)
- Delete: The PV and its data are deleted.
Dynamic Provisioning
Dynamic provisioning allows storage resources to be created on-demand. This is enabled by using Storage Classes, which define the provisioner and parameters for the dynamic creation of PVs.
# Example of a StorageClass definition
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: example-storage-class
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
# Example of a PersistentVolumeClaim using a StorageClass
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: example-pvc
spec:
storageClassName: example-storage-class
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Best Practices
Follow these best practices when working with Persistent Volumes:
- Choose Appropriate Reclaim Policy: Select a reclaim policy that matches your data retention and cleanup requirements.
- Monitor Storage Usage: Regularly monitor the usage of your PVs to ensure that your applications have sufficient storage resources.
- Use Dynamic Provisioning: Use dynamic provisioning to automate the creation of PVs and simplify storage management.
- Secure Your Data: Implement access controls and encryption to protect the data stored in your PVs.
- Backup and Recovery: Implement backup and recovery strategies to protect against data loss.
Conclusion
This guide provided an overview of Persistent Volumes in Kubernetes, including their creation, management, and best practices. By understanding and using Persistent Volumes effectively, you can ensure data persistence and reliable storage for your stateful applications in Kubernetes.