# Managing Persistent Volumes in Your Deployment .

**Day37 of 90daysofdevopschallenge initiated by Shubham Londhe**

**What are Persistent Volumes in k8s**

In Kubernetes, a **Persistent Volume (PV)** is a piece of storage in the cluster that has been provisioned by an administrator. It represents physical storage, such as a disk in a data centre or cloud storage. PVs are used to store data that should survive beyond the life of a pod.

A **Persistent Volume Claim (PVC)** is a request for storage by a user. The PVC references the PV, and the PV is bound to a specific node. It's a way for pods to claim a specific amount of storage from the available PVs

## Today's tasks:

### Task 1:

Add a Persistent Volume to your Deployment todo app.

* Create a Persistent Volume using a file on your node.
    
    ```typescript
      #persistentvolume.yml
      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: todo-app-pv
        namespace: my-app
      spec:
        capacity:
          storage: 1Gi
        accessModes:
          - ReadWriteOnce
        hostPath:
          path: /path/to/your/host
    ```
    
* Create a Persistent Volume Claim that references the Persistent Volume.
    
    ```typescript
     apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: todo-app-pvc
        namespace: my-app
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
         requests:
           storage: 1Gi
    ```
    
* Update your deployment.yml file to include the Persistent Volume Claim.
    
    ```typescript
    #deployment.yml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: todo-app
        namespace: my-app
      spec:
        replicas: 1
        template:
          metadata:
            labels:
              app: todo-app
          spec:
            containers:
              - name: todo-app-container
                image: your-image:tag
                ports:
                  - containerPort: 80
                volumeMounts:
                  - name: persistent-volume-claim
                    mountPath: /path/in/container
            volumes:
              - name: persistent-volume-claim
                persistentVolumeClaim:
                  claimName: todo-app-pvc
    ```
    
* Apply the updated deployment using the command: `kubectl apply -f deployment.yml`
    
    ```typescript
      kubectl apply -f deployment.yml -n my-app
    ```
    
* Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster. Use this commands `kubectl get pods`.
    

```typescript

  kubectl get pods -n my-app

  #to check the status of persistent volume
  kubectl get pv -n my-app
```

Make sure that the pods are running and persistent volume is in a bound state.

### **Task 2:**

**Accessing data in the Persistent Volume,**

**Connect to a Pod in your Deployment using the command.**

**Verify that you can access the data stored in the Persistent Volume from within the Pod**

**Steps:**

* Check the status of pods.
    
    ```typescript
      kubectl get pods -n my-app
    ```
    
    It will show you the lists of all the running pods.
    
* Go to your worker node and check the latest container running.
    
    ```typescript
      #in worker node
      docker ps
    ```
    
* Then go inside the latest container which should be running by using the command.
    
    ```typescript
      docker exec -it <container-id> bash
    ```
    
    It will allow you to access the running docker container.
    
* Inside the container, access your my-SQL database by using the command.
    
    ```typescript
      #inside the container
      mysql -u root -p
    ```
    
    It will ask you password which you have set in Secrets. Enter that password to access my SQL database.
    
* To check the databases you have set in an earlier task.
    
    ```typescript
      #in mysql
      show databases;
    ```
    
    It shows the database name you have set in earlier steps.
    
* To verify that you can access the data stored in persistent volume. Go to the path which you have given in the persistent volume and do the following command to check the data.
    
    ```typescript
      #show the list of data stored in persistent volume
      ls
    ```
    
    That's all for today. Hope you find it useful. Thanks for your support and appreciation.
