Skip to content

Commit 09f254c

Browse files
authored
Merge pull request #36577 from yanrongshi/zh-cn]Sync-1.25-get-shell-running-container.md
[zh-cn]Sync get-shell-running-container.md
2 parents 9771144 + b18ea1b commit 09f254c

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

content/zh-cn/docs/tasks/debug/debug-application/get-shell-running-container.md

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ content_type: task
77

88
<!--
99
This page shows how to use `kubectl exec` to get a shell to a
10-
running Container.
10+
running container.
1111
-->
1212
本文介绍怎样使用 `kubectl exec` 命令获取正在运行容器的 Shell。
1313

1414
## {{% heading "prerequisites" %}}
1515

16-
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
16+
{{< include "task-tutorial-prereqs.md" >}}
1717

1818
<!-- steps -->
1919

@@ -23,7 +23,7 @@ running Container.
2323
## 获取容器的 Shell
2424

2525
<!--
26-
In this exercise, you create a Pod that has one Container. The Container
26+
In this exercise, you create a Pod that has one container. The container
2727
runs the nginx image. Here is the configuration file for the Pod:
2828
-->
2929
在本练习中,你将创建包含一个容器的 Pod。容器运行 nginx 镜像。下面是 Pod 的配置文件:
@@ -36,11 +36,11 @@ Create the Pod:
3636
创建 Pod:
3737

3838
```shell
39-
kubectl create -f https://k8s.io/examples/application/shell-demo.yaml
39+
kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml
4040
```
4141

4242
<!--
43-
Verify that the Container is running:
43+
Verify that the container is running:
4444
-->
4545
检查容器是否运行正常:
4646

@@ -49,17 +49,18 @@ kubectl get pod shell-demo
4949
```
5050

5151
<!--
52-
Get a shell to the running Container:
52+
Get a shell to the running container:
5353
-->
5454
获取正在运行容器的 Shell:
5555

5656
```shell
57-
kubectl exec -it shell-demo -- /bin/bash
57+
kubectl exec --stdin --tty shell-demo -- /bin/bash
5858
```
59+
5960
{{< note >}}
6061

6162
<!--
62-
The double dash symbol "--" is used to separate the arguments you want to pass to the command from the kubectl arguments.
63+
The double dash (`--`) separates the arguments you want to pass to the command from the kubectl arguments.
6364
-->
6465
双破折号 "--" 用于将要传递给命令的参数与 kubectl 的参数分开。
6566
{{< /note >}}
@@ -70,7 +71,8 @@ In your shell, list the root directory:
7071
在 shell 中,打印根目录:
7172

7273
```shell
73-
root@shell-demo:/# ls /
74+
# 在容器内运行如下命令
75+
ls /
7476
```
7577

7678
<!--
@@ -80,17 +82,18 @@ some examples:
8082
在 shell 中,实验其他命令。下面是一些示例:
8183

8284
```shell
83-
root@shell-demo:/# ls /
84-
root@shell-demo:/# cat /proc/mounts
85-
root@shell-demo:/# cat /proc/1/maps
86-
root@shell-demo:/# apt-get update
87-
root@shell-demo:/# apt-get install -y tcpdump
88-
root@shell-demo:/# tcpdump
89-
root@shell-demo:/# apt-get install -y lsof
90-
root@shell-demo:/# lsof
91-
root@shell-demo:/# apt-get install -y procps
92-
root@shell-demo:/# ps aux
93-
root@shell-demo:/# ps aux | grep nginx
85+
# 你可以在容器中运行这些示例命令
86+
ls /
87+
cat /proc/mounts
88+
cat /proc/1/maps
89+
apt-get update
90+
apt-get install -y tcpdump
91+
tcpdump
92+
apt-get install -y lsof
93+
lsof
94+
apt-get install -y procps
95+
ps aux
96+
ps aux | grep nginx
9497
```
9598

9699
<!--
@@ -100,7 +103,7 @@ root@shell-demo:/# ps aux | grep nginx
100103

101104
<!--
102105
Look again at the configuration file for your Pod. The Pod
103-
has an `emptyDir` volume, and the Container mounts the volume
106+
has an `emptyDir` volume, and the container mounts the volume
104107
at `/usr/share/nginx/html`.
105108
-->
106109
再看一下 Pod 的配置文件。该 Pod 有个 `emptyDir` 卷,容器将该卷挂载到了 `/usr/share/nginx/html`
@@ -112,7 +115,8 @@ directory:
112115
在 shell 中,在 `/usr/share/nginx/html` 目录创建一个 `index.html` 文件:
113116

114117
```shell
115-
root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html
118+
# 在容器内运行如下命令
119+
echo 'Hello shell demo' > /usr/share/nginx/html/index.html
116120
```
117121

118122
<!--
@@ -121,9 +125,10 @@ In your shell, send a GET request to the nginx server:
121125
在 shell 中,向 nginx 服务器发送 GET 请求:
122126

123127
```shell
124-
root@shell-demo:/# apt-get update
125-
root@shell-demo:/# apt-get install curl
126-
root@shell-demo:/# curl localhost
128+
# 在容器内运行如下命令
129+
apt-get update
130+
apt-get install curl
131+
curl http://localhost/
127132
```
128133

129134
<!--
@@ -140,14 +145,18 @@ When you are finished with your shell, enter `exit`.
140145
-->
141146
当用完 shell 后,输入 `exit` 退出。
142147

148+
```shell
149+
exit # 快速退出容器内的 Shell
150+
```
151+
143152
<!--
144153
## Running individual commands in a Container
145154
-->
146155
## 在容器中运行单个命令
147156

148157
<!--
149158
In an ordinary command window, not your shell, list the environment
150-
variables in the running Container:
159+
variables in the running container:
151160
-->
152161
在普通的命令窗口(而不是 shell)中,打印环境运行容器中的变量:
153162

@@ -156,14 +165,14 @@ kubectl exec shell-demo env
156165
```
157166

158167
<!--
159-
Experiment running other commands. Here are some examples:
168+
Experiment with running other commands. Here are some examples:
160169
-->
161170
实验运行其他命令。下面是一些示例:
162171

163172
```shell
164-
kubectl exec shell-demo ps aux
165-
kubectl exec shell-demo ls /
166-
kubectl exec shell-demo cat /proc/1/mounts
173+
kubectl exec shell-demo -- ps aux
174+
kubectl exec shell-demo -- ls /
175+
kubectl exec shell-demo -- cat /proc/1/mounts
167176
```
168177

169178
<!-- discussion -->
@@ -174,20 +183,31 @@ kubectl exec shell-demo cat /proc/1/mounts
174183
## 当 Pod 包含多个容器时打开 shell
175184

176185
<!--
177-
If a Pod has more than one Container, use `--container` or `-c` to
178-
specify a Container in the `kubectl exec` command. For example,
186+
If a Pod has more than one container, use `--container` or `-c` to
187+
specify a container in the `kubectl exec` command. For example,
179188
suppose you have a Pod named my-pod, and the Pod has two containers
180-
named main-app and helper-app. The following command would open a
181-
shell to the main-app Container.
189+
named _main-app_ and _helper-app_. The following command would open a
190+
shell to the _main-app_ container.
182191
-->
183192
如果 Pod 有多个容器,`--container` 或者 `-c` 可以在 `kubectl exec` 命令中指定容器。
184-
例如,你有个名为 my-pod 的 Pod,该 Pod 有两个容器分别为 main-app 和 healper-app。
185-
下面的命令将会打开一个 shell 访问 main-app 容器。
193+
例如,你有个名为 my-pod 的 Pod,该 Pod 有两个容器分别为 **main-app****healper-app**
194+
下面的命令将会打开一个 shell 访问 **main-app** 容器。
186195

187196
```shell
188-
kubectl exec -it my-pod --container main-app -- /bin/bash
197+
kubectl exec -i -t my-pod --container main-app -- /bin/bash
189198
```
190199

200+
{{< note >}}
201+
<!--
202+
The short options `-i` and `-t` are the same as the long options `--stdin` and `--tty`
203+
-->
204+
短的命令参数 `-i``-t` 与长的命令参数 `--stdin``--tty` 作用相同。
205+
{{< /note >}}
206+
191207
## {{% heading "whatsnext" %}}
192208

193-
* [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)
209+
<!--
210+
* Read about [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)
211+
-->
212+
* 阅读 [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)
213+

0 commit comments

Comments
 (0)