DEV Community

Sina Tavakkol
Sina Tavakkol

Posted on

40 Days Of Kubernetes (40/40)

Day 40/40

JSONPath Tutorial - Advanced Kubectl Commands

Video Link
@piyushsachdeva
Git Repository
My Git Repo

In this section we will deep dive into JSONPath from the beginners perspective and see how you can write advanced kubectl commands using JSONPATH

Image description

  • Return result in json format by api-server
kubectl get nodes -o json

Enter fullscreen mode Exit fullscreen mode
  • Return result in yaml format by api-server
kubectl get nodes -o yaml

Enter fullscreen mode Exit fullscreen mode
  • Sample of jsonpath:
root@sinaops:~# kubectl get nodes -o=jsonpath='{.items[*].status.nodeInfo.osImage}{"\n"}'
Ubuntu 24.04.1 LTS Ubuntu 22.04.2 LTS Ubuntu 22.04.4 LTS

Enter fullscreen mode Exit fullscreen mode
  • With custom column:
root@sinaops:~# kubectl get nodes -o='custom-columns=OsType:{.status.nodeInfo.osImage},KubeletVersion:{.status.nodeInfo.kubeletVersion}'
OsType               KubeletVersion
Ubuntu 24.04.1 LTS   v1.30.4
Ubuntu 22.04.2 LTS   v1.30.0
Ubuntu 22.04.4 LTS   v1.30.4

Enter fullscreen mode Exit fullscreen mode
  • With custom column and statement:
root@sinaops:~# kubectl get nodes -o=custom-columns='Host:{.status.addresses[?(@.type=="Hostname")].address},OsType:{.status.nodeInfo.osImage}'
Host         OsType
cloudy.net   Ubuntu 24.04.1 LTS
jolly-net    Ubuntu 22.04.2 LTS
sinaops      Ubuntu 22.04.4 LTS

Enter fullscreen mode Exit fullscreen mode
  • Sort by an item:
root@sinaops:~# kubectl get nodes
NAME         STATUS                     ROLES           AGE     VERSION
cloudy.net   Ready                      worker          6d      v1.30.4
jolly-net    Ready,SchedulingDisabled   worker          7d22h   v1.30.0
sinaops      Ready                      control-plane   8d      v1.30.4
root@sinaops:~# kubectl get nodes --sort-by=.status.nodeInfo.kubeletVersion
NAME         STATUS                     ROLES           AGE     VERSION
jolly-net    Ready,SchedulingDisabled   worker          7d22h   v1.30.0
cloudy.net   Ready                      worker          6d      v1.30.4
sinaops      Ready                      control-plane   8d      v1.30.4

Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
thaisavieira profile image
Thaísa Vieira

Congrats on completing your challenge!

Collapse
 
ethanleetech profile image
Ethan Lee

awesome! congrats on completing challenge.

Collapse
 
piyushsachdeva profile image
piyushsachdeva

Great job buddy on completing the challenge! Well done. We have two more videos :) Feel free to check those as well.

Collapse
 
sina14 profile image
Sina Tavakkol

Thank you Sir :)