Kubernetes provides a flexible and extensible architecture for handling persistent storage. Persistent Volumes (PVs) represent a physical storage resource in a cluster, while Persistent Volume Claims (PVCs) are used by applications to request a specific amount of storage from the cluster. Storage Classes provide a way to dynamically provision PVs based on different parameters, such as performance, availability, or cost.
Here is a step-by-step explanation of how persistent storage is handled in Kubernetes:
Create a Storage Class: A Storage Class defines the underlying storage provider and the parameters for dynamically provisioning Persistent Volumes. For example, a Storage Class can specify a specific storage provider like AWS EBS or Google Cloud Storage, as well as the performance and redundancy characteristics of the storage.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: fast
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
Create a Persistent Volume Claim: A Persistent Volume Claim is a request for storage from a Kubernetes cluster. A PVC can specify the amount of storage needed, as well as the Storage Class to use for provisioning the Persistent Volume.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: my-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: fast
Bind a Persistent Volume to a Persistent Volume Claim: Once a PVC is created, Kubernetes will automatically provision a new PV that satisfies the request. The PV is then bound to the PVC, allowing the application to use the requested storage.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: my-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: fast
volumeName: my-pv
Mount the Persistent Volume in a Pod: The final step is to mount the PV in a Pod. The Pod can use the Persistent Volume as a regular file system, and any data written to the mount point will be stored in the underlying storage provider.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
volumeMounts:
- name: my-volume
mountPath: /var/www/html
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-claim
Persistent storage in Kubernetes is a powerful feature that allows applications to store and access data in a scalable and reliable way. By using PVs, PVCs, and Storage Classes, Kubernetes provides a unified interface for managing storage across different cloud providers and storage systems.