Today I have covered,
What Persistent Volume (PV) and PersistentVolumeClaim (PVC) ?
How to Verify the Volume Created is Working or Not?
What are Persistent Volumes in k8s?
A
PersistentVolume (PV)
is a piece ofstorage in the cluster
that has been provisioned by an administrator or dynamically provisioned using Storage Classes.It is a resource in the cluster just like a node is a cluster resource.
PVs are volume
plugins likeVolumes
, but have a lifecycle independent of any individual Pod that uses the PV.This API object captures the details of the storage implementation, be that NFS, iSCSI, or a cloud-provider-specific storage system.
What are Persistent Volume Claims in k8s?
- A
PersistentVolumeClaim (PVC)
is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific sizes and access modes (e.g., they can be mountedReadWriteOnce
,ReadOnlyMany
orReadWriteMany
, seeAccessModes
.
So before starting the task let's do the necessary installation and setup.
- Before Doing the task let's set up the git repository for the same and push the code to the repository.
Steps to follow:
Here we will use AWS for Deployment and Git for version control.
Step-01
: Open your terminal or any other cloud service.
Step-02
: Update and install Docker
on your machine.
sudo apt update
sudo apt install docker.io
Step-03
: Now give Docker permission for super user of your local machine.
sudo usermod -aG docker $USER && newgrp docker
Step-04
: Now let's install Minikube
.
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
Step-05
: Now let's start Minikube
.
minikube start
- let's connect with docker.
minikube start — driver=docker
Step-06
: Now let's check the status of Minikube
.
minikube status
Step-07
: Now we will install the Command line instruction for Minikube which is Kubectl
.
- But first, install snap on your machine.
sudo apt install snap
- CLI for Minikube is Kubectl.
sudo snap install kubectl --classic
Step-08
: Now let's check the version of Kubectl
.
kubectl version
Step-09
: Now Clone the repository from GitHub. Github Repo link: https://github.com/LondheShubham153/django-todo-cicd.git
git clone https://github.com/LondheShubham153/django-todo-cicd.git
Step-10
: Now let's check the files in the repository.
ls
cd django-todo-cicd
Task 1:
Create a Persistent Volume of 1Gi.
- Create a file
pv.yaml
and write the code for Persistent Volume.
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-django-todo-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/mnt/data"
kubectl apply -f pv.yaml
- Create a file
pvc.yaml
and write the code for Persistent Volume Claim.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-django-todo-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
kubectl apply -f pvc.yaml
- Create a file
deployment.yaml
and write the code for Deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
name: django-todo-deployment
spec:
replicas: 1
selector:
matchLabels:
app: django-todo-app
template:
metadata:
labels:
app: django-todo-app
spec:
containers:
- name: django-todo-app
image: rohit8329/django-todo:latest
ports:
- containerPort: 8001
volumeMounts:
- name: django-todo-app-data
mountPath: /tmp/app
volumes:
- name: django-todo-app-data
persistentVolumeClaim:
claimName: pvc-django-todo-app
kubectl apply -f deployment.yaml
- Verify that the Persistent Volume has been added to your Deployment by checking the Pods and Persistent Volumes status in your cluster. Use these commands.
kubectl get pods
Task 2:
- Connect to a Pod in your Deployment using the command :
kubectl exec -it <pod-name> -- /bin/bash
- Here we can create a file in the pod and check the data in the Persistent Volume in the interactive shell.
cd /tmp/app
- Create a file
test.txt
and write some data in it.
echo "Hello World" > test.txt
At last exit from the pod.
Verify that you can access the data stored in the Persistent Volume from within the Pod by checking the contents of the file you created in the Pod.