From cbc31a51fdad469390f06ee2280010fa0383f0d7 Mon Sep 17 00:00:00 2001 From: guzj11 <67083623+guzj11@users.noreply.github.com> Date: Fri, 18 Dec 2020 10:29:18 +0800 Subject: [PATCH] Update debug-running-pod.md sync with english version Update debug-running-pod.md apply suggestions Update debug-running-pod.md sync the left part Update debug-running-pod.md apply suggestions --- .../debug-running-pod.md | 225 +++++++++++++++++- 1 file changed, 220 insertions(+), 5 deletions(-) diff --git a/content/zh/docs/tasks/debug-application-cluster/debug-running-pod.md b/content/zh/docs/tasks/debug-application-cluster/debug-running-pod.md index 32fe8d87c7..26af9a3c35 100644 --- a/content/zh/docs/tasks/debug-application-cluster/debug-running-pod.md +++ b/content/zh/docs/tasks/debug-application-cluster/debug-running-pod.md @@ -126,6 +126,7 @@ command that can create ephemeral containers for debugging beginning with versio 当由于容器崩溃或容器镜像不包含调试程序(例如[无发行版镜像](https://github.com/GoogleContainerTools/distroless)等) 而导致 `kubectl exec` 无法运行时,{{< glossary_tooltip text="临时容器" term_id="ephemeral-container" >}}对于排除交互式故障很有用。 +从 'v1.18' 版本开始,'kubectl' 有一个可以创建用于调试的临时容器的 alpha 命令。 +## 通过 Pod 副本调试 + + +有些时候 Pod 的配置参数使得在某些情况下很难执行故障排查。 +例如,在容器镜像中不包含 shell 或者你的应用程序在启动时崩溃的情况下, +就不能通过运行 `kubectl exec` 来排查容器故障。 +在这些情况下,你可以使用 `kubectl debug` 来创建 Pod 的副本,通过更改配置帮助调试。 + + +### 在添加新的容器时创建 Pod 副本 + + +当应用程序正在运行但其表现不符合预期时,你会希望在 Pod 中添加额外的调试工具, +这时添加新容器是很有用的。 + + +例如,应用的容器镜像是建立在 `busybox` 的基础上, +但是你需要 `busybox` 中并不包含的调试工具。 +你可以使用 `kubectl run` 模拟这个场景: + +```shell +kubectl run myapp --image=busybox --restart=Never -- sleep 1d +``` + +通过运行以下命令,建立 `myapp` 的一个名为 `myapp-copy` 的副本, +新增了一个用于调试的 Ubuntu 容器, + +```shell +kubectl debug myapp -it --image=ubuntu --share-processes --copy-to=myapp-debug +``` + +``` +Defaulting debug container name to debugger-w7xmf. +If you don't see a command prompt, try pressing enter. +root@myapp-debug:/# +``` + +{{< note >}} +* 如果你没有使用 `--container` 指定新的容器名,`kubectl debug` 会自动生成的。 +* 默认情况下,`-i` 标志使 `kubectl debug` 附加到新容器上。 + 你可以通过指定 `--attach=false` 来防止这种情况。 + 如果你的会话断开连接,你可以使用 `kubectl attach` 重新连接。 +* `--share-processes` 允许在此 Pod 中的其他容器中查看该容器的进程。 + 参阅[在 Pod 中的容器之间共享进程命名空间](/zh/docs/tasks/configure-pod-container/share-process-namespace/) + 获取更多信息。 +{{< /note >}} + + +不要忘了清理调试 Pod: + +```shell +kubectl delete pod myapp myapp-debug +``` + + +### 在改变 Pod 命令时创建 Pod 副本 + + +有时更改容器的命令很有用,例如添加调试标志或因为应用崩溃。 + + +为了模拟应用崩溃的场景,使用 `kubectl run` 命令创建一个立即退出的容器: + +``` +kubectl run --image=busybox myapp -- false +``` + + +使用 `kubectl describe pod myapp` 命令,你可以看到容器崩溃了: + +``` +Containers: + myapp: + Image: busybox + ... + Args: + false + State: Waiting + Reason: CrashLoopBackOff + Last State: Terminated + Reason: Error + Exit Code: 1 +``` + + +你可以使用 `kubectl debug` 命令创建该 Pod 的一个副本, +在该副本中命令改变为交互式 shell: + +``` +kubectl debug myapp -it --copy-to=myapp-debug --container=myapp -- sh +``` + +``` +If you don't see a command prompt, try pressing enter. +/ # +``` + + +现在你有了一个可以执行类似检查文件系统路径或者手动运行容器命令的交互式 shell。 + + +{{< note >}} +* 要更改指定容器的命令,你必须用 `--container` 命令指定容器的名字, + 否则 `kubectl debug` 将建立一个新的容器运行你指定的命令。 +* 默认情况下,标志 `-i` 使 `kubectl debug` 附加到容器。 + 你可通过指定 `--attach=false` 来防止这种情况。 + 如果你的断开连接,可以使用 `kubectl attach` 重新连接。 +{{< /note >}} + + +不要忘了清理调试 Pod: + +```shell +kubectl delete pod myapp myapp-debug +``` + +### 在更改容器镜像时创建 Pod 副本 + +在某些情况下,你可能想从正常生产容器镜像中 +把行为异常的 Pod 改变为包含调试版本或者附加应用的镜像。 + +下面的例子,用 `kubectl run`创建一个 Pod: + +``` +kubectl run myapp --image=busybox --restart=Never -- sleep 1d +``` + +现在可以使用 `kubectl debug` 创建一个副本 +并改变容器镜像为 `ubuntu`: + +``` +kubectl debug myapp --copy-to=myapp-debug --set-image=*=ubuntu +``` + + +`--set-image` 与 `container_name=image` 使用相同的 `kubectl set image` 语法。 +`*=ubuntu` 表示把所有容器的镜像改为 `ubuntu`。 + +```shell +kubectl delete pod myapp myapp-debug +```