Running an Interactive Shell Inside a Kubernetes Pod

Ever wanted to run an interactive shell inside a Kubernetes pod? Kubernetes provides support for running containers in pods, but the container has no access to the host’s filesystem.

This is where Kubernetes’ exec command comes in handy! Exec lets you execute a command or program with an arbitrary argument list on a specific node of your Kubernetes cluster.

This blog post will be discussing how to use Kubernetes exec and why it can be useful when running processes inside pods.

Access the Shell or Run Command

In the below example, I have a pod called server-deployment-f66b6c774-bwxqp which I first going to run a command against (hostname)

kubectl exec -it server-deployment-f66b6c774-bwxqp  -- hostname

To enter the interactive shell I will run the following command.

kubectl exec -it server-deployment-f66b6c774-bwxqp  -- sh

Depending on what you are tying to do your can either run a single command or enter the shell.