Post Image

Kubernetes Access Shell in Pod

In this post I will walk you through spawning a shell session in a running pod on your Kubernetes cluster. This is very useful when getting the feel for what is happening in a container and looking at validating things during building a container deployment like proper volume mapping and other administrative tasks.


List Pods

List out your pods to get the name of a pod that is running.

$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-868c7b7964-lrcbm   1/1     Running   0          3m50s


Execute Shell in Pod

The command below will execute a bash shell in the running container. If Bash is not installed in the container image, regular shell likely will so change /bin/bash to /bin/sh

$  kubectl exec --stdin --tty nginx-868c7b7964-lrcbm -- /bin/bash


Bash Wrapper Script

Its obvious that command is a bear to remember and type every time you want to enter into a pod which is probably not very often so I find that it is easiest to make a quick and dirty bash script that takes the first argument after the command and uses it as the pod name to launch a shell into. 


kubectl exec --stdin --tty $1 -- /bin/bash

I then save this file in /usr/local/bin/execpod log out and back in and now I can exec into a pod with the command below.

$ execpod nginx-868c7b7964-lrcbm


Comments (0)
Leave a Comment