Is there kubectl filter command to get the PVC - it's volume - mounted in pod -hosted in node fields?

3/12/2020

I am trying to get pvc and it's volume and in which pod it is mounted and the node it is hosted.

There are separate commands are there like below

To get PVC and it's volume

kubectl get pvc <pvcname>

then from PVC i am getting where it is mounted

kubectl describe pvc <pvcname> | grep Mounted

Then getting the pod , i am find in which node the pod is hosted

 kubectl get pod <pod name> -o wide

As often i need to check this and having lot of PVC created by PVC config ,running one by one is complex task. May be a script can be written. Is there any other way using kubectl filter i can get these in single command?

Currently i am doing like this and finding node names where the pvc is mounted.

pvc_list=$(kubectl get pvc | awk '{print $1}')
pod_list=$(kubectl describe pvc $pvc_list | grep Mounted | awk '{print $NF}')
kubectl get pod $pod_list -o wide

But I need to get like this

PVC_name volume Pod_Name Node_Name
PvcTest   voltest  pod1   node1 
-- Samselvaprabu
command-line
kubectl
kubernetes
shell

2 Answers

3/12/2020

A PVC can be used on any number of pods so this model does not match how Kubernetes works. You could of course write your own script that ignores that possibility.

-- coderanger
Source: StackOverflow

3/12/2020

As of now i have written powershell script using pwsh 7.0 . For future readers , i record this.

$pvcobj = kubectl get pvc --no-headers |  awk '{print $1,$3}'
$pvc_list = foreach($pvc in $pvcobj){

    $pvc_name = $pvc.split(' ')[0]
    $vol_name = $pvc.split(' ')[1]    
    $pod_name = $(kubectl describe pvc $pvc_name | grep Mounted | awk '{print $NF}')
    $node_name = $(kubectl get pod $pod_name --no-headers -o wide | awk '{print $7}' )

    [pscustomobject]@{
        Pvc = $pvc_name
        Volume = $vol_name
        Pod = $pod_name
        Node = $node_name
    }
}
$pvc_list 

Store it in some ps1 file and run it

-- Samselvaprabu
Source: StackOverflow