Goglides Dev 🌱

Prason Pandey
Prason Pandey

Posted on

Get a Shell to a Running Container Inside Pod - Kubernetes

Getting a shell to a running container can be very useful to debug and manage container. kubectl exec lets you get shell to container inside the pod.

This article will show you how to get a shell into a running container inside a pod.

Reasons to get shell to a running container

Here are some reasons to get shell to a running container.

  • To debug your application running inside the container.
  • To run commands inside container.
  • To install any packages inside container.
  • To investigate the internal state of the running container.

kubectl exec

You can use following command to get the shell to a running container.

kubectl exec -it <pod-name> -- /bin/sh
Enter fullscreen mode Exit fullscreen mode

Example

Let's learn by example. We will be using the following YAML file to create our pod.

apiVersion: v1
kind: Pod
metadata:
  name: nginx-app-pod
spec:
  containers:
  - name: nginx
    image: nginx

Enter fullscreen mode Exit fullscreen mode

To start running pod, you can use following command:

kubectl create -f <filename>.yaml
Enter fullscreen mode Exit fullscreen mode

You can view the running pods by using following command:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

You should see output something like this:

Image description

You can use following command to get shell in the running container inside the pod:

kubectl exec -it nginx-app-pod -- bash
Enter fullscreen mode Exit fullscreen mode

You should see output something like this:
Image description

This is all for this article. Enjoy your root access in the container. ;)

Top comments (0)