Skip to content

Commit afdb056

Browse files
authored
Merge pull request #33332 from kinzhi/kinzhi60
[zh]Add content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md
2 parents 0cc95c6 + aaf7e99 commit afdb056

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
---
2+
title: 获取正在运行容器的 Shell
3+
content_type: task
4+
---
5+
6+
<!-- overview -->
7+
8+
<!--
9+
This page shows how to use `kubectl exec` to get a shell to a
10+
running Container.
11+
-->
12+
本文介绍怎样使用 `kubectl exec` 命令获取正在运行容器的 Shell。
13+
14+
## {{% heading "prerequisites" %}}
15+
16+
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
17+
18+
<!-- steps -->
19+
20+
<!--
21+
## Getting a shell to a Container
22+
-->
23+
24+
## 获取容器的 Shell
25+
26+
<!--
27+
In this exercise, you create a Pod that has one Container. The Container
28+
runs the nginx image. Here is the configuration file for the Pod:
29+
-->
30+
31+
在本练习中,你将创建包含一个容器的 Pod。容器运行 nginx 镜像。下面是 Pod 的配置文件:
32+
33+
{{< codenew file="application/shell-demo.yaml" >}}
34+
35+
<!--
36+
Create the Pod:
37+
-->
38+
39+
创建 Pod:
40+
41+
```shell
42+
kubectl create -f https://k8s.io/examples/application/shell-demo.yaml
43+
```
44+
45+
<!--
46+
Verify that the Container is running:
47+
-->
48+
49+
检查容器是否运行正常:
50+
51+
```shell
52+
kubectl get pod shell-demo
53+
```
54+
55+
<!--
56+
Get a shell to the running Container:
57+
-->
58+
59+
获取正在运行容器的 Shell:
60+
61+
```shell
62+
kubectl exec -it shell-demo -- /bin/bash
63+
```
64+
{{< note >}}
65+
66+
<!--
67+
The double dash symbol "--" is used to separate the arguments you want to pass to the command from the kubectl arguments.
68+
-->
69+
双破折号 "--" 用于将要传递给命令的参数与 kubectl 的参数分开。
70+
{{< /note >}}
71+
72+
<!--
73+
In your shell, list the root directory:
74+
-->
75+
76+
在 shell 中,打印根目录:
77+
78+
```shell
79+
root@shell-demo:/# ls /
80+
```
81+
82+
<!--
83+
In your shell, experiment with other commands. Here are
84+
some examples:
85+
-->
86+
87+
在 shell 中,实验其他命令。下面是一些示例:
88+
89+
```shell
90+
root@shell-demo:/# ls /
91+
root@shell-demo:/# cat /proc/mounts
92+
root@shell-demo:/# cat /proc/1/maps
93+
root@shell-demo:/# apt-get update
94+
root@shell-demo:/# apt-get install -y tcpdump
95+
root@shell-demo:/# tcpdump
96+
root@shell-demo:/# apt-get install -y lsof
97+
root@shell-demo:/# lsof
98+
root@shell-demo:/# apt-get install -y procps
99+
root@shell-demo:/# ps aux
100+
root@shell-demo:/# ps aux | grep nginx
101+
```
102+
103+
<!--
104+
## Writing the root page for nginx
105+
-->
106+
107+
## 编写 nginx 的根页面
108+
109+
<!--
110+
Look again at the configuration file for your Pod. The Pod
111+
has an `emptyDir` volume, and the Container mounts the volume
112+
at `/usr/share/nginx/html`.
113+
-->
114+
115+
在看一下 Pod 的配置文件。该 Pod 有个 `emptyDir` 卷,容器将该卷挂载到了 `/usr/share/nginx/html`
116+
117+
<!--
118+
In your shell, create an `index.html` file in the `/usr/share/nginx/html`
119+
directory:
120+
-->
121+
122+
在 shell 中,在 `/usr/share/nginx/html` 目录创建一个 `index.html` 文件:
123+
124+
```shell
125+
root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html
126+
```
127+
128+
<!--
129+
In your shell, send a GET request to the nginx server:
130+
-->
131+
132+
在 shell 中,向 nginx 服务器发送 GET 请求:
133+
134+
```shell
135+
root@shell-demo:/# apt-get update
136+
root@shell-demo:/# apt-get install curl
137+
root@shell-demo:/# curl localhost
138+
```
139+
140+
<!--
141+
The output shows the text that you wrote to the `index.html` file:
142+
-->
143+
144+
输出结果显示了你在 `index.html` 中写入的文本。
145+
146+
```shell
147+
Hello shell demo
148+
```
149+
150+
<!--
151+
When you are finished with your shell, enter `exit`.
152+
-->
153+
154+
当用完 shell 后,输入 `exit` 退出。
155+
156+
<!--
157+
## Running individual commands in a Container
158+
-->
159+
160+
## 在容器中运行单个命令
161+
162+
<!--
163+
In an ordinary command window, not your shell, list the environment
164+
variables in the running Container:
165+
-->
166+
167+
在普通的命令窗口(而不是 shell)中,打印环境运行容器中的变量:
168+
169+
```shell
170+
kubectl exec shell-demo env
171+
```
172+
173+
<!--
174+
Experiment running other commands. Here are some examples:
175+
-->
176+
177+
实验运行其他命令。下面是一些示例:
178+
179+
```shell
180+
kubectl exec shell-demo ps aux
181+
kubectl exec shell-demo ls /
182+
kubectl exec shell-demo cat /proc/1/mounts
183+
```
184+
185+
<!-- discussion -->
186+
187+
<!--
188+
## Opening a shell when a Pod has more than one Container
189+
-->
190+
191+
## 当 Pod 包含多个容器时打开 shell
192+
193+
<!--
194+
If a Pod has more than one Container, use `--container` or `-c` to
195+
specify a Container in the `kubectl exec` command. For example,
196+
suppose you have a Pod named my-pod, and the Pod has two containers
197+
named main-app and helper-app. The following command would open a
198+
shell to the main-app Container.
199+
-->
200+
201+
如果 Pod 有多个容器,`--container` 或者 `-c` 可以在 `kubectl exec` 命令中指定容器。
202+
例如,您有个名为 my-pod 的容器,该 Pod 有两个容器分别为 main-app 和 healper-app。
203+
下面的命令将会打开一个 shell 访问 main-app 容器。
204+
205+
```shell
206+
kubectl exec -it my-pod --container main-app -- /bin/bash
207+
```
208+
209+
## {{% heading "whatsnext" %}}
210+
211+
* [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)

0 commit comments

Comments
 (0)