34

I'm trying to build a script that can follow(-f) kubectl get pods see a realtime update when I make any changes/delete pods on Ubuntu server.

What could be the easiest/efficient way to do so?

Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28
Swapnil Patel
  • 757
  • 1
  • 7
  • 9

2 Answers2

69

You can just use

kubectl get pod <your pod name> -w

whenever any update/change/delete happen to the pod, you will see the update.

You can also use

watch -n 1 kubectl get  pod <your pod name>

This will continuously run kubectl get pod ... with 1 seconds interval. So, you will see latest state.

Koen.
  • 25,449
  • 7
  • 83
  • 78
Emruz Hossain
  • 4,764
  • 18
  • 26
  • 2
    If somebody wants to see all the pods and filter them using grep command, you can do it like watch -n 1 'kubectl get pods | grep my_filter' – Adriel Iclodean Mar 21 '22 at 12:44
  • 1
    Thank you! I've been using the `-w` flag for a while but its not a good user experience if you just want to the current state of multiple things. Been getting tired of pressing up then enter in the terminal. `watch -n .5 kubectl get pods` such a better ux. – fieldju Apr 27 '22 at 15:58
5

Adding a -w or --watch to all your kubectl get [resource]s command makes the result notified of each creation, modification, or deletion of that resource.
i.e kubectl get pod [pod_name] -w or kubectl get nodes --watch

Faramarz Qoshchi
  • 1,292
  • 1
  • 13
  • 24