Skip to content

Commit 485dd1d

Browse files
authored
Merge pull request #21151 from sftim/20200523_revise_get_shell_running_container
Revise “Get a Shell to a Running Container”
2 parents 36ab052 + 280a527 commit 485dd1d

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

content/en/docs/tasks/debug-application-cluster/get-shell-running-container.md

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ content_type: task
99
<!-- overview -->
1010

1111
This page shows how to use `kubectl exec` to get a shell to a
12-
running Container.
12+
running container.
1313

1414

1515

1616

1717
## {{% heading "prerequisites" %}}
1818

1919

20-
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
20+
{{< include "task-tutorial-prereqs.md" >}}
2121

2222

2323

2424

2525
<!-- steps -->
2626

27-
## Getting a shell to a Container
27+
## Getting a shell to a container
2828

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

3232
{{< codenew file="application/shell-demo.yaml" >}}
@@ -37,117 +37,121 @@ Create the Pod:
3737
kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml
3838
```
3939

40-
Verify that the Container is running:
40+
Verify that the container is running:
4141

4242
```shell
4343
kubectl get pod shell-demo
4444
```
4545

46-
Get a shell to the running Container:
46+
Get a shell to the running container:
4747

4848
```shell
49-
kubectl exec -it shell-demo -- /bin/bash
49+
kubectl exec --stdin --tty shell-demo -- /bin/bash
5050
```
51-
{{< note >}}
52-
53-
The double dash symbol "--" is used to separate the arguments you want to pass to the command from the kubectl arguments.
5451

52+
{{< note >}}
53+
The double dash (`--`) separates the arguments you want to pass to the command from the kubectl arguments.
5554
{{< /note >}}
5655

5756
In your shell, list the root directory:
5857

5958
```shell
60-
root@shell-demo:/# ls /
59+
# Run this inside the container
60+
ls /
6161
```
6262

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

6666
```shell
67-
root@shell-demo:/# ls /
68-
root@shell-demo:/# cat /proc/mounts
69-
root@shell-demo:/# cat /proc/1/maps
70-
root@shell-demo:/# apt-get update
71-
root@shell-demo:/# apt-get install -y tcpdump
72-
root@shell-demo:/# tcpdump
73-
root@shell-demo:/# apt-get install -y lsof
74-
root@shell-demo:/# lsof
75-
root@shell-demo:/# apt-get install -y procps
76-
root@shell-demo:/# ps aux
77-
root@shell-demo:/# ps aux | grep nginx
67+
# You can run these example commands inside the container
68+
ls /
69+
cat /proc/mounts
70+
cat /proc/1/maps
71+
apt-get update
72+
apt-get install -y tcpdump
73+
tcpdump
74+
apt-get install -y lsof
75+
lsof
76+
apt-get install -y procps
77+
ps aux
78+
ps aux | grep nginx
7879
```
7980

8081
## Writing the root page for nginx
8182

8283
Look again at the configuration file for your Pod. The Pod
83-
has an `emptyDir` volume, and the Container mounts the volume
84+
has an `emptyDir` volume, and the container mounts the volume
8485
at `/usr/share/nginx/html`.
8586

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

8990
```shell
90-
root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html
91+
# Run this inside the container
92+
echo 'Hello shell demo' > /usr/share/nginx/html/index.html
9193
```
9294

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

9597
```shell
96-
root@shell-demo:/# apt-get update
97-
root@shell-demo:/# apt-get install curl
98-
root@shell-demo:/# curl localhost
98+
# Run this in the shell inside your container
99+
apt-get update
100+
apt-get install curl
101+
curl http://localhost/
99102
```
100103

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

103-
```shell
106+
```
104107
Hello shell demo
105108
```
106109

107110
When you are finished with your shell, enter `exit`.
108111

109-
## Running individual commands in a Container
112+
```shell
113+
exit # To quit the shell in the container
114+
```
115+
116+
## Running individual commands in a container
110117

111118
In an ordinary command window, not your shell, list the environment
112-
variables in the running Container:
119+
variables in the running container:
113120

114121
```shell
115122
kubectl exec shell-demo env
116123
```
117124

118-
Experiment running other commands. Here are some examples:
125+
Experiment with running other commands. Here are some examples:
119126

120127
```shell
121-
kubectl exec shell-demo ps aux
122-
kubectl exec shell-demo ls /
123-
kubectl exec shell-demo cat /proc/1/mounts
128+
kubectl exec shell-demo -- ps aux
129+
kubectl exec shell-demo -- ls /
130+
kubectl exec shell-demo -- cat /proc/1/mounts
124131
```
125132

126133

127134

128135
<!-- discussion -->
129136

130-
## Opening a shell when a Pod has more than one Container
137+
## Opening a shell when a Pod has more than one container
131138

132-
If a Pod has more than one Container, use `--container` or `-c` to
133-
specify a Container in the `kubectl exec` command. For example,
139+
If a Pod has more than one container, use `--container` or `-c` to
140+
specify a container in the `kubectl exec` command. For example,
134141
suppose you have a Pod named my-pod, and the Pod has two containers
135-
named main-app and helper-app. The following command would open a
136-
shell to the main-app Container.
142+
named _main-app_ and _helper-app_. The following command would open a
143+
shell to the _main-app_ container.
137144

138145
```shell
139-
kubectl exec -it my-pod --container main-app -- /bin/bash
146+
kubectl exec -i -t my-pod --container main-app -- /bin/bash
140147
```
141148

142-
149+
{{< note >}}
150+
The short options `-i` and `-t` are the same as the long options `--stdin` and `--tty`
151+
{{< /note >}}
143152

144153

145154
## {{% heading "whatsnext" %}}
146155

147156

148-
* [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)
149-
150-
151-
152-
153-
157+
* Read about [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)

0 commit comments

Comments
 (0)