Skip to content

Instantly share code, notes, and snippets.

@salehi
Created January 4, 2026 20:52
Show Gist options
  • Select an option

  • Save salehi/da793ac61036f0c6c975310c95600eb4 to your computer and use it in GitHub Desktop.

Select an option

Save salehi/da793ac61036f0c6c975310c95600eb4 to your computer and use it in GitHub Desktop.

Exec into a Pod by Label

kubectl exec doesn't support label selectors directly. Use a subshell to grab the pod name first:

kubectl exec -it $(kubectl get pod -l app.kubernetes.io/name=api -o jsonpath='{.items[0].metadata.name}') -- bash

Edge Cases

  • Multiple pods? This grabs the first one. List all with:
  kubectl get pod -l app.kubernetes.io/name=api
  • Multi-container pod? Add -c <container-name>
  • Different namespace? Add -n <namespace> to both commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment