๐ Introduction:
In this blog, we will explore Persistent Volumes & use Persistent Volumes in our deployment.
What are Persistent Volumes in k8s?
In Kubernetes, a Persistent Volume (PV) is a cluster-wide storage resource that can be used by pods to persist data beyond the lifetime of the pod. A Persistent Volume is a separate entity from the pod, and its lifecycle is managed independently from the pod.
A Persistent Volume is created by a cluster administrator and made available for use by the cluster's users. Once a Persistent Volume is created, it can be bound to a Persistent Volume Claim (PVC), which is a request for a specific amount of storage that a pod can use. When a pod requests storage using a PVC, the Kubernetes scheduler finds an available Persistent Volume that matches the request, binds the volume to the claim, and mounts the volume in the pod's container.
Many types of Persistent Volumes can be used in Kubernetes, including network-attached storage (NAS), storage area network (SAN), local storage, and cloud-based storage. Kubernetes also provides a plugin architecture for storage providers, allowing third-party storage systems to be integrated into the Kubernetes cluster.
๐ผTask :1
Add a Persistent Volume to your Deployment todo app
Steps:
- 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"
- Apply the Persistent Volume
kubectl apply -f pv.yml
- 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
- Apply the Persistent Volume Claim
kubectl apply -f pvc.yml
- Update your deployment.yml file to include the Persistent Volume Claim
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app-deployment
spec:
replicas: 2
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: sutish/django-todo-cicd:latest
ports:
- containerPort: 8000
volumeMounts:
- name: todo-app-data
mountPath: /app
volumes:
- name: todo-app-dat
persistentVolumeClaim:
claimName: pvc-todo-app
- Apply the updated deployment
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.
kubectl get pods
kubectl get pv
๐ผTask:2
Accessing data in the Persistent Volume
Steps:
- Connect to a Pod in your Deployment
kubectl exec -it todo-app-deployment-6d86879976-jgmxs -- bash
- Create a test file inside that pod
echo "adding data into the pod" >test.txt
Verify that you can access the data stored in the Persistent Volume from within the Pod
- To verify that we can access the data stored, delete the pod we have used in the above step and create a new pod. Connect to the new pod and verify whether the same file exists.
kubectl delete -f deployment.yml
kubectl apply -f deployment.yml
kubectl get pods
๐ Conclusion :
In this blog, we cover Persistent Volume in Kubernetes & do hands-on Persistent Volume. In the next blog, we will cover some Kubernetes Important Interview Questions.
Thank you for reading!
Contact me on Linkedin ๐ค
Check out my GitHub for more resources ๐