Kubectl tips and tricks

Retrieving Pod Name

Often times you need to get the name of a pod. We can easily retrieve this and store it into a shell variable. In this case we’ll retrieve a pod by it’s label:

kubectl get pods --all-namespaces -lapp=my-awesome-app -o jsonpath='{.items[0].metadata.name}

Now you can take the output of this command and store it in a variable like:

MY_POD=$(kubectl get pods --all-namespaces -lapp=my-awesome-app -o jsonpath='{.items[0].metadata.name})

So later you can run commands like:

kubectl logs -f $MY_POD
kubectl exec -it $MY_POD sh

…and so on!

2 Likes