What Is Persistent Storage in Kubernetes? | FinOps Glossary
Kubernetes Persistent Storage
In Kubernetes, persistent storage refers to storage that retains data even after the lifecycle of a pod ends. This is essential for stateful applications, where data must persist beyond individual pod restarts, rescheduling, or failures. Kubernetes provides a system to manage persistent storage using Persistent Volumes (PVs) and Persistent Volume Claims (PVCs), allowing users to easily attach long-term storage to their applications running within the cluster.
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:
- 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.
- 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.
- 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:
- Persistent Volume (PV): This configuration reserves 10Gi of storage on the host path
/mnt/dataand can be accessed by pods that use a matching PVC. The reclaim policy is set toRetain, meaning that the data on the PV will be preserved even after the PVC is deleted. - 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.
- Pod Configuration: The pod specifies a
volumeMountto 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.