Power of Persistent💿Volumes in Kubernetes

Day 36 Task: Managing Persistent Volumes in Your Deployment

Saurabh Dahibhate ♾️☁️
DevOps.dev

--

New day, New Topic…. Let’s learn along..!!😉

Managing-Persistent-Volumes-in-Your-Deployment

- What are Persistent Volumes in k8s

In Kubernetes, Persistent Volumes (PVs) provide a way for users to manage durable storage for their applications. PVs abstract the underlying storage technology and provide a consistent API for users to interact with storage resources. They enable users to request storage resources that persist beyond the lifetime of a pod, which is the basic unit of deployment in Kubernetes.

Persistent Volumes are created by the cluster administrator and can be dynamically or statically provisioned. Users can request a Persistent Volume Claim (PVC) to use storage resources from a Persistent Volume. PVCs are created by the user and they specify their desired storage capacity and access modes.

PVs can be backed by various storage technologies such as network-attached storage, local storage on the host machine, cloud storage, and more. They also support various access modes such as ReadWriteOnce, ReadOnlyMany, and ReadWriteMany to enable different types of usage.

Overall, Persistent Volumes are an essential feature in Kubernetes that enable users to manage storage resources for their applications in a more scalable and efficient manner.

Here is our Today’s Task-01 :

01. Add a Persistent Volume to your Deployment todo app.

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

This is a piece of storage in your cluster that can be dynamically provisioned and claimed by a Pod. To create a Persistent Volume, you can use a file on your node. You can create a YAML file, called pv.yml, that defines the Persistent Volume. This file should include the size of the storage, the access modes, and the path to the file on your node.

Create a namespace for deployments:

kubectl create namespace <namespace name>
Create a namespace for deployments:

Create Persistent Volume File:

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

Apply the updates using command:

kubectl apply -f pv.yaml -n django-app
Create Persistent Volume File:
Create Persistent Volume File:

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

A Persistent Volume Claim is a request for storage by a user. It specifies the required size and access modes of the storage, and is automatically bound to a matching Persistent Volume. You can create a YAML file, called pvc.yml, that defines the Persistent Volume Claim.

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

Apply the updates using command:

kubectl apply -f pvc.yaml -n django-app
Create a Persistent Volume Claim that references the Persistent Volume.
Create a Persistent Volume Claim that references the Persistent Volume.

- Update your deployment.yml file to include the Persistent Volume Claim.

To use the Persistent Volume Claim in your Deployment, you need to update your Deployment file, deployment.yml. You should add a volume and a volumeMount section to the container definition, which references the Persistent Volume Claim.

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

Apply the updated deployment using the command:

kubectl apply -f deployment.yml -n django-app
Persistent Volume Claim.
Persistent Volume Claim.

- 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

These commands will show the list of Pods and Persistent Volumes in your cluster.

Pods and Persistent Volumes

Here is our Today’s Task-02 :

02. Accessing data in the Persistent Volume

- Connect to a Pod in your Deployment using command : `kubectl exec -it pod-name — — /bin/bash’

below inside app folder create a new file named demo.txt

Connect to a Pod in your Deployment using command

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

Delete a pod which we used in above step and create a new pod using command kubectl apply -f deployment.yaml

Go inside a pod using kubectl exec command, then go to app folder and check demo.txt file.

Verify that you can access the data stored in the Persistent Volume

🔶That’s all about today’s task of DevOps journey

🔸Thankyou for reading 👍.

If you liked this story then click on 👏👏 do follow for more interesting and helpful stories.

— — — — — — — — #keepLearning_DevOpsCloud ♾️☁️ — — — — — —

--

--

- ⭐Passionate Web Developer and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.