Managing Persistent Volumes in Your Deployment
Hello folks! In this post I will be talking about
☸ Persistent Volumes
☸ Persistent Volume claims
☸ How to deploy them in your pods
Need for Persistent Volumes:
Suppose there is a MySQL database pod that your application uses. Data gets added/updated in the database.
Now when the pod is restarted, all changes will be gone as Kubernetes does not give us data persistence out-of-the-box.
Hence for configuring a storage that saves data between pod restarts and which does not depend on pod lifecycle, we use persistent volumes. This way, when one pod dies and a new one is created under a deployment, it can pick up where the previous one left off.
The persistent volume does not exist in one namespace and is available to all nodes.
Persistent Volume claims:
For applications to use these persistent volumes, the developers or DevOps engineers need to configure YAML files to use these volumes. Thus, claiming the persistent volume.
This is done using persistent volume claim where a part or the whole volume is bound using persistent volume claim and this claim is specified inside the deployment.yml file.
Lets dive into handson for creating PV and accessing it using PVC -
Task 1:
Add a Persistent Volume to the sample python app.
- Create a Persistent Volume using a file on your node.For creating a volume, lets
vim pv.yml
Now use this template that we used from kubernetes documentation and make the changes required:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-python-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/tmp/data"
Here, we have specified storage as 1 GB
Permission to read and write once
And stored inside path "/tmp/data"
Now to create this volume,
kubectl apply -f pv.yml
Now check using
kubectl get persistentvolumes
Now that volume is available, lets create a Persistant volume claim -
vim pvc.yml
Now use the following template make required changes:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-python-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
Everything is the same except that though the volume is of 1 GB, we are claiming just for 500Mb.
Now lets create this too
kubectl apply -f pvc.yml
Check using
kubectl get persistentvolumeclaims
Now that persistant volume and persistent volume claim have been created, we need to include it in the deployment file
vim deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-python-app
labels:
app: sample-python-app
spec:
replicas: 2
selector:
matchLabels:
app: sample-python-app
template:
metadata:
labels:
app: sample-python-app
spec:
containers:
- name: python-app
image: abhishekf5/python-sample-app-demo:v1
volumeMounts:
- name: python-app-data
mountPath: /tmp/data
ports:
- containerPort: 8000
volumes:
- name: python-app-data
persistentVolumeClaim:
claimName: pvc-python-app
Once done, apply the deployment using
kubectl get apply -f deployment.yml
Your volumes will be up in the /tmp/data inside the nodes.
To check you can kubectl exec -it <pod-name> -- /bin/bash