2
2
title : 调试运行中的 Pod
3
3
content_type : task
4
4
---
5
- <!--
6
- reviewers:
7
- - verb
8
- - soltysh
9
- title: Debug Running Pods
10
- content_type: task
11
- -->
12
5
13
6
<!-- overview -->
14
-
15
- <!--
7
+ <!--
16
8
This page explains how to debug Pods running (or crashing) on a Node.
17
9
-->
18
- 本页解释了如何调试节点上正在运行(或者正在崩溃)的 Pod。
19
-
20
-
10
+ 本页解释如何在节点上调试运行中(或崩溃)的 Pod。
21
11
22
12
## {{% heading "prerequisites" %}}
23
13
24
-
25
- <!--
14
+ <!--
26
15
* Your {{< glossary_tooltip text="Pod" term_id="pod" >}} should already be
27
16
scheduled and running. If your Pod is not yet running, start with [Troubleshoot
28
17
Applications](/docs/tasks/debug-application-cluster/debug-application/).
29
18
* For some of the advanced debugging steps you need to know on which Node the
30
19
Pod is running and have shell access to run commands on that Node. You don't
31
20
need that access to run the standard debug steps that use `kubectl`.
32
21
-->
33
- * 你的 {{< glossary_tooltip text="Pod" term_id="pod" >}}
34
- 应该已经调度并正在运行。
35
- 如果 Pod 尚未运行,则从
36
- [ 应用故障排查] ( /zh/docs/tasks/debug-application-cluster/debug-application/ ) 开始。
37
- * 对于一些高级调试步骤,
38
- 你需要知道 Pod 在哪个节点上运行,并拥有在该节点上执行 shell 命令的权限。
39
- 在使用 ` kubectl ` 运行标准调试步骤时,则不需要这种权限。
40
-
22
+ * 你的 {{< glossary_tooltip text="Pod" term_id="pod" >}} 应该已经被调度并正在运行中,
23
+ 如果你的 Pod 还没有运行,请参阅[ 应用问题排查] ( /docs/tasks/debug-application-cluster/debug-application/ ) 。
41
24
25
+ * 对于一些高级调试步骤,你应该知道 Pod 具体运行在哪个节点上,在该节点上有权限去运行一些命令。
26
+ 你不需要任何访问权限就可以使用 ` kubectl ` 去运行一些标准调试步骤。
42
27
43
28
<!-- steps -->
44
29
45
30
<!--
46
31
## Examining pod logs {#examine-pod-logs}
47
32
48
33
First, look at the logs of the affected container:
49
- -->
50
- ## 检查 Pod 日志 {#examine-pod-logs}
51
-
52
- 首先,查看受影响的容器的日志:
53
34
54
35
```shell
55
36
kubectl logs ${POD_NAME} ${CONTAINER_NAME}
56
37
```
57
38
58
- <!--
59
39
If your container has previously crashed, you can access the previous container's crash log with:
60
- -->
61
- 如果你的容器以前崩溃过,你可以访问前一个容器的崩溃日志:
62
40
63
41
```shell
64
42
kubectl logs --previous ${POD_NAME} ${CONTAINER_NAME}
65
43
```
44
+ -->
45
+ ## 检查 Pod 的日志 {#examine-pod-logs}
66
46
67
- <!--
68
- ## Debugging with container exec {#container-exec}
47
+ 首先,查看受到影响的容器的日志:
69
48
70
- If the {{< glossary_tooltip text="container image" term_id="image" >}} includes
71
- debugging utilities, as is the case with images built from Linux and Windows OS
72
- base images, you can run commands inside a specific container with
73
- `kubectl exec`:
74
- -->
75
- ## 使用容器 exec 调试 {#container-exec}
49
+ ``` shell
50
+ kubectl logs ${POD_NAME} ${CONTAINER_NAME}
51
+ ```
76
52
77
- 如果 {{< glossary_tooltip text="容器镜像" term_id="image" >}}
78
- 包含调试工具,就像基于 Linux 和 Windows 基础镜像构建的镜像一样,
79
- 你可以使用 ` kubectl exec ` 在特定的容器中执行命令:
53
+ 如果你的容器之前崩溃过,你可以通过下面命令访问之前容器的崩溃日志:
80
54
81
55
``` shell
82
- kubectl exec ${POD_NAME} -c ${CONTAINER_NAME} -- ${CMD } ${ARG1} ${ARG2} ... ${ARGN }
56
+ kubectl logs --previous ${POD_NAME } ${CONTAINER_NAME }
83
57
```
84
58
85
59
<!--
86
- `-c ${CONTAINER_NAME}` is optional. You can omit it for Pods that only contain a single container.
87
- -->
88
- {{< note >}}
89
- ` -c ${CONTAINER_NAME} ` 是可选项。
90
- 对于单容器 Pod,可以省略此参数。
91
- {{< /note >}}
60
+ ## Debugging with container exec {#container-exec}
61
+
62
+ ```shell
63
+ kubectl exec ${POD_NAME} -c ${CONTAINER_NAME} -- ${CMD} ${ARG1} ${ARG2} ... ${ARGN}
64
+ ```
92
65
93
- <!--
94
66
As an example, to look at the logs from a running Cassandra pod, you might run
95
- -->
96
- 例如,要查看正在运行的 Cassandra Pod 的日志,可以执行:
97
67
98
68
```shell
99
69
kubectl exec cassandra -- cat /var/log/cassandra/system.log
100
70
```
101
71
102
- <!--
103
72
You can run a shell that's connected to your terminal using the `-i` and `-t`
104
73
arguments to `kubectl exec`, for example:
105
- -->
106
- 你可以使用 ` kubectl exec ` 的 ` -i ` 和 ` -t ` 参数启动一个连接到终端的 shell,例如:
107
74
108
75
```shell
109
76
kubectl exec -it cassandra -- sh
110
77
```
111
78
112
- <!--
113
79
For more details, see [Get a Shell to a Running Container](
114
80
/docs/tasks/debug-application-cluster/get-shell-running-container/).
115
81
-->
116
- 更多细节,参见
117
- [ 获取运行容器的 Shell] (
118
- /zh/docs/tasks/debug-application-cluster/get-shell-running-container/)。
82
+ ## 使用容器 exec 进行调试 {#container-exec}
83
+
84
+ 如果 {{< glossary_tooltip text="容器镜像" term_id="image" >}} 包含调试程序,
85
+ 比如从 Linux 和 Windows 操作系统基础镜像构建的镜像,你可以使用 ` kubectl exec ` 命令
86
+ 在特定的容器中运行一些命令:
87
+
88
+ ``` shell
89
+ kubectl exec ${POD_NAME} -c ${CONTAINER_NAME} -- ${CMD} ${ARG1} ${ARG2} ... ${ARGN}
90
+ ```
91
+ {{< note >}}
92
+ ` -c ${CONTAINER_NAME} ` 是可选择的。如果Pod中仅包含一个容器,就可以忽略它。
93
+ {{< /note >}}
94
+
95
+ 例如,要查看正在运行的 Cassandra pod中的日志,可以运行:
96
+
97
+ ``` shell
98
+ kubectl exec cassandra -- cat /var/log/cassandra/system.log
99
+ ```
100
+
101
+ 你可以在 ` kubectl exec ` 命令后面加上 ` -i ` 和 ` -t ` 来运行一个连接到你的终端的 Shell,比如:
102
+
103
+ ``` shell
104
+ kubectl exec -it cassandra -- sh
105
+ ```
106
+
107
+ 若要了解更多内容,可查看[ 获取正在运行容器的 Shell] ( /zh/docs/tasks/debug-application-cluster/get-shell-running-container/ ) 。
119
108
120
109
<!--
121
110
## Debugging with an ephemeral debug container {#ephemeral-container}
122
- -->
123
- ## 使用临时调试容器进行调试 {#ephemeral-container}
124
111
125
112
{{< feature-state state="alpha" for_k8s_version="v1.18" >}}
126
113
127
- <!--
128
114
{{< glossary_tooltip text="Ephemeral containers" term_id="ephemeral-container" >}}
129
115
are useful for interactive troubleshooting when `kubectl exec` is insufficient
130
116
because a container has crashed or a container image doesn't include debugging
@@ -133,58 +119,52 @@ https://github.com/GoogleContainerTools/distroless). `kubectl` has an alpha
133
119
command that can create ephemeral containers for debugging beginning with version
134
120
`v1.18`.
135
121
-->
136
- 因为容器已经崩溃,或因为容器镜像没有内含调试工具,比如
137
- [ distroless images ] ( https://github.com/GoogleContainerTools/distroless ) ,
138
- 导致 ` kubectl exec ` 不足以解决问题时,
139
- {{< glossary_tooltip text="Ephemeral containers" term_id="ephemeral-container" >}}
140
- 对交互式故障诊断非常有用。
141
- 从 ` v1.18 ` 开始, ` kubectl ` 提供 alpha 命令,它可以为调试创建临时容器 。
122
+ ## 使用临时调试容器来进行调试 {#ephemeral-container}
123
+
124
+ {{< feature-state state="alpha" for_k8s_version="v1.18" >}}
125
+
126
+ 当由于容器崩溃或容器镜像不包含调试程序(例如 [ 无发行版镜像 ] ( https://github.com/GoogleContainerTools/distroless ) 等)
127
+ 而导致 ` kubectl exec ` 无法运行时,{{< glossary_tooltip text="临时容器" term_id="ephemeral-container" >}}对于排除交互式故障很有用 。
142
128
143
129
<!--
144
130
### Example debugging using ephemeral containers {#ephemeral-container-example}
145
131
146
132
The examples in this section require the `EphemeralContainers` [feature gate](
147
133
/docs/reference/command-line-tools-reference/feature-gates/) enabled in your
148
134
cluster and `kubectl` version v1.18 or later.
149
- -->
150
- ## 示例:使用临时容器调试 {#ephemeral-container-example}
151
-
152
- {{< note >}}
153
- 本节中的示例要求在集群启用 ` EphemeralContainers ` [ 特性门控] (
154
- /zh/docs/reference/command-line-tools-reference/feature-gates/
155
- )。
156
- 并且要求 ` kubectl ` v1.18 或更高版本。
157
- {{< /note >}}
158
-
159
135
160
- <!--
161
136
You can use the `kubectl alpha debug` command to add ephemeral containers to a
162
137
running Pod. First, create a pod for the example:
163
- -->
164
- 可以使用 ` kubectl alpha debug ` 命令将临时容器添加到正在运行的 Pod 中。
165
- 首先,为本例创建一个 Pod:
166
138
167
139
```shell
168
140
kubectl run ephemeral-demo --image=k8s.gcr.io/pause:3.1 --restart=Never
169
141
```
170
142
171
- <!--
172
143
This section use the `pause` container image in examples because it does not
173
144
contain userland debugging utilities, but this method works with all container
174
145
images.
175
-
176
- If you attempt to use `kubectl exec` to create a shell you will see an error
177
- because there is no shell in this container image.
178
146
-->
147
+ ## 使用临时容器来调试的例子 {#ephemeral-container-example}
148
+
179
149
{{< note >}}
180
- 本节在示例中使用 ` pause ` 容器镜像,
181
- 是因为它不包含用户态的调试工具。
182
- 但此方法适用于所有容器镜像。
150
+ 本示例需要你的集群已经开启 ` EphemeralContainers ` [ 特性门控] ( /zh/docs/reference/command-line-tools-reference/feature-gates/ ) ,
151
+ ` kubectl ` 版本为 v1.18 或者更高。
183
152
{{< /note >}}
184
153
185
- 如果你试图使用 ` kubectl exec ` 去建立一个 shell,
186
- 你会看到一个报错,
187
- 这是因为在容器镜像中并没有包含 shell。
154
+ 你可以使用 ` kubectl alpha debug ` 命令来给正在运行中的 Pod 增加一个临时容器。
155
+ 首先,像示例一样创建一个 pod:
156
+
157
+ ``` shell
158
+ kubectl run ephemeral-demo --image=k8s.gcr.io/pause:3.1 --restart=Never
159
+ ```
160
+
161
+ {{< note >}}
162
+ 本节示例中使用 ` pause ` 容器镜像,因为它不包含任何用户级调试程序,但是这个方法适用于所有容器镜像。
163
+ {{< /note >}}
164
+
165
+ <!--
166
+ If you attempt to use `kubectl exec` to create a shell you will see an error
167
+ because there is no shell in this container image.
188
168
189
169
```shell
190
170
kubectl exec -it ephemeral-demo -- sh
@@ -194,14 +174,32 @@ kubectl exec -it ephemeral-demo -- sh
194
174
OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"sh\": executable file not found in $PATH": unknown
195
175
```
196
176
197
- <!--
198
177
You can instead add a debugging container using `kubectl alpha debug`. If you
199
178
specify the `-i`/`--interactive` argument, `kubectl` will automatically attach
200
179
to the console of the Ephemeral Container.
180
+
181
+ ```shell
182
+ kubectl alpha debug -it ephemeral-demo --image=busybox --target=ephemeral-demo
183
+ ```
184
+
185
+ ```
186
+ Defaulting debug container name to debugger-8xzrl.
187
+ If you don't see a command prompt, try pressing enter.
188
+ / #
189
+ ```
201
190
-->
202
- 你可以使用 ` kubectl alpha debug ` 添加一个调试容器。
203
- 如果指定了 ` -i ` /` --interactive ` 参数,
204
- ` kubectl ` 将自动连接到临时容器的控制台。
191
+ 如果你尝试使用 ` kubectl exec ` 来创建一个 shell,你将会看到一个错误,因为这个容器镜像中没有 shell。
192
+
193
+ ``` shell
194
+ kubectl exec -it ephemeral-demo -- sh
195
+ ```
196
+
197
+ ```
198
+ OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"sh\": executable file not found in $PATH": unknown
199
+ ```
200
+
201
+ 你可以改为使用 ` kubectl alpha debug ` 添加调试容器。
202
+ 如果你指定 ` -i ` 或者 ` --interactive ` 参数,` kubectl ` 将自动挂接到临时容器的控制台。
205
203
206
204
``` shell
207
205
kubectl alpha debug -it ephemeral-demo --image=busybox --target=ephemeral-demo
@@ -227,19 +225,16 @@ isolated process namespace.
227
225
228
226
You can view the state of the newly created ephemeral container using `kubectl describe`:
229
227
-->
230
- 此命令添加一个新的 busybox 容器并连接。
231
- ` --target ` 参数指定了另一个容器的进程命名空间。
232
- 这里必须这样做,因为 ` kubectl run ` 没有在它创建的 Pod 中启用
233
- [ 进程命名空间共享] ( /zh/docs/tasks/configure-pod-container/share-process-namespace/ ) 。
228
+ 此命令添加一个新的 busybox 容器并将其挂接到该容器。` --target ` 参数指定另一个容器的进程命名空间。
229
+ 这是必需的,因为 ` kubectl run ` 不能在它创建的pod中启用
230
+ [ 共享进程命名空间] ( /zh/docs/tasks/configure-pod-container/share-process-namespace/ ) 。
234
231
235
232
{{< note >}}
236
- {{< glossary_tooltip text="Container Runtime" term_id="container-runtime" >}}
237
- 必须支持 ` --target ` 参数。
238
- 如果不支持,临时容器可能无法启动,
239
- 或者可能使用隔离的进程名称空间启动。
233
+ {{< glossary_tooltip text="容器运行时" term_id="container-runtime" >}}必须支持` --target ` 参数。
234
+ 如果不支持,则临时容器可能不会启动,或者可能使用隔离的进程命名空间启动。
240
235
{{< /note >}}
241
236
242
- 可以使用 ` kubectl describe ` 查看新创建的临时容器的状态:
237
+ 你可以使用 ` kubectl describe ` 查看新创建的临时容器的状态:
243
238
244
239
``` shell
245
240
kubectl describe pod ephemeral-demo
@@ -266,7 +261,7 @@ Ephemeral Containers:
266
261
<!--
267
262
Use `kubectl delete` to remove the Pod when you're finished:
268
263
-->
269
- 完成后, 使用 ` kubectl delete ` 删除 Pod:
264
+ 使用 ` kubectl delete ` 来移除已经结束掉的 Pod:
270
265
271
266
``` shell
272
267
kubectl delete pod ephemeral-demo
@@ -289,10 +284,9 @@ given tools in the Kubernetes API. Therefore, if you find yourself needing to
289
284
ssh into a machine, please file a feature request on GitHub describing your use
290
285
case and why these tools are insufficient.
291
286
-->
292
- ## 通过节点上的 shell 进行调试 {#node-shell-session}
293
-
294
- 如果这些方法都不起作用,
295
- 你可以找到运行 Pod 的主机并通过 SSH 连接到该主机,
296
- 但是 Kubernetes API 中的工具通常不需要这样做。
297
- 因此,如果你发现自己需要 ssh 到一台机器上,请在 GitHub 上提交一个功能请求,描述你的用例以及为什么这些工具不够用。
287
+ ## 在节点上通过 shell 来调试 {#node-shell-session}
298
288
289
+ 如果这些方法都不起作用,你可以找到运行 Pod 的主机并通过 SSH 进入该主机,
290
+ 但是如果使用 Kubernetes API 中的工具,则通常不需要这样做。
291
+ 因此,如果你发现自己需要使用 ssh 进入主机,请在GitHub 上提交功能请求,
292
+ 以描述你的用例以及这些工具不足的原因。
0 commit comments