Day 36: Managing Persistent Volumes in Kubernetes.

Day 36: Managing Persistent Volumes in Kubernetes.

Today I have covered,

  • What Persistent Volume (PV) and PersistentVolumeClaim (PVC) ?

  • How to Verify the Volume Created is Working or Not?

What are Persistent Volumes in k8s?

  • A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using 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 storage implementation, be that NFS, iSCSI, or a cloud-provider-specific storage system.

What are Persistent Volume Claims in k8s?

  • 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 sizes and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany, see AccessModes.

So before starting the task let's do the necessary installation and setup.

  • Before Doing the task let's set up the git repository for the same and push the code to the repository.

Steps to follow: Here we will use AWS for Deployment and Git for version control.

Step-01: Open your terminal or any other cloud service.

Step-02: Update and install Docker on your machine.

sudo apt update
sudo apt install docker.io

Screenshot from 2023-04-17 22-08-47

Screenshot from 2023-04-17 22-15-44

Step-03: Now give Docker permission for super user of your local machine.

sudo usermod -aG docker $USER && newgrp docker

Screenshot from 2023-04-17 22-24-25

Step-04: Now let's install Minikube.

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

Screenshot from 2023-04-17 22-19-41

Screenshot from 2023-04-17 22-20-07

Screenshot from 2023-04-17 22-20-36

Step-05: Now let's start Minikube.

minikube start
  • let's connect with docker.
minikube start  driver=docker

Screenshot from 2023-04-17 22-22-18

Screenshot from 2023-04-17 23-06-54

Step-06: Now let's check the status of Minikube.

minikube status

Step-07: Now we will install the Command line instruction for Minikube which is Kubectl.

  • But first, install snap on your machine.
sudo apt install snap

Screenshot from 2023-04-17 23-10-55

  • CLI for Minikube is Kubectl.
sudo snap install kubectl --classic

Screenshot from 2023-04-17 23-12-11

Step-08: Now let's check the version of Kubectl.

kubectl version

Step-09: Now Clone the repository from GitHub. Github Repo link: https://github.com/LondheShubham153/django-todo-cicd.git

git clone https://github.com/LondheShubham153/django-todo-cicd.git

Step-10: Now let's check the files in the repository.

ls
cd django-todo-cicd

Screenshot from 2023-05-05 16-03-41


Task 1:

Create a Persistent Volume of 1Gi.

  • Create a file pv.yaml and write the code for Persistent Volume.
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-django-todo-app
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/mnt/data"

Screenshot from 2023-06-16 23-55-57

kubectl apply -f pv.yaml

Screenshot from 2023-06-16 23-41-05

  • Create a file pvc.yaml and write the code for Persistent Volume Claim.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-django-todo-app
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi

Screenshot from 2023-06-16 23-56-27

kubectl apply -f pvc.yaml

Screenshot from 2023-06-16 23-41-33

  • Create a file deployment.yaml and write the code for Deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-todo-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: django-todo-app
  template:
    metadata:
      labels:
        app: django-todo-app
    spec:
      containers:
        - name: django-todo-app
          image: rohit8329/django-todo:latest
          ports:
            - containerPort: 8001
          volumeMounts:
            - name: django-todo-app-data
              mountPath: /tmp/app
      volumes:
        - name: django-todo-app-data
          persistentVolumeClaim:
            claimName: pvc-django-todo-app

Screenshot from 2023-06-16 23-56-54

kubectl apply -f deployment.yaml

Screenshot from 2023-06-16 23-42-11

  • Verify that the Persistent Volume has been added to your Deployment by checking the Pods and Persistent Volumes status in your cluster. Use these commands.
kubectl get pods

Screenshot from 2023-06-16 22-02-06


Task 2:

  • Connect to a Pod in your Deployment using the command :

Screenshot from 2023-06-16 23-42-43

kubectl exec -it <pod-name> -- /bin/bash
  • Here we can create a file in the pod and check the data in the Persistent Volume in the interactive shell.
cd /tmp/app
  • Create a file test.txt and write some data in it.
echo "Hello World" > test.txt
  • At last exit from the pod.

  • Verify that you can access the data stored in the Persistent Volume from within the Pod by checking the contents of the file you created in the Pod.


Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!