# Day 23: Managing Persistent Volumes in Your Deployment

Managing storage is a distinct problem from managing compute instances. The PersistentVolume subsystem provides an API for users and administrators that abstracts details of how storage is provided from how it is consumed.

A ***PersistentVolume*** (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using [Storage Classes](https://kubernetes.io/docs/concepts/storage/storage-classes/). It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.

A ***PersistentVolumeClaim*** (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany, or ReadWriteMany, see [AccessModes](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes)).

There are two types of PVs: Static (Static PVs are created by an administrator and are always available) and Dynamic (Dynamic PVs are created on demand and are destroyed when they are no longer needed).

### **Task 1)Add a Persistent Volume to your Deployment todo app**

Step 1) Create a Persistent Volume using a file on your node

create a file called pv.yaml

```yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-todo-app
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/tmp/data"
```

Step 2) Let us create the Persistent Volume Claims named pvc.yaml.

```yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: persistent-volume-claims-todo
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
```

Step 3) Let’s update the deployment.yml file to include the Persistent Volume Claims.

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
      volumes:
        - name: todo-app-data
          persistentVolumeClaim:
            claimName: pvc-todo-app
```

Step 4) Let’s apply all the above definitions one by one.

```yaml
kubectl apply -f pv.yaml 
kubectl apply -f pvc.yaml
kubectl apply -f deployment.yml -n deploy1
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692640815605/ae3ada95-e419-4133-afdf-75943e863e2e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692640893770/fe48c4e2-7c07-46be-8acf-44592867aed7.png align="center")

### **Task 2: Access the data in the Persistent Volume.**

Let’s connect to any pod in the cluster using:

```yaml
kubectl exec -n day-20-namespace mysql-configuration-74758c649d-gj58g  -it -- /bin/sh
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692641084048/d6268d96-7e47-4180-953d-2767d78c6002.png align="center")

---

Thanks for reading ;)
