#90DaysOfDevOps Challenge - Day 36 - Managing Persistent Volumes in Your Deployment

#90DaysOfDevOps Challenge - Day 36 - Managing Persistent Volumes in Your Deployment

Welcome to Day 36 of the #90DaysOfDevOps challenge. In today's challenge, we will explore how to manage Persistent Volumes in your Kubernetes deployment. Persistent Volumes (PVs) provide a powerful mechanism to store and manage data that needs to persist beyond the lifecycle of a Pod. Let's dive in and master this important aspect of Kubernetes.

What are Persistent Volumes in Kubernetes?

Persistent Volumes (PVs) in Kubernetes are a vital component for managing storage in containerized environments. They provide a reliable and persistent storage solution for applications running in Kubernetes clusters.

PVs act as an abstraction layer that separates the details of the underlying storage infrastructure from the application. This allows administrators to manage storage resources independently, providing flexibility and scalability.

With PVs, administrators can define and allocate storage resources to applications based on their specific requirements. PVs support different storage types, including local storage, network-attached storage (NAS), and cloud-based storage solutions.

The lifecycle of a PV is decoupled from the lifecycle of a pod, meaning that data stored in a PV is preserved even if the pod is terminated or rescheduled. This is especially important for stateful applications that require data persistence.

To use a PV, applications create Persistent Volume Claims (PVCs) that request storage resources. The PVCs are then bound to available PVs based on their capacity, access modes, and other specified criteria. This dynamic provisioning allows for efficient resource utilization and simplifies the process of allocating storage to applications.

Once a PV is bound to a PVC, the application can access the storage as a mounted volume. This allows data to be read from and written to the storage by the application running inside the pod.

Monitoring and managing PVs can be done using Kubernetes commands and tools. Administrators can view the status of PVs, track resource utilization, and perform maintenance tasks such as resizing or deleting PVs.

In summary, Persistent Volumes in Kubernetes provide a powerful mechanism for managing storage in containerized environments. They ensure data availability, enable data persistence for stateful applications, and offer flexibility in choosing storage solutions. By leveraging PVs, administrators can optimize resource allocation, enhance application performance, and simplify storage management in Kubernetes clusters.

Task 1 - Adding a Persistent Volume to your Deployment

Follow these steps to add a Persistent Volume to your Deployment:

  1. Create a Persistent Volume (PV):

     apiVersion: v1
     kind: PersistentVolume
     metadata:
       name: nginx-pv-volume
     spec:
       storageClassName: standard
       capacity:
         storage: 1Gi
       accessModes:
         - ReadWriteOnce
       hostPath:
         path: "/mnt/nginx"
    
  2. Run the Persistent Volume file on the server using the kubectl command:

     kubectl apply -f pv.yml -n dev
    

  3. Create a Persistent Volume Claim (PVC) file with the required configurations:

     apiVersion: v1
     kind: PersistentVolumeClaim
     metadata:
       name: nginx-pv-claim
     spec:
       storageClassName: standard
       accessModes:
         - ReadWriteOnce
       resources:
         requests:
           storage: 500Mi
    
  4. Run the Persistent Volume Claim file on the server:

     kubectl apply -f pvc.yml -n dev
    

  5. Update the deployment file to include the necessary configurations to attach the Persistent Volume:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: nginx-pv-deployment
     spec:
       replicas: 2
       selector:
         matchLabels:
           app: nginx
       template:
         metadata:
           labels:
             app: nginx
         spec:
           containers:
             - name: nginx-container
               image: nginx
               ports:
                 - containerPort: 80
                   name: "http-server"
               volumeMounts:
                 - mountPath: "/usr/share/nginx/html"
                   name: nginx-storage
               resources:
                 limits:
                   memory: 512Mi
                   cpu: "1"
                 requests:
                   memory: 256Mi
                   cpu: "0.2"
           volumes:
             - name: nginx-storage
               persistentVolumeClaim:
                 claimName: nginx-pv-claim
    
  6. Apply the updated deployment file on the server:

     kubectl apply -f deployment.yml -n dev
    

  7. Check the running Pods on the server to ensure the changes have taken effect:

     kubectl get pods -n dev
    

  8. Verify the status of the Persistent Volume running on the server:

     kubectl get pv -n dev
    

Task 2 - Accessing data in the Persistent Volume

After adding a Persistent Volume to your deployment, it's important to ensure that you can access the data stored in it. Follow these steps:

  1. Connect to a Pod in your deployment using the command:

     kubectl exec -it <pod-name> -n dev -- sh
    

    This will open an interactive shell within the Pod. Create a test file in the persistent volume to test it:

  2. To ensure a clean state, delete the existing pod by deleting the deployment. Then, after reapplying the deployment, verify if the file in the newly created pod is successfully generated.

     kubectl delete -f deployment.yml -n dev
     kubectl get pods -n dev
    

    This step ensures that the deployment process is properly updating and managing the pods within your Kubernetes cluster.

By completing these tasks, you will gain valuable hands-on experience in managing Persistent Volumes in your Kubernetes deployment. Stay tuned for Day 37 of the #90DaysOfDevOps challenge, where we will dive into some useful Kubernetes Interview Questions.

Did you find this article valuable?

Support Esteban Moreno by becoming a sponsor. Any amount is appreciated!