Persistent Volumes in Deployment in K8s

Persistent Volumes in Deployment in K8s

Task 1:

Add a Persistent Volume to your Deployment todo app.

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

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

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

  • 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

Task 2:

Accessing data in the Persistent Volume,

  • Connect to a Pod in your Deployment using command : kubectl exec -it -- /bin/bash

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


Task 1

Step 1: create pv.yml file

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

Step 2: Apply the file

kubectl apply -f pv.yml

pv

Step 3: Create pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-todo-app
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi

Step 4: Apply the file

kubectl apply -f pvc.yml

pvc

Step 5: Modify or create deployment file and add volumeMounts and volumes data.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-todo-deploy
  labels:
    app: django-todo
  namespace: django-todo-ns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: django-todo
  template:
    metadata:
      labels:
        app: django-todo
    spec:
      containers:
        - name: django-todo-app
          image: vinay000/django-todo-app:latest
          ports:
            - containerPort: 8001
          volumeMounts:
            - name: todo-app-data
              mountPath: /app
      volumes:
        - name: todo-app-data
          persistentVolumeClaim:
            claimName: pvc-todo-app

Step 6: Apply the file.

kubectl apply -f django-todo-Deployment.yml

Step 8: Verify.

kubectl get pods -n=django-todo-ns
kubectl get pv -n=django-todo-ns

PVs


Task 2

Step 1: Connect to the Pod in your Deployment using command : kubectl exec -it POD-NAME /bin/bash

Step 2: Change directory to app

Step 3: Create a file and delete the Pod.

Step 4: To verify whether the volume working or not by checking the new pod that has the same file.

Enter into the Pod

pod deleted

New Pod with same file.


So this was the tutorial on creating volume in k8s. Hope you learned something from this. Thank You!