To exec into a container in a pod, I use the following two commands (note the template
flag in the first command trims the output to print just the name of the pods):
$ kubectl get pods --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
app-api-6421cdf4fd-x9tbk
app-worker-432f86f54-fknxw
app-frontend-87dd65d49c-6b4mn
app-rabbit-413632c874-s2ptw
$ kubectl exec -it app-api-6421cdf4fd-x9tbk -- bash
It would be nice to exec into the container without having to discover the random guid at the end of the pod name every single time. How can I do this?
I believe you expect this.
(sed 's/pod\///'
is used below because kubectl get pod -o name
gives you names like pod/rabbitmq-0
and you should cut first part)
kubectl exec -it $(kubectl get pod -o name | sed 's/pod\///' | grep api) -- bash
Similar to previous one you can use any known commands to find and sort needed objects. Providing version from your initial question
kubectl exec -it $(kubectl get pod --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'| grep api) -- bash
You can exec into a pod using the deployment <br> You can use the following command:
kubectl exec -it deploy/<deployment-name> -- bash
Suppose you have a Deployment
with the name myadmin
and namespace demo
. After deploy, you can exec into the deployment's pod by running the below command:
$ export id=( kubectl get pod -n demo | grep 'myadmin' | awk 'END {print $1}' | xargs echo) && kubectl -n demo exec -it $id -- bash