Step 1. Viewing Application Logs
A pod named “webapp” is running and writes its logs to/log/app.log inside the container. To view these logs, run:
/log/app.log) resides solely within the container.
Step 2. Configuring a HostPath Volume
To ensure log persistence, mount a host directory into the pod.-
Verify that the host directory
/var/log/webappis empty: -
Edit the pod manifest to add a new volume. Run:
-
In the pod specification, locate the
volumes:section (currently showing only the kube API access volume) and add a new volume for logs. For example: -
If you encounter an error about immutable fields when saving the changes, save the modified manifest to a temporary file and force-replace the pod:
/var/log/webapp will be mounted at /log in the container. Verify this by checking the log file:
Step 3. Creating a Persistent Volume (PV)
Next, create a persistent volume to provide external storage. Use the following example manifest to create a HostPath PV:-
Save the above manifest to a file named
pv.yaml. -
Create the persistent volume:
-
Verify the PV creation:
Step 4. Creating a Persistent Volume Claim (PVC)
Claiming storage requires creating a PVC. Consider the following sample PVC manifest requesting 50Mi with an access mode ofReadWriteOnce:
- Save this content to a file named
pvc.yaml.
Although the PV is configured with
ReadWriteMany, the PVC is initially set to ReadWriteOnce. This mismatch prevents the PVC from binding to the PV.-
Apply the PVC manifest:
-
Check the PVC status:
-
Verify that the PV remains available:
ReadWriteMany:
-
Force replace the PVC with the updated configuration:
- Recheck the PVC status. It should now be bound to the PV with the appropriate capacity.
Step 5. Updating the Pod to Use the PVC
With the PVC bound to the PV, update the “webapp” pod to use the PVC instead of a direct HostPath mount.-
Edit the pod manifest to remove the HostPath volume and replace it with the PVC reference. Use the following snippet for guidance:
-
Force replace the pod with the updated manifest:
-
Confirm that application logs are being written to the persistent storage on the host. For example:
Step 6. Verifying Reclaim Policy and Cleanup Behavior
Reviewing the reclaim policy ensures proper cleanup behavior. Check the PV details:-
Delete the PVC:
-
In a separate terminal or after a moment, verify the PVC status:
The PVC might display a “Terminating” status if the volume is still in use.
-
Once the PVC is removed, check the PV status:
Lab Summary
In this lab, you have successfully:- Viewed application logs inside a pod.
- Configured a HostPath volume for log persistence.
- Created a PersistentVolume (PV) and a corresponding PersistentVolumeClaim (PVC).
- Updated the pod configuration to use the PVC.
- Verified the reclaim policy behavior, ensuring that a PV with a “Retain” policy remains available even after deleting the PVC.