kubectl exec Into a Pod Without Knowing the Pod Name

February 13, 2023

One common task in Kubernetes is to exec into a pod to run commands. The typical way to do this is as follows:

kubectl exec -it pod/$POD_NAME -- /bin/bash

This pattern typically works fine. However, if you are frequently creating and deleting new pods, the name will constantly change. This means you will have to retype the command with the new pod name instead of just using the up arrow to select a recent command. A more consistent way is to use the deployment name instead:

kubectl exec -it deployment/$DEPLOYMENT_NAME -- /bin/bash