Table of contents
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.
Task 1:
Add a Persistent Volume to your Deployment todo app.
Create a Persistent Volume using a file on your node.
This is a piece of storage in your cluster that can be dynamically provisioned and claimed by a Pod. To create a Persistent Volume, you can use a file on your node. You can create a YAML file, called pv.yml, that defines the Persistent Volume. This file should include the size of the storage, the access modes, and the path to the file on your node.
- 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
- Create a file
deploymentvolumes.yaml
and write the code for Deployment.
kubectl apply -f deploymentvolumes.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.
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
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.