Get a Shell to a Running Container

This page shows how to use kubectl exec to get a shell to a running container.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

Getting a shell to a container

In this exercise, you create a Pod that has one container. The container runs the nginx image. Here is the configuration file for the Pod:

application/shell-demo.yaml Get a Shell to a Running Container - 图1

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: shell-demo
  5. spec:
  6. volumes:
  7. - name: shared-data
  8. emptyDir: {}
  9. containers:
  10. - name: nginx
  11. image: nginx
  12. volumeMounts:
  13. - name: shared-data
  14. mountPath: /usr/share/nginx/html
  15. hostNetwork: true
  16. dnsPolicy: Default

Create the Pod:

  1. kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml

Verify that the container is running:

  1. kubectl get pod shell-demo

Get a shell to the running container:

  1. kubectl exec --stdin --tty shell-demo -- /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:

  1. # Run this inside the container
  2. ls /

In your shell, experiment with other commands. Here are some examples:

  1. # You can run these example commands inside the container
  2. ls /
  3. cat /proc/mounts
  4. cat /proc/1/maps
  5. apt-get update
  6. apt-get install -y tcpdump
  7. tcpdump
  8. apt-get install -y lsof
  9. lsof
  10. apt-get install -y procps
  11. ps aux
  12. ps aux | grep nginx

Writing the root page for nginx

Look again at the configuration file for your Pod. The Pod has an emptyDir volume, and the container mounts the volume at /usr/share/nginx/html.

In your shell, create an index.html file in the /usr/share/nginx/html directory:

  1. # Run this inside the container
  2. echo 'Hello shell demo' > /usr/share/nginx/html/index.html

In your shell, send a GET request to the nginx server:

  1. # Run this in the shell inside your container
  2. apt-get update
  3. apt-get install curl
  4. curl http://localhost/

The output shows the text that you wrote to the index.html file:

  1. Hello shell demo

When you are finished with your shell, enter exit.

  1. exit # To quit the shell in the container

Running individual commands in a container

In an ordinary command window, not your shell, list the environment variables in the running container:

  1. kubectl exec shell-demo -- env

Experiment with running other commands. Here are some examples:

  1. kubectl exec shell-demo -- ps aux
  2. kubectl exec shell-demo -- ls /
  3. kubectl exec shell-demo -- cat /proc/1/mounts

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.

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

Note: The short options -i and -t are the same as the long options --stdin and --tty

What’s next