Revise sample commands to match style guide

This commit is contained in:
Tim Bannister 2020-05-23 18:18:27 +01:00
parent b635757bc0
commit 280a527a72
1 changed files with 51 additions and 47 deletions

View File

@ -9,7 +9,7 @@ content_type: task
<!-- overview --> <!-- overview -->
This page shows how to use `kubectl exec` to get a shell to a This page shows how to use `kubectl exec` to get a shell to a
running Container. running container.
@ -17,16 +17,16 @@ running Container.
## {{% heading "prerequisites" %}} ## {{% heading "prerequisites" %}}
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} {{< include "task-tutorial-prereqs.md" >}}
<!-- steps --> <!-- steps -->
## Getting a shell to a Container ## Getting a shell to a container
In this exercise, you create a Pod that has one Container. The 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: runs the nginx image. Here is the configuration file for the Pod:
{{< codenew file="application/shell-demo.yaml" >}} {{< codenew file="application/shell-demo.yaml" >}}
@ -37,117 +37,121 @@ Create the Pod:
kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml
``` ```
Verify that the Container is running: Verify that the container is running:
```shell ```shell
kubectl get pod shell-demo kubectl get pod shell-demo
``` ```
Get a shell to the running Container: Get a shell to the running container:
```shell ```shell
kubectl exec -it shell-demo -- /bin/bash kubectl exec --stdin --tty shell-demo -- /bin/bash
``` ```
{{< note >}} {{< note >}}
The double dash (`--`) separates the arguments you want to pass to the command from the kubectl arguments.
The double dash symbol "--" is used to separate the arguments you want to pass to the command from the kubectl arguments.
{{< /note >}} {{< /note >}}
In your shell, list the root directory: In your shell, list the root directory:
```shell ```shell
root@shell-demo:/# ls / # Run this inside the container
ls /
``` ```
In your shell, experiment with other commands. Here are In your shell, experiment with other commands. Here are
some examples: some examples:
```shell ```shell
root@shell-demo:/# ls / # You can run these example commands inside the container
root@shell-demo:/# cat /proc/mounts ls /
root@shell-demo:/# cat /proc/1/maps cat /proc/mounts
root@shell-demo:/# apt-get update cat /proc/1/maps
root@shell-demo:/# apt-get install -y tcpdump apt-get update
root@shell-demo:/# tcpdump apt-get install -y tcpdump
root@shell-demo:/# apt-get install -y lsof tcpdump
root@shell-demo:/# lsof apt-get install -y lsof
root@shell-demo:/# apt-get install -y procps lsof
root@shell-demo:/# ps aux apt-get install -y procps
root@shell-demo:/# ps aux | grep nginx ps aux
ps aux | grep nginx
``` ```
## Writing the root page for nginx ## Writing the root page for nginx
Look again at the configuration file for your Pod. The Pod Look again at the configuration file for your Pod. The Pod
has an `emptyDir` volume, and the Container mounts the volume has an `emptyDir` volume, and the container mounts the volume
at `/usr/share/nginx/html`. at `/usr/share/nginx/html`.
In your shell, create an `index.html` file in the `/usr/share/nginx/html` In your shell, create an `index.html` file in the `/usr/share/nginx/html`
directory: directory:
```shell ```shell
root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html # Run this inside the container
echo 'Hello shell demo' > /usr/share/nginx/html/index.html
``` ```
In your shell, send a GET request to the nginx server: In your shell, send a GET request to the nginx server:
```shell ```shell
root@shell-demo:/# apt-get update # Run this in the shell inside your container
root@shell-demo:/# apt-get install curl apt-get update
root@shell-demo:/# curl localhost apt-get install curl
curl http://localhost/
``` ```
The output shows the text that you wrote to the `index.html` file: The output shows the text that you wrote to the `index.html` file:
```shell ```
Hello shell demo Hello shell demo
``` ```
When you are finished with your shell, enter `exit`. When you are finished with your shell, enter `exit`.
## Running individual commands in a Container ```shell
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 In an ordinary command window, not your shell, list the environment
variables in the running Container: variables in the running container:
```shell ```shell
kubectl exec shell-demo env kubectl exec shell-demo env
``` ```
Experiment running other commands. Here are some examples: Experiment with running other commands. Here are some examples:
```shell ```shell
kubectl exec shell-demo ps aux kubectl exec shell-demo -- ps aux
kubectl exec shell-demo ls / kubectl exec shell-demo -- ls /
kubectl exec shell-demo cat /proc/1/mounts kubectl exec shell-demo -- cat /proc/1/mounts
``` ```
<!-- discussion --> <!-- discussion -->
## Opening a shell when a Pod has more than one Container ## Opening a shell when a Pod has more than one container
If a Pod has more than one Container, use `--container` or `-c` to If a Pod has more than one container, use `--container` or `-c` to
specify a Container in the `kubectl exec` command. For example, specify a container in the `kubectl exec` command. For example,
suppose you have a Pod named my-pod, and the Pod has two containers 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 named _main-app_ and _helper-app_. The following command would open a
shell to the main-app Container. shell to the _main-app_ container.
```shell ```shell
kubectl exec -it my-pod --container main-app -- /bin/bash 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`
{{< /note >}}
## {{% heading "whatsnext" %}} ## {{% heading "whatsnext" %}}
* [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec) * Read about [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)