@@ -7,13 +7,13 @@ content_type: task
7
7
8
8
<!--
9
9
This page shows how to use `kubectl exec` to get a shell to a
10
- running Container .
10
+ running container .
11
11
-->
12
12
本文介绍怎样使用 ` kubectl exec ` 命令获取正在运行容器的 Shell。
13
13
14
14
## {{% heading "prerequisites" %}}
15
15
16
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
16
+ {{< include "task-tutorial-prereqs.md" >}}
17
17
18
18
<!-- steps -->
19
19
@@ -23,7 +23,7 @@ running Container.
23
23
## 获取容器的 Shell
24
24
25
25
<!--
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
27
27
runs the nginx image. Here is the configuration file for the Pod:
28
28
-->
29
29
在本练习中,你将创建包含一个容器的 Pod。容器运行 nginx 镜像。下面是 Pod 的配置文件:
@@ -36,11 +36,11 @@ Create the Pod:
36
36
创建 Pod:
37
37
38
38
``` 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
40
40
```
41
41
42
42
<!--
43
- Verify that the Container is running:
43
+ Verify that the container is running:
44
44
-->
45
45
检查容器是否运行正常:
46
46
@@ -49,17 +49,18 @@ kubectl get pod shell-demo
49
49
```
50
50
51
51
<!--
52
- Get a shell to the running Container :
52
+ Get a shell to the running container :
53
53
-->
54
54
获取正在运行容器的 Shell:
55
55
56
56
``` shell
57
- kubectl exec -it shell-demo -- /bin/bash
57
+ kubectl exec --stdin --tty shell-demo -- /bin/bash
58
58
```
59
+
59
60
{{< note >}}
60
61
61
62
<!--
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.
63
64
-->
64
65
双破折号 "--" 用于将要传递给命令的参数与 kubectl 的参数分开。
65
66
{{< /note >}}
@@ -70,7 +71,8 @@ In your shell, list the root directory:
70
71
在 shell 中,打印根目录:
71
72
72
73
``` shell
73
- root@shell-demo:/# ls /
74
+ # 在容器内运行如下命令
75
+ ls /
74
76
```
75
77
76
78
<!--
@@ -80,17 +82,18 @@ some examples:
80
82
在 shell 中,实验其他命令。下面是一些示例:
81
83
82
84
``` 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
94
97
```
95
98
96
99
<!--
@@ -100,7 +103,7 @@ root@shell-demo:/# ps aux | grep nginx
100
103
101
104
<!--
102
105
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
104
107
at `/usr/share/nginx/html`.
105
108
-->
106
109
再看一下 Pod 的配置文件。该 Pod 有个 ` emptyDir ` 卷,容器将该卷挂载到了 ` /usr/share/nginx/html ` 。
@@ -112,7 +115,8 @@ directory:
112
115
在 shell 中,在 ` /usr/share/nginx/html ` 目录创建一个 ` index.html ` 文件:
113
116
114
117
``` 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
116
120
```
117
121
118
122
<!--
@@ -121,9 +125,10 @@ In your shell, send a GET request to the nginx server:
121
125
在 shell 中,向 nginx 服务器发送 GET 请求:
122
126
123
127
``` 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/
127
132
```
128
133
129
134
<!--
@@ -140,14 +145,18 @@ When you are finished with your shell, enter `exit`.
140
145
-->
141
146
当用完 shell 后,输入 ` exit ` 退出。
142
147
148
+ ``` shell
149
+ exit # 快速退出容器内的 Shell
150
+ ```
151
+
143
152
<!--
144
153
## Running individual commands in a Container
145
154
-->
146
155
## 在容器中运行单个命令
147
156
148
157
<!--
149
158
In an ordinary command window, not your shell, list the environment
150
- variables in the running Container :
159
+ variables in the running container :
151
160
-->
152
161
在普通的命令窗口(而不是 shell)中,打印环境运行容器中的变量:
153
162
@@ -156,14 +165,14 @@ kubectl exec shell-demo env
156
165
```
157
166
158
167
<!--
159
- Experiment running other commands. Here are some examples:
168
+ Experiment with running other commands. Here are some examples:
160
169
-->
161
170
实验运行其他命令。下面是一些示例:
162
171
163
172
``` 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
167
176
```
168
177
169
178
<!-- discussion -->
@@ -174,20 +183,31 @@ kubectl exec shell-demo cat /proc/1/mounts
174
183
## 当 Pod 包含多个容器时打开 shell
175
184
176
185
<!--
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,
179
188
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 .
182
191
-->
183
192
如果 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** 容器。
186
195
187
196
``` shell
188
- kubectl exec -it my-pod --container main-app -- /bin/bash
197
+ kubectl exec -i -t my-pod --container main-app -- /bin/bash
189
198
```
190
199
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
+
191
207
## {{% heading "whatsnext" %}}
192
208
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