Day 36 Task: Managing Persistent Volumes in Your Deployment

Day 36 Task: Managing Persistent Volumes in Your Deployment

Effectively Managing Persistent Volumes in Kubernetes Deployments

🔶 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. 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. Read official documentation of Persistent Volumes.

Prerequisites Make instances of t2.medium.

Install minikube, login dockerhub, build docker push image to docker hub and use that image for your deployment.yml

🔶 Task 1: Add a Persistent Volume to your Deployment todo app.

  • Create a Persistent Volume using a file on your node. Template

  •   apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: pv-django
      spec:
        capacity:
          storage: 2Gi
        accessModes:
          - ReadWriteOnce
        persistentVolumeReclaimPolicy: Retain
        hostPath:
          path: "/mnt/data"
    

  • Create a Persistent Volume Claim that references the Persistent Volume. Template

  •   apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: pvc-django
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
    

  • Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml your deployment file looks like this template.

  •   apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: todo-app-deployment
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: todo-app
        template:
          metadata:
            labels:
              app: todo-app
          spec:
            containers:
              - name: todo-app
                image: chandreshpatle28/django-todo:latest
                ports:
                  - containerPort: 8000
                volumeMounts:
                  - name: todo-app-data
                    mountPath: /app
            volumes:
              - name: todo-app-data
                persistentVolumeClaim:
                  claimName: pvc-django
    

  • Apply the updated deployment using the command: kubectl apply -f deployment.yml

  • 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 command kubectl get pods, kubectl get pv

⚠️ Don't forget: To apply changes or create files in your Kubernetes deployments, each file must be applied separately. ⚠️

🔶 Task 2: Accessing data in the Persistent Volume.

  • Connect to a Pod in your Deployment using the command: kubectl exec -it <pod-name> -- /bin/bash

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

Need help with Persistent Volumes? Check out this video for assistance.


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!