# 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:

1. **Persistent Volumes (PV)**:

- A [PV](/content/finops-glossary/kubernetes-persistent-volumes/index.html) 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](/content/finops-glossary/kubernetes-pods/index.html) 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](https://www.mysql.com/)** in your [Kubernetes cluster](/content/finops-glossary/kubernetes-cluster/index.html). 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)**:

```yaml
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)**:

```yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
```

- **Configure a Pod to Use the PVC**:

```yaml
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](https://kubernetes.io/docs/concepts/storage/storage-classes/)
2. [Red Hat: Persistent Storage in Kubernetes](https://docs.redhat.com/en/documentation/openshift_container_platform/4.1/html/storage/understanding-persistent-storage)
3. AWS Documentation: [Amazon EBS – Persistent Block Storage](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html)
