How Persistent Storage Works in Kubernetes

Persistent storage in Kubernetes decouples the storage lifecycle from the pod lifecycle, ensuring that data persists even if the pod accessing it is terminated or rescheduled. Here’s how it works:

  1. Persistent Volumes (PV):
    • A PV is a piece of storage in the cluster that an administrator can provision manually or dynamically using a StorageClass. It represents the actual physical storage resource, such as an NFS share, cloud storage disk, or local disk.
  2. Persistent Volume Claims (PVC):
    • A PVC is a request by a pod for a specific amount and type of storage. It allows users to specify the size, access mode, and type of storage they need. Kubernetes automatically matches the PVC to an appropriate PV.
  3. Storage Classes:
    • A StorageClass defines the types of storage available in the cluster (e.g., SSD, HDD) and provides parameters for dynamic provisioning. When a PVC is created, Kubernetes can automatically create a PV based on the StorageClass defined.

Example of Persistent Storage Use Case

Imagine you are running a MySQL database in your Kubernetes cluster. Databases require persistent storage to retain data like user records, settings, and transactions even if the database container is restarted or moved to another node. By using a Persistent Volume, the MySQL data will persist, allowing the database to recover from disruptions without losing information.

How to Configure a Pod to Use Persistent Storage

  • Create a Persistent Volume (PV):

  apiVersion: v1 kind: PersistentVolume metadata: name: my-pv spec: capacity: storage: 10Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: /mnt/data
  • Create a Persistent Volume Claim (PVC):

  apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi
  • Configure a Pod to Use the PVC:

  apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: mysql:5.7 volumeMounts: - mountPath: "/var/lib/mysql" name: my-storage volumes: - name: my-storage persistentVolumeClaim: claimName: my-pvc

Explanation of Configuration:

  1. Persistent Volume (PV): This configuration reserves 10Gi of storage on the host path /mnt/data and can be accessed by pods that use a matching PVC. The reclaim policy is set to Retain, meaning that the data on the PV will be preserved even after the PVC is deleted.
  2. Persistent Volume Claim (PVC): This acts as a request for the 10Gi of storage. Kubernetes will bind this PVC to the PV if it matches the storage request and access mode.
  3. Pod Configuration: The pod specifies a volumeMount to mount the storage from the PVC to /var/lib/mysql, which is the directory MySQL uses to store its data. When the pod writes to this directory, the data will be saved on the persistent storage specified by the PV.

References

  1. Kubernetes Documentation: Storage Classes
  2. Red Hat: Persistent Storage in Kubernetes
  3. AWS Documentation: Amazon EBS – Persistent Block Storage