Get a Shell to a Running Container

let's list the running container using below command

C:\Users\Gaurav>kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
test-vol   1/1     Running   2          3d14h

Get a shell to the running container (test-vol):

kubectl exec -it test-vol -- /bin/bash

Note: The double dash (--) separates the arguments you want to pass to the command from the kubectl arguments.

In your shell, list the root directory:

# Run this inside the container
ls /

Running individual commands in a container

list the environment variables in the running container:

kubectl exec test-vol env

Here are some other examples:

kubectl exec test-vol -- ps aux
kubectl exec test-vol -- ls /

Opening a shell when a Pod has more than one container

If a Pod has more than one container, use --container or -c to specify a container in the kubectl exec command. For example, suppose you have a Pod named my-pod, and the Pod has two containers named main-app and helper-app. The following command would open a shell to the main-app container.

kubectl exec -i -t my-pod --container main-app -- /bin/bash