1. Persistent Volumes (PVs)
A Persistent Volume (PV) is a cluster-level resource in Kubernetes representing a piece of storage that has been provisioned for use by one or more Pods. PVs are not tied to the lifecycle of individual Pods, ensuring data persistence even if Pods are rescheduled or deleted.
Key characteristics of Persistent Volumes:
- Independent of Pods: This means that even if you delete a Pod that was using a PV, the data in the PV will remain intact. This is crucial for applications that require long-term data storage, such as databases or data analysis tools.
- Persistent Storage: PVs can be backed by different storage systems, giving you the flexibility to choose the best storage solution for your specific needs. For example, you might use SSDs for high-performance applications and HDDs for large-scale data storage.
- Lifecycle Management: These policies allow you to control how PVs are managed over time. For example, you can set a retention policy to automatically delete data after a certain period, or you can set a reclaim policy to automatically release the PV when it is no longer in use.
Create a Persistent Volume
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
nfs:
path: /path/to/nfs
server: nfs-server.example.com
storageClassName: manual
accessModes:
- ReadWriteMany
2. Persistent Volume Claims (PVCs)
A Persistent Volume Claim (PVC) is a Kubernetes object that represents a request for storage. It's like a user or application submitting a ticket to a storage administrator, specifying the amount of storage needed, how it should be accessed (read-only, read-write, etc.), and what type of storage is preferred (e.g., SSD, HDD).
Think of it as a contract between the application and the underlying storage system. The PVC defines the application's storage needs, and Kubernetes is responsible for matching the PVC to an available Persistent Volume (PV) that meets the specified requirements.
Key Characteristics of Persistent Volume Claims:
- Size and Access Modes: PVCs can specify a certain amount of storage and access modes (ReadWriteOnce, ReadOnlyMany, ReadWriteMany).
- Automatic Provisioning: Kubernetes will automatically provision PVCs with suitable PVs based on the PVC's requests and the PV's attributes.
- Abstraction: PVCs allow applications to request storage without knowing the details of how the storage is provided.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: manual
3. Using Persistent Volumes and Persistent Volume Claims
Step 1: Create a Persistent Volume
First, you need to create a Persistent Volume (PV) to provide storage for PVCs. You can configure the PV to use various storage types such as NFS, AWS EBS, GCE PD, etc.
Step 2: Create a Persistent Volume Claim
After creating the PV, you create a Persistent Volume Claim (PVC) to request a portion of the storage from the PV. The PVC will request a specific amount of storage and will automatically be bound to a PV if the requests match the PV's characteristics.
Step 3: Use the PVC in a Pod
Once the PVC is created and bound to a PV, you can use the PVC in Pods to store data. The PVC will be mounted into the Pod as a volume.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
volumeMounts:
- mountPath: /data
name: my-volume
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-pvc
Read more at: Persistent Volumes and Persistent Volume Claims