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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Viewing Application Logs Inside the Pod
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:
Configuring a HostPath Volume for Log Storage
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 Directory
First, ensure that the host directory exists and is empty:
-
Update the Pod Configuration
Next, edit the Pod configuration to add a volume named
log-volumeof type hostPath: -
Apply the Configuration
Save 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:
/var/log/webapp/app.log file.
Creating a Persistent Volume (PV)
To leverage persistent storage, create a Persistent Volume. Here is an example PV specification using a hostPath:-
Save the file as
pv.yaml. -
Create the PV using:
-
Verify that the PV is available:
Creating a Persistent Volume Claim (PVC)
Now, create a Persistent Volume Claim to request storage from the configured PV. Initially, your PVC might look like this:-
Save the file as
pvc.yaml. -
Apply it with:
-
Check the state of the PVC:
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.pvc.yaml file accordingly and execute:
Updating the Pod to Use the PVC
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:/pv/log/app.log.
Examining the Reclaim Policy and Cleanup Behavior
The persistent volumepv-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:
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.-
Delete the PVC:
-
If the PVC appears stuck, delete the Pod:
Released:
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.