From 8a74686dd3cc5b9f39f5e13c484751c1bbdf91b0 Mon Sep 17 00:00:00 2001 From: windsonsea Date: Tue, 2 Jul 2024 09:20:13 +0800 Subject: [PATCH] [zh] Sync debug-application/debug-running-pod.md --- .../debug-application/debug-running-pod.md | 203 +++++++++++++----- 1 file changed, 155 insertions(+), 48 deletions(-) diff --git a/content/zh-cn/docs/tasks/debug/debug-application/debug-running-pod.md b/content/zh-cn/docs/tasks/debug/debug-application/debug-running-pod.md index 0336ac3730..e2fc046211 100644 --- a/content/zh-cn/docs/tasks/debug/debug-application/debug-running-pod.md +++ b/content/zh-cn/docs/tasks/debug/debug-application/debug-running-pod.md @@ -2,7 +2,6 @@ title: 调试运行中的 Pod content_type: task --- - ## 检查 Pod 的日志 {#examine-pod-logs} @@ -448,6 +437,9 @@ kubectl logs --previous ${POD_NAME} ${CONTAINER_NAME} kubectl logs ${POD_NAME} ${CONTAINER_NAME} ``` + 如果你的容器之前崩溃过,你可以通过下面命令访问之前容器的崩溃日志: ```shell @@ -461,28 +453,6 @@ If the {{< glossary_tooltip text="container image" term_id="image" >}} includes debugging utilities, as is the case with images built from Linux and Windows OS base images, you can run commands inside a specific container with `kubectl exec`: - -```shell -kubectl exec ${POD_NAME} -c ${CONTAINER_NAME} -- ${CMD} ${ARG1} ${ARG2} ... ${ARGN} -``` - -`-c ${CONTAINER_NAME}` is optional. You can omit it for Pods that only contain a single container. - -As an example, to look at the logs from a running Cassandra pod, you might run - -```shell -kubectl exec cassandra -- cat /var/log/cassandra/system.log -``` - -You can run a shell that's connected to your terminal using the `-i` and `-t` -arguments to `kubectl exec`, for example: - -```shell -kubectl exec -it cassandra -- sh -``` - -For more details, see [Get a Shell to a Running Container]( -/docs/tasks/debug/debug-application/get-shell-running-container/). --> ## 使用容器 exec 进行调试 {#container-exec} @@ -495,21 +465,35 @@ kubectl exec ${POD_NAME} -c ${CONTAINER_NAME} -- ${CMD} ${ARG1} ${ARG2} ... ${AR ``` {{< note >}} + `-c ${CONTAINER_NAME}` 是可选择的。如果 Pod 中仅包含一个容器,就可以忽略它。 {{< /note >}} + 例如,要查看正在运行的 Cassandra Pod 中的日志,可以运行: ```shell kubectl exec cassandra -- cat /var/log/cassandra/system.log ``` + 你可以在 `kubectl exec` 命令后面加上 `-i` 和 `-t` 来运行一个连接到你的终端的 Shell,比如: ```shell kubectl exec -it cassandra -- sh ``` + 若要了解更多内容,可查看[获取正在运行容器的 Shell](/zh-cn/docs/tasks/debug/debug-application/get-shell-running-container/)。 此命令添加一个新的 busybox 容器并将其挂接到该容器。`--target` 参数指定另一个容器的进程命名空间。 这个指定进程命名空间的操作是必需的,因为 `kubectl run` 不能在它创建的 Pod 中启用[共享进程命名空间](/zh-cn/docs/tasks/configure-pod-container/share-process-namespace/)。 {{< note >}} + {{< glossary_tooltip text="容器运行时" term_id="container-runtime" >}}必须支持 `--target` 参数。 如果不支持,则临时容器可能不会启动,或者可能使用隔离的进程命名空间启动, 导致 `ps` 不显示其他容器内的进程。 {{< /note >}} + 你可以使用 `kubectl describe` 查看新创建的临时容器的状态: ```shell @@ -701,6 +687,7 @@ If you don't see a command prompt, try pressing enter. root@myapp-debug:/# ``` +{{< note >}} -{{< note >}} * 如果你没有使用 `--container` 指定新的容器名,`kubectl debug` 会自动生成的。 * 默认情况下,`-i` 标志使 `kubectl debug` 附加到新容器上。 你可以通过指定 `--attach=false` 来防止这种情况。 @@ -791,8 +777,9 @@ If you don't see a command prompt, try pressing enter. Now you have an interactive shell that you can use to perform tasks like checking filesystem paths or running the container command manually. --> -现在你有了一个可以执行类似检查文件系统路径或者手动运行容器命令的交互式 shell。 +现在你有了一个可以执行类似检查文件系统路径或者手动运行容器命令的交互式 shell。 +{{< note >}} -{{< note >}} * 要更改指定容器的命令,你必须用 `--container` 命令指定容器的名字, 否则 `kubectl debug` 将建立一个新的容器运行你指定的命令。 * 默认情况下,标志 `-i` 使 `kubectl debug` 附加到容器。 @@ -833,7 +819,7 @@ As an example, create a Pod using `kubectl run`: 下面的例子,用 `kubectl run` 创建一个 Pod: -``` +```shell kubectl run myapp --image=busybox:1.28 --restart=Never -- sleep 1d ``` @@ -843,7 +829,7 @@ to `ubuntu`: --> 现在可以使用 `kubectl debug` 创建一个拷贝并将其容器镜像更改为 `ubuntu`: -``` +```shell kubectl debug myapp --copy-to=myapp-debug --set-image=*=ubuntu ``` @@ -892,7 +878,7 @@ When creating a debugging session on a node, keep in mind that: * The container runs in the host IPC, Network, and PID namespaces, although the pod isn't privileged, so reading some process information may fail, and `chroot /host` may fail. -* If you need a privileged pod, create it manually. +* If you need a privileged pod, create it manually or use the `--profile=sysadmin` flag. Don't forget to clean up the debugging Pod when you're finished with it: --> @@ -901,10 +887,131 @@ Don't forget to clean up the debugging Pod when you're finished with it: * 节点的根文件系统会被挂载在 `/host`。 * 新的调试容器运行在主机 IPC 名字空间、主机网络名字空间以及主机 PID 名字空间内, Pod 没有特权,因此读取某些进程信息可能会失败,并且 `chroot /host` 也可能会失败。 -* 如果你需要一个特权 Pod,需要手动创建。 +* 如果你需要一个特权 Pod,需要手动创建或使用 `--profile=sysadmin` 标志。 当你完成节点调试时,不要忘记清理调试 Pod: ```shell kubectl delete pod node-debugger-mynode-pdx84 ``` + + +## 调试配置 {#debugging-profiles} + +使用 `kubectl debug` 通过调试 Pod 来调试节点、通过临时容器来调试 Pod 或者调试复制的 Pod 时, +你可以使用 `--profile` 标志为其应用调试配置(Debugging Profile)。通过应用配置,可以设置特定的属性(如 +[securityContext](/zh-cn/docs/tasks/configure-pod-container/security-context/)), +以适应各种场景。 + +可用的配置如下: + + +| 配置 | 描述 | +| ----------- | --------------------------------------------------------- | +| legacy | 一组与 1.22 行为向后兼容的属性 | +| general | 一组对大多数调试过程而言均合理的通用属性 | +| baseline | 一组与 [PodSecurityStandard Baseline 策略](/zh-cn/docs/concepts/security/pod-security-standards/#baseline)兼容的属性 | +| restricted | 一组与 [PodSecurityStandard Restricted 策略](/zh-cn/docs/concepts/security/pod-security-standards/#restricted)兼容的属性 | +| netadmin | 一组包含网络管理员特权的属性 | +| sysadmin | 一组包含系统管理员(root)特权的属性 | + + +{{< note >}} + +如果你不指定 `--profile`,`legacy` 配置被默认使用,但此配置计划在不久的将来弃用。 +因此建议使用 `general` 等其他配置。 +{{< /note >}} + + +假设你要创建一个 Pod 并进行调试。 +先创建一个名为 `myapp` 的 Pod 作为示例: + +```shell +kubectl run myapp --image=busybox:1.28 --restart=Never -- sleep 1d +``` + + +然后,使用临时容器调试 Pod。 +如果临时容器需要具有特权,你可以使用 `sysadmin` 配置: + +```shell +kubectl debug -it myapp --image=busybox:1.28 --target=myapp --profile=sysadmin +``` + +``` +Targeting container "myapp". If you don't see processes from this container it may be because the container runtime doesn't support this feature. +Defaulting debug container name to debugger-6kg4x. +If you don't see a command prompt, try pressing enter. +/ # +``` + + +通过在容器内运行以下命令检查临时容器进程的权能: + +```shell +/ # grep Cap /proc/$$/status +``` + +``` +... +CapPrm: 000001ffffffffff +CapEff: 000001ffffffffff +... +``` + + +这意味着通过应用 `sysadmin` 配置,容器进程被授予了作为特权容器的全部权能。 +更多细节参见[权能](/zh-cn/docs/tasks/configure-pod-container/security-context/#set-capabilities-for-a-container)。 + +你还可以检查临时容器是否被创建为特权容器: + +```shell +kubectl get pod myapp -o jsonpath='{.spec.ephemeralContainers[0].securityContext}' +``` + +``` +{"privileged":true} +``` + + +你在完成上述操作后,可运行以下命令清理 Pod: + +```shell +kubectl delete pod myapp +```