๐Ÿš€Day 36 Task: Managing Persistent Volumes in Your Deployment ๐Ÿ’ฅ

๐Ÿš€Day 36 Task: Managing Persistent Volumes in Your Deployment ๐Ÿ’ฅ

ยท

4 min read

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.

! ๐Ÿ“ฃ Attention all #90daysofDevOps Challengers. ๐Ÿ’ช

Before diving into today's task, don't forget to share your thoughts on the #90daysofDevOps challenge ๐Ÿ’ช Let's continue to grow together ๐ŸŒฑ

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

Add a Persistent Volume to your Deployment todo app.

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

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

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

  • 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 commands 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. โš ๏ธ


Step 1: Creating a Persistent Volume

$ vi pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: todo-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/todo

Persistent Volume of 1GB in size and will use the storage location directory as /data/todo

$ kubectl apply -f pv.yaml

A persistent Volume named todo-pv is created

Now we need to create a Persistent Volume Claim that references to this Persistent Volume

Step 2: Creating a Persistent VolumeClaim

$ vi pvc.yaml

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

A persistent Volume of 1GB in size will be created

$ kubectl apply -f pvc.yaml

A persistent Volume claim named todo-pvc is created

Now we will apply the pv and pvc.yaml in deployment.yaml file to include Persistent volume claim

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodedeployment
spec:
  replicas: 2
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nodeimage
  template:
    metadata:
      name: nodeimage
      labels:
        app: nodeimage
    spec:
      containers:
      - name: nodecontainer
        image: rjthapaa/nodeimage
        volumeMounts:
        - name: todo-pv
          mountPath: /app/data
        ports:
        - containerPort: 8000
      volumes:
      - name: todo-pv
        persistentVolumeClaim:
          claimName: todo-pvc

This will mount the Persistent Volume Claim todo-pvc to the /app/data directory in the container.

$ kubectl apply -f deployment.yml

Verify that the Persistent Volume has been added to your Deployment:

kubectl get pods
kubectl get pv

Getting inside the Pods to check the volumes

# Command to get inside the container
$ kubectl exec --stdin --tty PODID -- sh

We are successfully inside the container and the

You should see the status of the pods and the persistent volume. If everything is working correctly, you should see that the Pod is running and the Persistent Volume is bound to the Persistent Volume Claim.

Verify that data is being persisted:

Create a sample todo item in your app, and then delete the Pod running the app. Verify that the todo item is still present when a new Pod is created to replace the deleted one.

To do this, first find the name of the Pod running the app:

kubectl get pods

Then delete the Pod:

kubectl delete pod <pod-name>

Finally, verify that the todo item is still present in the app by visiting the app in your web browser.

Congratulations! ๐ŸŽ‰๐ŸฅณYou have successfully added a Persistent Volume to your Deployment in Kubernetes for a todo app, and you have verified that data is being persisted across Pod restarts.

Task 2: Accessing data in the Persistent Volume

$ kubectl get pods

We will be entering into the first POD nodedeployment-549f454568-t6z5z

and creating a text.file in /app/data (PV storage location)

# Command to get inside the container
$ kubectl exec --stdin --tty PODID -- sh

You accessed Persistent Volume within the Pod, and created a file called text.txt.

Let's Delete the Pod, where we created a text.txt file and see if we can access it in another pod or not

$ kubectl delete pod <pod-name>

Wait a few moments for another Pod to be recreated.

Getting into another pod to check whether the volume exists or not

See we successfully accessed the volumes that had a text.txt file even the pods is deleted.

ย