Tasks

Step-by-step instructions for performing operations with Kubernetes.

Documentation for Kubernetes v1.5 is no longer actively maintained. The version you are currently viewing is a static snapshot. For up-to-date documentation, see the latest version.

Edit This Page

Getting 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. If you do not already have a cluster, you can create one by using Minikube.

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:

shell-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: shell-demo
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

Create the Pod:

kubectl create -f https://k8s.io/docs/tasks/kubectl/shell-demo.yaml

Verify that the Container is running:

kubectl get pod shell-demo

Get a shell to the running Container:

kubectl exec -it shell-demo -- /bin/bash

In your shell, list the running processes:

root@shell-demo:/# ps aux

In your shell, list the nginx processes:

root@shell-demo:/# ps aux | grep nginx

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

root@shell-demo:/# ls /
root@shell-demo:/# cat /proc/mounts
root@shell-demo:/# cat /proc/1/maps
root@shell-demo:/# apt-get update
root@shell-demo:/# apt-get install tcpdump
root@shell-demo:/# tcpdump
root@shell-demo:/# apt-get install lsof
root@shell-demo:/# lsof

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:

root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html

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

root@shell-demo:/# apt-get update
root@shell-demo:/# apt-get install curl
root@shell-demo:/# curl localhost

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

Hello shell demo

When you are finished with your shell, enter exit.

Running individual commands in a Container

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

kubectl exec shell-demo env

Experiment running other commands. Here are some examples:

kubectl exec shell-demo ps aux
kubectl exec shell-demo ls /
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.

kubectl exec -it my-pod --container main-app -- /bin/bash

What’s next

Analytics

Create an Issue Edit this Page