After successfully tackling ConfigMaps and Secrets in our last session, we're gearing up for a deep dive into managing persistent volumes in our Kubernetes deployment.
What are Persistent Volumes in k8s ?
In the realm of Kubernetes, a Persistent Volume (PV) stands as a core asset, representing a distinct storage unit within the cluster. These volumes are provisioned autonomously, existing independently of any particular Pod or application. Cluster administrators wield YAML or JSON configuration files to create and oversee these
What are Persistent Volume Claims (PVCs) in Kubernetes?
A Persistent Volume Claim (PVC) is a user's request for storage resources from a PV. PVCs are created by application developers or users and specify their storage needs.
PVs and PVCs Working
The interaction between PVs and PVCs is essential for Kubernetes storage management:
Cluster administrators create one or more PVs to make storage resources available in the cluster.
Application developers or users create PVCs that request specific storage resources based on their needs.
Kubernetes attempts to bind each PVC to a suitable PV. Check the binding status with:
kubectl get pvc
.Pods can reference PVCs in their specifications, allowing them to use the associated storage
Today's tasks:
Task 1
Add a Persistent Volume to your Deployment todo app.
Create a Persistent Volume using a file on your node. Template
Create a Persistent Volume Claim that references the Persistent Volume. Template
Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml your deployment file look like this Template
Apply the updated deployment using the command:
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. Use this commands
kubectl get pods
,kubectl get pv
Solution
Step 1: Lets create pv.yaml file like below
Step 2: Apply pv.yaml with the help of kubectl apply -f pv.yaml
Step 3: Create pvc.yaml
Step 4: Apply pvc.yaml with the help of kubectl apply -f pvc.yaml
Step 5: After creating the PV and PVC, update your deployment.yml file to include the Persistent Volume Claim. Ensure it references the PVC we just created.
Step 6:Apply updated deployment with kubectl apply -f deploy.yaml
Task 2:
Accessing data in the Persistent Volume,
Connect to a Pod in your Deployment using command : `kubectl exec -it -- /bin/bash
Verify that you can access the data stored in the Persistent Volume from within the Pod
Solution
Step 1: Connect to a pod
Step 2: Verify the volume by accessing /app folder and create file inside the volume.
Step 3: Now will delete current pod. Kubernetes will automatically recreate the new pod against the deleted one.
Step 4: Verify the volume in new POD and abe to access file which we created in deleted POD.
In our Kubernetes deployment, we've effectively handled Persistent Volumes.
We've acquired the skills to create PVs and PVCs, seamlessly integrate them into our deployment, and access data stored within a Persistent Volume.
Thanks,
Kishor Chavan