Kubectl: Exec Shell – Login to Pod (Container)

A kubectl exec command serves for executing commands in Docker containers running inside Kubernetes Pods.

With this command it is also possible to get an interactive shell to a Docker container running inside a Pod.

In this post i will show how to login to a Pod and execute an interactive shell session using the kubectl exec command.

Login to Pod in Kubernetes

Info: If you are getting “executable file not found in $PATH” or “no such file or directory”, try to use /bin/sh instead of /bin/bash.

Get interactive shell to a Pod (if the Pod has multiple containers, you will login to a default one, i.e. the first container specified in the Pod’s config.):

$ kubectl exec -it <pod_name> -- /bin/bash

Cool Tip: List Pods in Kubernetes cluster! Read more →

To list all the containers in a Kubernetes Pod, execute:

$ kubectl get pod <pod_name> -o jsonpath='{.spec.containers[*].name}{"\n"}'

Login to a particular container in the Pod:

$ kubectl exec -it <pod_name> -c <container_name> -- /bin/bash

Login to a Pod in another Namespace:

$ kubectl exec -it <pod_name> -n <namespace> -- /bin/bash
Was it useful? Share this post with the world!

Leave a Reply