2017-02-13 23:03:37 +00:00
|
|
|
---
|
2018-02-18 19:29:37 +00:00
|
|
|
reviewers:
|
2017-02-13 23:03:37 +00:00
|
|
|
- caesarxuchao
|
|
|
|
- mikedanese
|
2017-06-08 20:41:57 +00:00
|
|
|
title: Get a Shell to a Running Container
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2017-02-13 23:03:37 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
This page shows how to use `kubectl exec` to get a shell to a
|
|
|
|
running Container.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
## 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:
|
|
|
|
|
2018-07-02 18:17:19 +00:00
|
|
|
{{< codenew file="application/shell-demo.yaml" >}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
Create the Pod:
|
|
|
|
|
|
|
|
```shell
|
2018-07-02 18:17:19 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/application/shell-demo.yaml
|
2017-02-13 23:03:37 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Verify that the Container is running:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl get pod shell-demo
|
|
|
|
```
|
|
|
|
|
|
|
|
Get a shell to the running Container:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl exec -it shell-demo -- /bin/bash
|
|
|
|
```
|
|
|
|
|
2017-11-02 18:22:45 +00:00
|
|
|
In your shell, list the root directory:
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
```shell
|
2017-11-02 18:22:45 +00:00
|
|
|
root@shell-demo:/# ls /
|
2017-02-13 23:03:37 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
In your shell, experiment with other commands. Here are
|
|
|
|
some examples:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
root@shell-demo:/# ls /
|
|
|
|
root@shell-demo:/# cat /proc/mounts
|
|
|
|
root@shell-demo:/# cat /proc/1/maps
|
|
|
|
root@shell-demo:/# apt-get update
|
2017-11-02 18:22:45 +00:00
|
|
|
root@shell-demo:/# apt-get install -y tcpdump
|
2017-02-13 23:03:37 +00:00
|
|
|
root@shell-demo:/# tcpdump
|
2017-11-02 18:22:45 +00:00
|
|
|
root@shell-demo:/# apt-get install -y lsof
|
2017-02-13 23:03:37 +00:00
|
|
|
root@shell-demo:/# lsof
|
2017-11-02 18:22:45 +00:00
|
|
|
root@shell-demo:/# apt-get install -y procps
|
|
|
|
root@shell-demo:/# ps aux
|
|
|
|
root@shell-demo:/# ps aux | grep nginx
|
2017-02-13 23:03:37 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## 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:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html
|
|
|
|
```
|
|
|
|
|
|
|
|
In your shell, send a GET request to the nginx server:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
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:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
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:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl exec shell-demo env
|
|
|
|
```
|
|
|
|
|
|
|
|
Experiment running other commands. Here are some examples:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl exec shell-demo ps aux
|
|
|
|
kubectl exec shell-demo ls /
|
|
|
|
kubectl exec shell-demo cat /proc/1/mounts
|
|
|
|
```
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture discussion %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl exec -it my-pod --container main-app -- /bin/bash
|
|
|
|
```
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2017-02-13 23:03:37 +00:00
|
|
|
|
2018-04-27 22:02:19 +00:00
|
|
|
* [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)
|
2017-02-13 23:03:37 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
|
|
|
|
2017-02-13 23:03:37 +00:00
|
|
|
|
|
|
|
|