Skip to main content
In this lesson, you will learn how to configure persistent storage for your Kubernetes pods using Persistent Volumes (PV) and Persistent Volume Claims (PVC). This lab demonstrates how to deploy a pod, inspect its logs, and ensure that the logs persist even if the pod is deleted.

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:
This command displays the application events logged inside the container. Since these logs are stored on the container’s ephemeral filesystem, deleting the pod will also remove the logs. To confirm that no extra volumes are configured, list the pods:
Expected output:
Then, inspect the pod details:
The description reveals that only the default volume for API access is present. This confirms that the log file (/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.
  1. Verify that the host directory /var/log/webapp is empty:
  2. Edit the pod manifest to add a new volume. Run:
  3. 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:
  4. 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:
After the pod is recreated, the host directory /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:
  1. Save the above manifest to a file named pv.yaml.
  2. Create the persistent volume:
  3. Verify the PV creation:
Expected output:

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 of ReadWriteOnce:
  1. 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.
  1. Apply the PVC manifest:
  2. Check the PVC status:
You may see the status as “Pending”:
  1. Verify that the PV remains available:
To resolve the access mode incompatibility, update the PVC manifest to use ReadWriteMany:
  1. Force replace the PVC with the updated configuration:
  2. 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.
  1. Edit the pod manifest to remove the HostPath volume and replace it with the PVC reference. Use the following snippet for guidance:
  2. Force replace the pod with the updated manifest:
  3. 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:
Expected output:
A PV with a “Retain” reclaim policy is not automatically deleted when the PVC is removed. To test this:
  1. Delete the PVC:
  2. 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.
  3. Once the PVC is removed, check the PV status:
The PV should now appear in a “Released” state. With the “Retain” policy, you must manually reclaim or delete the PV if needed.

Lab Summary

In this lab, you have successfully:
  1. Viewed application logs inside a pod.
  2. Configured a HostPath volume for log persistence.
  3. Created a PersistentVolume (PV) and a corresponding PersistentVolumeClaim (PVC).
  4. Updated the pod configuration to use the PVC.
  5. Verified the reclaim policy behavior, ensuring that a PV with a “Retain” policy remains available even after deleting the PVC.
Happy learning!

Watch Video