Published : 2022-09-07

Find all volumes used by all Kubernetes pods

Simple sheetcheat this day, if you need to find all used volumes of a given flexVolume to be found.

Here is a simple example showing all shares for NFS flexVolume driver:

kubectl get pod -A -o json | jq  -r '.items[].spec.volumes[]? | select(.flexVolume.options.share != null) .flexVolume.options.share' | sort -u

For CIFS flexVolume driver you can do similar, it’s another attribute

kubectl get pod -A -o json | jq  -r '.items[].spec.volumes[]? | select(.flexVolume.options.networkPath != null) .flexVolume.options.networkPath' | sort -u

Let’s find all configMaps now:

kubectl get pod -A -o json | jq  -r '.items[].spec.volumes[]? | select(.configMap.name != null) .configMap.name' | sort -u

All secrets this time:

kubectl get pod -A -o json | jq  -r '.items[].spec.volumes[]? | select(.secret.secretName != null) .secret.secretName' | sort -u

We can also see all hostPath mounted on pods:

kubectl get pod -A -o json | jq  -r '.items[].spec.volumes[]? | select(.hostPath.path != null) .hostPath.path' | sort -u

And to finish all PersistentVolumeClaims (PVC):

kubectl get pod -A -o json | jq  -r '.items[].spec.volumes[]? | select(.persistentVolumeClaim.claimName != null) | .persistentVolumeClaim.claimName'| sort -u