Day 36 : Managing Persistent Volumes in Your Deployment
Task 1: Adding a Persistent Volume to Your Deployment for the todo app
To ensure data persistence in your Deployment, you can add a Persistent Volume.
Here's an example pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-todo-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/tmp/data"
This PV definition specifies a capacity of 1Gi, with read-write access for a single node. It uses a hostPath to store the data on the node's local filesystem at /tmp/data.
Create a Persistent Volume Claim (PVC) using a YAML file. Here's an example pvc.yml:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-todo-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
This PVC definition requests 500Mi of storage with read-write access. It will match the PV based on the access mode and capacity.
Update your deployment.yml file to include the Persistent Volume Claim (PVC). Here's an updated snippet:
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-app
spec:
replicas: 5
selector:
matchLabels:
app: node-app
template:
metadata:
labels:
app: node-app
spec:
containers:
- name: node-app
image: patelajay745/node-app-new:latest
ports:
- containerPort: 3000
env:
- name: MY_SECRET
valueFrom:
secretKeyRef:
name: my-secret
key: my-credentials.txt
- name: MY_CONFIG
valueFrom:
configMapKeyRef:
name: my-config
key: my-settings.conf
volumeMounts:
- name: data-volume
mountPath: /app/data
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: pvc-todo-app
In this updated deployment.yml file, we added the volumeMounts and volumes sections. The volumeMounts section specifies the mount path within the container, while the volumes section references the PVC.
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 using the following commands: kubectl get pods and kubectl get pv
Task 2: Accessing data in the Persistent Volume
Connect to a Pod in your Deployment using the following command: kubectl exec -it <pod-name> -- /bin/bash (Replace <pod-name> with the actual name of your Pod)
Once inside the Pod's shell, navigate to the directory where the data is stored. In this case, it will be mounted at /app/data based on the deployment.yml file.
Verify that you can access the data by listing the files and directories within the mounted volume. You can use commands such as ls, cat, or vi to view the contents of specific files.
By following these steps and the example provided, you can successfully manage Persistent Volumes in your Deployment and access the data stored within them.
Keep up the great work in mastering Persistent Volumes in your Deployment! If you have any questions or need further assistance, feel free to leave a comment below.
#Kubernetes #DevOps #PersistentVolumes #Deployment #LearningInProgress #TechCommunity #KeepLearning
See you another day with another challenge.



