hostPath and then migrate that storage to a Kubernetes PersistentVolume (PV) and PersistentVolumeClaim (PVC). Steps include inspecting the Pod and logs, adding a hostPath volume, creating a PV and PVC, resolving an accessMode mismatch, switching the Pod to use the PVC, and understanding reclaim policies and PVC termination behavior.
Table of contents
- Inspect the Pod and view logs
- Add a hostPath volume to persist logs on the host
- Create a PersistentVolume (PV)
- Create a PersistentVolumeClaim (PVC)
- Resolve accessMode mismatch and bind PVC to PV
- Update the Pod to use the PVC
- Reclaim policy and PVC deletion behavior
- Links and references
Inspect the Pod and view logs
First confirm the Pod is Running:/log/app.log inside the container. View the file directly from the Pod:
hostPath persists data only on the node where the Pod runs. In multi-node clusters a hostPath is tied to that specific node and is not portable; prefer network-backed storage (NFS, cloud volumes, CSI drivers) for production workloads.Add a hostPath volume to persist logs on the host
To persist logs on the node, add ahostPath volume to the Pod spec and mount it to the container path where the application writes logs (/log).
Pod YAML fragment (container + volumeMount):
/log/app.log inside the container are persisted on the host at /var/log/webapp/app.log:
Create a PersistentVolume (PV)
Next create aPersistentVolume to back storage. For demo purposes this PV uses hostPath, but in production use a network or cloud-backed storage class.
pv.yaml
Create a PersistentVolumeClaim (PVC)
Create a PVC that requests storage for application logs. This example intentionally starts with an accessMode mismatch to illustrate binding behavior. Initialpvc.yaml:
Pending state because the PV’s accessModes (RWX) do not match the PVC’s accessModes (RWO). Kubernetes only binds PVs to PVCs when capacity, access modes, and selectors match.
Resolve accessMode mismatch and bind PVC to PV
To bind the PVC to the PV, update the PVC to requestReadWriteMany (RWX) to match the PV. Some Pod/PVC fields cannot be modified in place, so we replace the PVC after editing.
Updated pvc.yaml:
100Mi) and access modes.
Update the Pod to use the PVC
Replace the previoushostPath volume in the Pod with a persistentVolumeClaim volume that references claim-log-1.
Pod YAML fragment using the claim:
/log/app.log is backed by the PV/PVC and the underlying host path /pv/log (from the PV) will contain the log file:
Reclaim policy and PVC deletion behavior
Check the PV’s reclaim policy:Retain, deleting the PVC does not remove the underlying storage — the PV moves to Released and the data remains in place. An administrator must manually clean or recycle the storage before the PV can be reused.
If you attempt to delete a PVC that is still in use by a Pod, PVC deletion can hang in Terminating because of the kubernetes.io/pvc-protection finalizer, which prevents accidental data loss.
Example of a stuck PVC:
webapp Pod, the finalizer prevents deletion. Delete the Pod first, then the PVC deletion will complete and the PV will transition to Released (since reclaimPolicy is Retain):
/pv/log on the host. An administrator must manually clean up or change the PV to make it Available again.
Do not forcibly remove PVCs that are in use. The
kubernetes.io/pvc-protection finalizer prevents deletion while a Pod references the PVC to protect data integrity. Always delete or update consuming Pods first, then delete the PVC if appropriate.Quick reference: PV vs PVC
This completes the steps to persist logs first with
hostPath, then with a PV/PVC, and explains common gotchas such as accessMode mismatches and PVC protection finalizers.
Links and References
- Kubernetes Persistent Volumes
- PersistentVolume and PersistentVolumeClaim
- Access Modes for Volumes
- Reclaim Policy