Enhancing Application Resilience with Persistent Volumes in Kubernetes

Enhancing Application Resilience with Persistent Volumes in Kubernetes

Day 37: Managing Persistent Volumes in Your Deployment

ยท

3 min read

Introduction

Today's challenge focuses on fortifying the resilience and data persistence of your application by integrating a Persistent Volume into your Kubernetes Deployment. Let's dive into the tasks and ensure that your application's data remains intact even amidst pod rescheduling or movement.

Task 1: Adding Persistent Volume to Your Deployment

Step 1: Create a Persistent Volume (pv.yml)

Begin by defining a Persistent Volume (PV) that will serve as the storage for your Todo application. Update the pv.yml file with the correct path on your node and apply the Persistent Volume using the following command:

# pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  namespace: mysql
  labels:
    app: mysql
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  hostPath:
    path: /tmp
kubectl apply -f pv.yml

Step 2: Create a Persistent Volume Claim (pvc.yml)

Create a Persistent Volume Claim (PVC) that requests a specific amount of storage. Update the pvc.yml file with the required capacity and apply the Persistent Volume Claim using the following command:

# pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-volume
  namespace: mysql
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  resources:
    requests:
      storage: 1Gi
kubectl apply -f pvc.yml

Step 3: Update Deployment File (deployment.yml)

Update your Deployment file (deployment.yml) to include the Persistent Volume Claim. This ensures that each pod in your deployment has access to the persistent storage. Apply the updated deployment using the following command:

# deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: todo
    spec:
      containers:
        - name: todo-container
          image: your-todo-image:latest
          volumeMounts:
            - mountPath: "/app/data"
              name: my-persistent-storage
      volumes:
        - name: my-persistent-storage
          persistentVolumeClaim:
            claimName: my-pvc
kubectl apply -f deployment.yml

Step 4: Verify the Deployment

Check the status of your deployment and persistent volumes to ensure the changes have been applied successfully:

kubectl get pods
kubectl get pv

Task 2: Accessing Data in the Persistent Volume

Step 1: Connect to a Pod

To interact with the data stored in the Persistent Volume, connect to a pod using the following command:

kubectl exec -it <pod-name> -- /bin/bash

Step 2: Verify Access to Persistent Volume Data

Inside the pod, navigate to the mount path and verify that you can access the data:

cd /app/data
ls

Conclusion

Congratulations on successfully integrating a Persistent Volume into your Todo application's deployment! With data persistence across pod restarts, your application now boasts enhanced resilience in dynamic Kubernetes environments. Keep up the excellent work on your DevOps journey, and remember to apply changes or create files in your Kubernetes deployments separately.

Stay tuned for more thrilling challenges in the 90 Days of DevOps series! Let's continue to explore and elevate our DevOps skills together! ๐Ÿš€๐Ÿ”’

ย