Day 36 : Managing Persistent Volumes in Your Deployment
Welcome to Day 36 of our comprehensive guide to mastering Kubernetes deployments. As we venture deeper into the world of container orchestration, it becomes evident that persistence is a crucial aspect of managing statefull applications. In this installment, we shift our focus to explore the essential topic of “Managing Persistent Volumes in Your Deployment
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.
There are two types of PVs: Static (Static PVs are created by an administrator and are always available) and Dynamic (Dynamic PVs are created on demand and are destroyed when they are no longer needed).
Pods can access PVs through PersistentVolumeClaims (PVCs). I will discuss PVC in detail in the next section.
PV is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes but have a lifecycle independent of any individual Pod that uses the PV.
There are three Volume Access Modes:
- RWO(ReadWriteOnce)
- ROX(ReadOnlyMany)
- RWX(ReadWriteMany)
Today’s tasks:
Task 1)Add a Persistent Volume to your Deployment todo app.
Step 1) Create a Persistent Volume using a file on your node
create a file called pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-todo-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/tmp/data"
Step 2) Let us create the Persistent Volume Claims named pvc.yaml.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: persistent-volume-claims-todo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
Step 3) Let’s update the deployment.yml file to include the Persistent Volume Claims.
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app-deployment
namespace: deploy1
spec:
replicas: 1
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: rishikeshops/todo-app
ports:
- containerPort: 8000
volumeMounts:
- name: todo-app-data
mountPath: /app
volumes:
- name: todo-app-data
persistentVolumeClaim:
claimName: pvc-todo-app
Step 4) Let’s apply all the above definitions one by one.
kubectl apply -f pv.yaml
kubectl apply -f pvc.yaml
kubectl apply -f deployment.yml -n deploy1
Let’s check if the Persistent Volume has been added to my deployment by checking the status of the Pods and Persistent Volumes in my cluster.
kubectl get pv
kubectl get pvc
kubectl describe pvc <name>
We have created PV and applied the PVC.
Task 2: Access the data in the Persistent Volume.
Let’s connect to any pod in the cluster using:
kubectl exec -it <Pod_name> -n <namespace> -- /bin/bash
Now enter the /app directory, create a file, and exit the terminal.
cd /app
echo “hello” > test.txt
Let us delete this pod, so the K8s cluster will create a new pod using its auto-healing feature.
kubectl delete pods <pod_name> -n <namespace>
Now make note of the Pod name. Go to the worker node and copy the corresponding container ID of the pod that just got created. Also, go to the /app directory to check if the volume is mounted or not.
docker ps -it <container_id> /bin/bash
cd /app
ls
Here, the volume is mounted! That means we can access the data stored in the Persistent Volume from within the Pod.