Search code examples
kuberneteskubectl

Single command to display logs from running kubernetes pod


Want to have a single command to have continue streaming of logs from particular pod. Currently I have to do

  • kubectl get pods (lists all the running pods)
  • check for the running pod from the list and copy the pod name
  • kubectl logs <pod name> -f (continuous streaming of logs from pod)

Bonus points: list pods starting with certain word like, kubectl get pods asset*. Which would just display pods with names starting with asset


Solution

  • Finally was able to figure out the solution. This would be somewhat hacky, but I would basically use --field-selector=status.phase=Running and just get the name with -o=name flag. My final command would be something like

    kubectl logs -f $(kubectl get pods --field-selector=status.phase=Running -o=name | awk '/asset/ {print $1;exit}')
    

    Links: Field Selectors