@@ -126,6 +126,7 @@ command that can create ephemeral containers for debugging beginning with versio
126
126
127
127
当由于容器崩溃或容器镜像不包含调试程序(例如[ 无发行版镜像] ( https://github.com/GoogleContainerTools/distroless ) 等)
128
128
而导致 ` kubectl exec ` 无法运行时,{{< glossary_tooltip text="临时容器" term_id="ephemeral-container" >}}对于排除交互式故障很有用。
129
+ 从 'v1.18' 版本开始,'kubectl' 有一个可以创建用于调试的临时容器的 alpha 命令。
129
130
130
131
<!--
131
132
### Example debugging using ephemeral containers {#ephemeral-container-example}
@@ -270,12 +271,226 @@ kubectl delete pod ephemeral-demo
270
271
```
271
272
272
273
<!--
273
- Planned future sections include:
274
+ ## Debugging using a copy of the Pod
275
+ -->
276
+ ## 通过 Pod 副本调试
277
+
278
+ <!--
279
+ Sometimes Pod configuration options make it difficult to troubleshoot in certain
280
+ situations. For example, you can't run `kubectl exec` to troubleshoot your
281
+ container if your container image does not include a shell or if your application
282
+ crashes on startup. In these situations you can use `kubectl debug` to create a
283
+ copy of the Pod with configuration values changed to aid debugging.
284
+ -->
285
+ 有些时候 Pod 的配置参数使得在某些情况下很难执行故障排查。
286
+ 例如,在容器镜像中不包含 shell 或者你的应用程序在启动时崩溃的情况下,
287
+ 就不能通过运行 ` kubectl exec ` 来排查容器故障。
288
+ 在这些情况下,你可以使用 ` kubectl debug ` 来创建 Pod 的副本,通过更改配置帮助调试。
289
+
290
+ <!--
291
+ ### Copying a Pod while adding a new container
292
+ -->
293
+ ### 在添加新的容器时创建 Pod 副本
294
+
295
+ <!--
296
+ Adding a new container can be useful when your application is running but not
297
+ behaving as you expect and you'd like to add additional troubleshooting
298
+ utilities to the Pod.
299
+ -->
300
+ 当应用程序正在运行但其表现不符合预期时,你会希望在 Pod 中添加额外的调试工具,
301
+ 这时添加新容器是很有用的。
302
+
303
+ <!--
304
+ For example, maybe your application's container images are built on `busybox`
305
+ but you need debugging utilities not included in `busybox`. You can simulate
306
+ this scenario using `kubectl run`:
307
+ -->
308
+ 例如,应用的容器镜像是建立在 ` busybox ` 的基础上,
309
+ 但是你需要 ` busybox ` 中并不包含的调试工具。
310
+ 你可以使用 ` kubectl run ` 模拟这个场景:
311
+
312
+ ``` shell
313
+ kubectl run myapp --image=busybox --restart=Never -- sleep 1d
314
+ ```
315
+ <!--
316
+ Run this command to create a copy of `myapp` named `myapp-copy` that adds a
317
+ new Ubuntu container for debugging:
318
+ -->
319
+ 通过运行以下命令,建立 ` myapp ` 的一个名为 ` myapp-copy ` 的副本,
320
+ 新增了一个用于调试的 Ubuntu 容器,
321
+
322
+ ``` shell
323
+ kubectl debug myapp -it --image=ubuntu --share-processes --copy-to=myapp-debug
324
+ ```
325
+
326
+ ```
327
+ Defaulting debug container name to debugger-w7xmf.
328
+ If you don't see a command prompt, try pressing enter.
329
+ root@myapp-debug:/#
330
+ ```
331
+ <!--
332
+ {{< note >}}
333
+ * `kubectl debug` automatically generates a container name if you don't choose
334
+ one using the `--container` flag.
335
+ * The `-i` flag causes `kubectl debug` to attach to the new container by
336
+ default. You can prevent this by specifying `--attach=false`. If your session
337
+ becomes disconnected you can reattach using `kubectl attach`.
338
+ * The `--share-processes` allows the containers in this Pod to see processes
339
+ from the other containers in the Pod. For more information about how this
340
+ works, see [Share Process Namespace between Containers in a Pod](
341
+ /docs/tasks/configure-pod-container/share-process-namespace/).
342
+ {{< /note >}}
343
+ -->
344
+ {{< note >}}
345
+ * 如果你没有使用 ` --container ` 指定新的容器名,` kubectl debug ` 会自动生成的。
346
+ * 默认情况下,` -i ` 标志使 ` kubectl debug ` 附加到新容器上。
347
+ 你可以通过指定 ` --attach=false ` 来防止这种情况。
348
+ 如果你的会话断开连接,你可以使用 ` kubectl attach ` 重新连接。
349
+ * ` --share-processes ` 允许在此 Pod 中的其他容器中查看该容器的进程。
350
+ 参阅[ 在 Pod 中的容器之间共享进程命名空间] ( /zh/docs/tasks/configure-pod-container/share-process-namespace/ )
351
+ 获取更多信息。
352
+ {{< /note >}}
274
353
275
- * Debugging with a copy of the pod
354
+ <!--
355
+ Don't forget to clean up the debugging Pod when you're finished with it:
356
+ -->
357
+ 不要忘了清理调试 Pod:
358
+
359
+ ``` shell
360
+ kubectl delete pod myapp myapp-debug
361
+ ```
276
362
277
- See https://git.k8s.io/enhancements/keps/sig-cli/20190805-kubectl-debug.md
363
+ <!--
364
+ ### Copying a Pod while changing its command
278
365
-->
366
+ ### 在改变 Pod 命令时创建 Pod 副本
367
+
368
+ <!--
369
+ Sometimes it's useful to change the command for a container, for example to
370
+ add a debugging flag or because the application is crashing.
371
+ -->
372
+ 有时更改容器的命令很有用,例如添加调试标志或因为应用崩溃。
373
+
374
+ <!--
375
+ To simulate a crashing application, use `kubectl run` to create a container
376
+ that immediately exits:
377
+ -->
378
+ 为了模拟应用崩溃的场景,使用 ` kubectl run ` 命令创建一个立即退出的容器:
379
+
380
+ ```
381
+ kubectl run --image=busybox myapp -- false
382
+ ```
383
+
384
+ <!--
385
+ You can see using `kubectl describe pod myapp` that this container is crashing:
386
+ -->
387
+ 使用 ` kubectl describe pod myapp ` 命令,你可以看到容器崩溃了:
388
+
389
+ ```
390
+ Containers:
391
+ myapp:
392
+ Image: busybox
393
+ ...
394
+ Args:
395
+ false
396
+ State: Waiting
397
+ Reason: CrashLoopBackOff
398
+ Last State: Terminated
399
+ Reason: Error
400
+ Exit Code: 1
401
+ ```
402
+
403
+ <!--
404
+ You can use `kubectl debug` to create a copy of this Pod with the command
405
+ changed to an interactive shell:
406
+ -->
407
+ 你可以使用 ` kubectl debug ` 命令创建该 Pod 的一个副本,
408
+ 在该副本中命令改变为交互式 shell:
409
+
410
+ ```
411
+ kubectl debug myapp -it --copy-to=myapp-debug --container=myapp -- sh
412
+ ```
413
+
414
+ ```
415
+ If you don't see a command prompt, try pressing enter.
416
+ / #
417
+ ```
418
+
419
+ <!--
420
+ Now you have an interactive shell that you can use to perform tasks like
421
+ checking filesystem paths or running the container command manually.
422
+ -->
423
+ 现在你有了一个可以执行类似检查文件系统路径或者手动运行容器命令的交互式 shell。
424
+
425
+ <!--
426
+ {{< note >}}
427
+ * To change the command of a specific container you must
428
+ specify its name using `--container` or `kubectl debug` will instead
429
+ create a new container to run the command you specified.
430
+ * The `-i` flag causes `kubectl debug` to attach to the container by default.
431
+ You can prevent this by specifying `--attach=false`. If your session becomes
432
+ disconnected you can reattach using `kubectl attach`.
433
+ {{< /note >}}
434
+ -->
435
+ {{< note >}}
436
+ * 要更改指定容器的命令,你必须用 ` --container ` 命令指定容器的名字,
437
+ 否则 ` kubectl debug ` 将建立一个新的容器运行你指定的命令。
438
+ * 默认情况下,标志 ` -i ` 使 ` kubectl debug ` 附加到容器。
439
+ 你可通过指定 ` --attach=false ` 来防止这种情况。
440
+ 如果你的断开连接,可以使用 ` kubectl attach ` 重新连接。
441
+ {{< /note >}}
442
+
443
+ <!--
444
+ Don't forget to clean up the debugging Pod when you're finished with it:
445
+ -->
446
+ 不要忘了清理调试 Pod:
447
+
448
+ ``` shell
449
+ kubectl delete pod myapp myapp-debug
450
+ ```
451
+ <!--
452
+ ### Copying a Pod while changing container images
453
+
454
+ In some situations you may want to change a misbehaving Pod from its normal
455
+ production container images to an image containing a debugging build or
456
+ additional utilities.
457
+
458
+ As an example, create a Pod using `kubectl run`:
459
+ -->
460
+ ### 在更改容器镜像时创建 Pod 副本
461
+
462
+ 在某些情况下,你可能想从正常生产容器镜像中
463
+ 把行为异常的 Pod 改变为包含调试版本或者附加应用的镜像。
464
+
465
+ 下面的例子,用 ` kubectl run ` 创建一个 Pod:
466
+
467
+ ```
468
+ kubectl run myapp --image=busybox --restart=Never -- sleep 1d
469
+ ```
470
+ <!--
471
+ Now use `kubectl debug` to make a copy and change its container image
472
+ to `ubuntu`:
473
+ -->
474
+ 现在可以使用 ` kubectl debug ` 创建一个副本
475
+ 并改变容器镜像为 ` ubuntu ` :
476
+
477
+ ```
478
+ kubectl debug myapp --copy-to=myapp-debug --set-image=*=ubuntu
479
+ ```
480
+
481
+ <!--
482
+ The syntax of `--set-image` uses the same `container_name=image` syntax as
483
+ `kubectl set image`. `*=ubuntu` means change the image of all containers
484
+ to `ubuntu`.
485
+
486
+ Don't forget to clean up the debugging Pod when you're finished with it:
487
+ -->
488
+ ` --set-image ` 与 ` container_name=image ` 使用相同的 ` kubectl set image ` 语法。
489
+ ` *=ubuntu ` 表示把所有容器的镜像改为 ` ubuntu ` 。
490
+
491
+ ``` shell
492
+ kubectl delete pod myapp myapp-debug
493
+ ```
279
494
280
495
<!--
281
496
## Debugging via a shell on the node {#node-shell-session}
0 commit comments