Day 36 Task: Managing Persistent Volumes in Your Deployment ๐ฅ - 90DaysOfDevOps
What are Persistent Volumes in k8s
In Kubernetes, a Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. A Persistent Volume Claim (PVC) is a request for storage by a user. The PVC references the PV, and the PV is bound to a specific node. Read official documentation of Persistent Volumes.
Today's tasks:
Task 1:
Add a Persistent Volume to your Deployment todo app.
- Create a Persistent Volume using a file on your node.
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-todo-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/tmp/data"
- Create a Persistent Volume Claim that references the Persistent Volume.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-todo-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
- Update your deployment.yml file to include the Persistent Volume Claim.
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: amana6420/node-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
kubectl apply -f pv.yml
kubectl apply -f pvc.yml
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.
ubuntu@ip-172-31-60-234:~/90DaysOfDevOps/2023/day36/90DaysOfDevOps/2023/day36$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-todo-app 1Gi RWO Retain Available 45m
pvc-ee1d932e-68e2-435f-83d0-c34785fa84e4 500Mi RWO Delete Bound default/pvc-todo-app standard 45m
ubuntu@ip-172-31-60-234:~/90DaysOfDevOps/2023/day36/90DaysOfDevOps/2023/day36$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 2 (32m ago) 3d19h
todo-app-798ddcd497-j9sxx 1/1 Running 0 2m3s
Task 2:
Accessing data in the Persistent Volume -
kubectl exec -it <pod-name> -- /bin/bash
Now, even if we delete the pod, the pod is removed but the PVC remains and the data in the PV is retained.
ย