Day 36 Task: Managing Persistent Volumes in Your Deployment 💥
What are Persistent Volumes in k8s?
Kubernetes persistent volumes (PVs) are a unit of storage provided by an administrator as part of a Kubernetes cluster. Just as a node is a compute resource used by the cluster, a PV is a storage resource.
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.
Kubernetes persistent volumes are administrator-provided volumes. They have predefined properties including file system, size, and identifiers like volume ID and name.
In order 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. yaml, 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.
Apply the updates using the command
kubectl apply -f pv.yaml
Create a Persistent Volume Claim that references the Persistent Volume.
A Persistent Volume Claim is a request for storage by a user. It specifies the required size and access modes of the storage and is automatically bound to a matching Persistent Volume. You can create a YAML file, called PVC.yaml, that defines the Persistent Volume Claim.
Apply the updates using the command
kubectl apply -f pvc.yaml
Update your deployment.yml file to include the Persistent Volume Claim.
To use the Persistent Volume Claim in your Deployment, you need to update your Deployment file, deployment. yaml. You should add a volume and a volumeMount section to the container definition, which references the Persistent Volume Claim.
Apply the updated deployment using the command:
kubectl apply -f deployment.yaml
Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster. Use this commands
kubectl get pv
These commands will show the list of Persistent Volumes in your cluster.
Task 2:
Accessing data in the Persistent Volume,
Connect to a Pod in your Deployment using the command :
kubectl exec Pod name -it -- /bin/bash
below inside the /tmp/data folder create a new file named samplefile.txt
Verify that you can access the data stored in the Persistent Volume from within the Pod
delete the pod which we used in the above step with the following command:
kubectl delete -f deployment.yaml
and create a new pod using the command:
kubectl apply -f deployment.yaml
Go inside a pod using kubectl exec command, then go to the folder and check the samplefile.txt file.
Thank you for reading!! I hope you find this article helpful......