What are Persistent Volumes in k8s?
Kubernetes persistent volumes are administrator-provided volumes. They have predefined properties including file system, size, and identifiers like volume ID and name.
Persistent volumes are independent of the lifecycle of the pod that uses it, meaning that even if the pod shuts down, the data in the volume is not erased. They are defined by an API object, which captures the implementation details of storage such as NFS file shares, or specific cloud storage systems.
For a Pod to start using these volumes, it must request a volume by issuing a persistent volume claim (PVC). PVCs describe the storage capacity and characteristics a pod requires, and the cluster attempts to match the request and provision the desired persistent volume.
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.