Search code examples
kuberneteskubectl

How to get and describe the a pod in a one-liner?


Using kubectl, I want to describe one of the pods (can be a random one) filtered on a labelSelector. Currently, I perform it in two steps:

  1. get the pods,
  2. Copy the name of one of the results, and describe it.
$ kubectl get pods -l app=my-app,environment=production
$ kubectl describe pods my-pod-name-copied-from-the-results

Can I do that with a one-liner, and without copying the pod name every single time?


Solution

  • A one liner to get the first pod's decribe (if scaled to more than 1):

    kubectl -n namespace describe po "$(kubectl -n namespace get po -l app.kubernetes.io/name=my-app --no-headers -o name|cut -d/ -f2|head -1)"
    

    If you need a random one (as mentioned in the other answer's comments):

    kubectl -n namespace describe po "$(kubectl -n namespace get po -l app.kubernetes.io/name=my-app --no-headers -o name|cut -d/ -f2|shuf -n1)"