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

Cloud Engineer and DevOps enthusiast, just talking about AWS, Docker, Linux, Kubernetes, Python, and automation.
Search for a command to run...

Cloud Engineer and DevOps enthusiast, just talking about AWS, Docker, Linux, Kubernetes, Python, and automation.
No comments yet. Be the first to comment.
The #90DaysOfDevOps Challenge is a dedicated initiative aimed at promoting continuous learning and skill development in the field of DevOps over a 90-day period.
Running your own home lab used to mean expensive hardware, complex networking, and hours of manual configuration. This project changes that. With a single Raspberry Pi, Docker Compose, and a few hundr

Welcome to an advanced hybrid directory demo where we'll guide you through setting up a hybrid environment, connecting to simulated on-premises infrastructure, creating a managed Microsoft AD within AWS, establishing a two-way forest trust, and integ...

Hello DevOps Community! Today marks the completion of the #90DaysOfDevOps Challenge, and it's a moment to reflect on the journey we've undertaken together. Throughout this period, I've engaged with various technologies, refining my skills in the DevO...

In a modern cloud infrastructure setup, creating a robust and scalable 2-tier architecture is essential. Such an architecture helps meet the demands of your applications while ensuring repeatability, reliability, and security. In this article, we'll ...

Welcome to Part 4 of my series on building a comprehensive CI/CD pipeline in AWS. In this article, I will explore AWS CodePipeline, a powerful service for building and deploying applications in a continuous integration and continuous delivery (CI/CD)...

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.

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.
Follow these steps to add a Persistent Volume to your Deployment:
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"
Run the Persistent Volume file on the server using the kubectl command:
kubectl apply -f pv.yml -n dev

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
Run the Persistent Volume Claim file on the server:
kubectl apply -f pvc.yml -n dev

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
Apply the updated deployment file on the server:
kubectl apply -f deployment.yml -n dev

Check the running Pods on the server to ensure the changes have taken effect:
kubectl get pods -n dev

Verify the status of the Persistent Volume running on the server:
kubectl get pv -n dev

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:
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:

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.