3

Need only specific columns using JSONPATH query in kubernetes:

  • $ kubectl get node
NAME  STATUS    ROLES   AGE VERSION
1     Ready     master  35d v1.18.6
2     Ready     <none>  35d v1.18.6
3     Ready     <none>  35d v1.18.6             
4     Ready     <none>  35d v1.18.6             
5     Ready     master  35d v1.18.6             
6     Ready     <none>  35d v1.18.6

Desired output should look like this:

NAME    VERSION
1       v1.18.6
2       v1.18.6
3       v1.18.6
4       v1.18.6
5       v1.18.6 
6       v1.18.6
Dawid Kruk
  • 8,982
  • 2
  • 22
  • 45
raju
  • 129
  • 1
  • 9

1 Answers1

2

Th answer to above question is:

  • $ kubectl get node -o=jsonpath='{range.items[*]}{.metadata.selfLink} {"\t"} {.status.nodeInfo.kubeletVersion}{"\n"}{end}'

It will produce output:

01    v1.18.6
02    v1.18.6
03    v1.18.6
04    v1.18.6
05    v1.18.6

For further sorting:

  • $ kubectl get node -o=custom-columns=NODE:.metadata.selfLink

  • $ kubectl get node -o=custom-columns=VERSION:.status.nodeInfo.kubeletVersion

    kubectl get node -o=custom-columns=NODE:.metadata.selfLink,VERSION:.status.nodeInfo.kubeletVersion N

Andy K
  • 4,944
  • 10
  • 53
  • 82
raju
  • 129
  • 1
  • 9
  • I think you have a typo here. If you are looking for name of the node you should use: `.metadata.name` instead of `.metadata.selfLink`. – Dawid Kruk Sep 09 '20 at 09:03
  • Not exactly .metadata.selfLink gives complete path --> """/api/v1/nodes/hostname01 v1.18.6""" Where as .metadata.name gives only host list --> """hostname01 v1.18.6""" – raju Feb 26 '21 at 17:05