How to get the relationship between the Persistent Volumes and the Pods in Kubernetes
I was asked to provide a way to tell the relationship between a Persistent Volume (PV) and the Pods in a Kubernetes environment.
First, there is another Kubernetes resource in the middle: a Persistent Volume Claim (PVC). So, a PVC is associated with a PV, and a Pod can mount a PVC. It’s that simple!
So, now let’s see how we can get the association between a PVC and PV.
The following Matrix-style command returns all PVCs and their respective PVs:
kubectl get pvc --all-namespaces -o json | jq -j '.items | "\(.metadata.namespace), \(.metadata.name), \(.spec.volumeName)\n"'
Now the following command returns the association between the Pods and the PVCs, showing the Pod name, namespace, and the PVC:
kubectl get po -o json --all-namespaces | jq -j '.items | "\(.metadata.namespace), \(.metadata.name), \(.spec.volumes.persistentVolumeClaim.claimName)\n"' | grep -v null
Now if you combine these two commands, you can determine which Pod is using which PVC.
Bring your plan to the IBM Garage.
Are you ready to learn more about working with the IBM Garage? We’re here to help. Contact us today to schedule time to speak with a Garage expert about your next big idea. Learn about our IBM Garage Method, the design, development and startup communities we work in, and the deep expertise and capabilities we bring to the table.
Schedule a no-charge visit with the IBM Garage.