Solution Persistent Volumes and Persistent Volume Claims optional
Learn to configure persistent storage in Kubernetes by creating and managing Persistent Volumes and Persistent Volume Claims for log persistence.
In this lab exercise, you will learn how to configure persistent storage for your Kubernetes workloads by deploying a Pod that writes log entries to a file, configuring a hostPath volume to persist logs, creating a Persistent Volume (PV) and Persistent Volume Claim (PVC), resolving binding issues due to access mode mismatches, updating the Pod to use the PVC, and finally exploring the reclaim policy behavior upon deletion.
After deploying the web application Pod, you can check its status and view the logs generated by the application. The application writes its log entries to /log/app.log within the container. To view these logs, run the following command:
Copy
Ask AI
kubectl exec webapp -- cat /log/app.log
This command displays the log entries generated by your application. If the Pod were deleted, the logs stored within the container would be lost since they reside on the default ephemeral volume provided by Kubernetes. To verify the Pod configuration, use:
Copy
Ask AI
kubectl describe pod webapp
Notice that there is no additional volume configured to persist the logs.
Currently, the /log/app.log file exists only within the container’s file system. To ensure log persistence even when the Pod is recreated, you need to configure a hostPath volume. This involves mounting a directory from the host system (for example, /var/log/webapp) into the container.
Verify the Host DirectoryFirst, ensure that the host directory exists and is empty:
Copy
Ask AI
ls /var/log/webapp
Update the Pod ConfigurationNext, edit the Pod configuration to add a volume named log-volume of type hostPath:
Apply the ConfigurationSave your changes. In some cases, direct edits to a running Pod may be rejected. If so, force the update by saving the configuration to a temporary file and executing:
At this point, you might notice that the PVC is still in a Pending state due to an access mode mismatch. Although the PV has a capacity of 100Mi, the PVC requests ReadWriteOnce while the PV supports ReadWriteMany.
To resolve the pending state, update the PVC to request ReadWriteMany access mode, ensuring that it correctly binds to the PV.
Edit the pvc.yaml file accordingly and execute:
Copy
Ask AI
kubectl replace --force -f pvc.yaml
Verify that the PVC and PV are now correctly bound:
Copy
Ask AI
kubectl get pvkubectl get pvc
You should see output similar to:
Copy
Ask AI
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS AGEpv-log 100Mi RWX Retain Bound default/claim-log-1 <none> 5m5s
And:
Copy
Ask AI
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEclaim-log-1 Bound pv-log 100Mi RWX <none> 26s
Update the webapp Pod configuration to mount the storage from the PVC instead of relying on the hostPath directly. Here is the updated volume configuration snippet for the Pod:
The persistent volume pv-log is configured with a reclaim policy of Retain. This means that if the PVC is deleted, the PV is not automatically removed; it remains in the cluster with a status of Released. Check its status with:
Copy
Ask AI
kubectl get pv pv-log
Expected output:
Copy
Ask AI
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS AGEpv-log 100Mi RWX Retain Bound default/claim-log-1 <none> 9m47s
If you attempt to delete the PVC while it is still mounted by an active Pod, it may remain in a terminating state until the Pod is deleted. To fully remove the PVC and allow the PV to transition to a Released state, delete the associated Pod first.
For example:
Delete the PVC:
Copy
Ask AI
kubectl delete pvc claim-log-1
If the PVC appears stuck, delete the Pod:
Copy
Ask AI
kubectl delete pod webapp
Finally, check the PV status to confirm it has transitioned to Released:
Copy
Ask AI
kubectl get pv pv-log
This lab exercise has guided you through configuring a hostPath volume for log persistence, creating and binding a PV and PVC, updating a Pod to utilize the PVC, and understanding the effects of the reclaim policy on cleanup behavior. For more detailed Kubernetes concepts and examples, consider exploring the official Kubernetes Documentation.