How to Find Which Pods are Using a PVC with kubectl?
You can run kubectl describe pvc [pvc_name]
and look at the Used By
label to find out the list of pods using that particular PersistentVolumeClaim in your Kubernetes/K8S cluster’s namespace.
$ kubectl describe pvc myclaim
Name: myclaim
Namespace: test
StorageClass: standard
Status: Bound
Volume: pv0001
Labels: <none>
Annotations: pv.kubernetes.io/bind-completed: yes
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 50Gi
Access Modes: RWO
VolumeMode: Filesystem
Used By: pod-1
pod-2
Events: <none>
The myclaim
pvc above is being used by pod-1
and pod-2
in the test
namespace.
Sometimes it might be useful to go through all the PVCs across your cluster (all namespaces) and see if there are any PVCs that are not being used by any pod. Such unused PVCs can be purged for cost efficiency or general cleanup. The following command can help find unused PVCs:
$ kubectl get pvc -A | awk '{ print $1, $2 }' | xargs -r -n2 kubectl describe pvc -n | grep -E 'Name|Capacity|Used By'
The command above describes all the PVCs across all namespaces and then prints out certain details about them – name, namespace, capacity and pods using it. If there’s a PVC not being used by any pod, it will print out like this (Used By: <none>
):
Name: postgres
Namespace: postgres-db
Capacity: 50Gi
Used By: <none>
The postgres
PVC in the postgres-db
namespace with 50Gi
storage capacity is not bound to any pod.
Note: Just because a PVC is not bound to a pod does not necessarily mean it can be removed. Maybe some pod from a deployment, statefulset or a job was using it before but got scaled down. It might scale back up in the future when need arises.