|
| 1 | +--- |
| 2 | +title: 用 Kubectl 调试 Kubernetes 节点 |
| 3 | +content_type: task |
| 4 | +min-kubernetes-server-version: 1.20 |
| 5 | +--- |
| 6 | +<!-- |
| 7 | +title: Debugging Kubernetes Nodes With Kubectl |
| 8 | +content_type: task |
| 9 | +min-kubernetes-server-version: 1.20 |
| 10 | +--> |
| 11 | + |
| 12 | +<!-- overview --> |
| 13 | + |
| 14 | +<!-- |
| 15 | +This page shows how to debug a [node](/docs/concepts/architecture/nodes/) |
| 16 | +running on the Kubernetes cluster using `kubectl debug` command. |
| 17 | +--> |
| 18 | +本页演示如何使用 `kubectl debug` 命令调试在 Kubernetes |
| 19 | +集群上运行的[节点](/zh-cn/docs/concepts/architecture/nodes/)。 |
| 20 | + |
| 21 | +## {{% heading "prerequisites" %}} |
| 22 | + |
| 23 | +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} |
| 24 | + |
| 25 | +<!-- |
| 26 | +You need to have permission to create Pods and to assign those new Pods to arbitrary nodes. |
| 27 | +You also need to be authorized to create Pods that access filesystems from the host. |
| 28 | +--> |
| 29 | +你需要有权限创建 Pod 并将这些新 Pod 分配到任意节点。 |
| 30 | +你还需要被授权创建能够访问主机上文件系统的 Pod。 |
| 31 | + |
| 32 | +<!-- steps --> |
| 33 | + |
| 34 | +<!-- |
| 35 | +## Debugging a Node using `kubectl debug node` |
| 36 | +
|
| 37 | +Use the `kubectl debug node` command to deploy a Pod to a Node that you want to troubleshoot. |
| 38 | +This command is helpful in scenarios where you can't access your Node by using an SSH connection. |
| 39 | +When the Pod is created, the Pod opens an interactive shell on the Node. |
| 40 | +To create an interactive shell on a Node named “mynode”, run: |
| 41 | +--> |
| 42 | +## 使用 `kubectl debug node` 调试节点 {#debugging-a-node-using-kubectl-debug-node} |
| 43 | + |
| 44 | +使用 `kubectl debug node` 命令将 Pod 部署到要排查故障的节点上。 |
| 45 | +此命令在你无法使用 SSH 连接节点时比较有用。 |
| 46 | +当 Pod 被创建时,Pod 会在节点上打开一个交互的 Shell。 |
| 47 | +要在名为 “mynode” 的节点上创建一个交互式 Shell,运行: |
| 48 | + |
| 49 | +```shell |
| 50 | +kubectl debug node/mynode -it --image=ubuntu |
| 51 | +``` |
| 52 | + |
| 53 | +```console |
| 54 | +Creating debugging pod node-debugger-mynode-pdx84 with container debugger on node mynode. |
| 55 | +If you don't see a command prompt, try pressing enter. |
| 56 | +root@mynode:/# |
| 57 | +``` |
| 58 | + |
| 59 | +<!-- |
| 60 | +The debug command helps to gather information and troubleshoot issues. Commands |
| 61 | +that you might use include `ip`, `ifconfig`, `nc`, `ping`, and `ps` and so on. You can also |
| 62 | +install other tools, such as `mtr`, `tcpdump`, and `curl`, from the respective package manager. |
| 63 | +--> |
| 64 | +调试命令有助于收集信息和排查问题。 |
| 65 | +你可能使用的命令包括 `ip`、`ifconfig`、`nc`、`ping` 和 `ps` 等等。 |
| 66 | +你还可以从各种包管理器安装 `mtr`、`tcpdump` 和 `curl` 等其他工具。 |
| 67 | + |
| 68 | +{{< note >}} |
| 69 | +<!-- |
| 70 | +The debug commands may differ based on the image the debugging pod is using and |
| 71 | +these commands might need to be installed. |
| 72 | +--> |
| 73 | +这些调试命令会因调试 Pod 所使用的镜像不同而有些差别,并且这些命令可能需要被安装。 |
| 74 | +{{< /note >}} |
| 75 | + |
| 76 | +<!-- |
| 77 | +The debugging Pod can access the root filesystem of the Node, mounted at `/host` in the Pod. |
| 78 | +If you run your kubelet in a filesystem namespace, |
| 79 | +the debugging Pod sees the root for that namespace, not for the entire node. For a typical Linux node, |
| 80 | +you can look at the following paths to find relevant logs: |
| 81 | +--> |
| 82 | +用于调试的 Pod 可以访问节点的根文件系统,该文件系统挂载在 Pod 中的 `/host` 路径。 |
| 83 | +如果你在 filesystem 名字空间中运行 kubelet, |
| 84 | +则正调试的 Pod 将看到此名字空间的根,而不是整个节点的根。 |
| 85 | +对于典型的 Linux 节点,你可以查看以下路径找到一些重要的日志: |
| 86 | + |
| 87 | +<!-- |
| 88 | +`/host/var/log/kubelet.log` |
| 89 | +: Logs from the `kubelet`, responsible for running containers on the node. |
| 90 | +
|
| 91 | +`/host/var/log/kube-proxy.log` |
| 92 | +: Logs from `kube-proxy`, which is responsible for directing traffic to Service endpoints. |
| 93 | +
|
| 94 | +`/host/var/log/containerd.log` |
| 95 | +: Logs from the `containerd` process running on the node. |
| 96 | +
|
| 97 | +`/host/var/log/syslog` |
| 98 | +: Shows general messages and information regarding the system. |
| 99 | +
|
| 100 | +`/host/var/log/kern.log` |
| 101 | +: Shows kernel logs. |
| 102 | +--> |
| 103 | +`/host/var/log/kubelet.log` |
| 104 | +: 负责在节点上运行容器的 `kubelet` 所产生的日志。 |
| 105 | + |
| 106 | +`/host/var/log/kube-proxy.log` |
| 107 | +: 负责将流量导向到 Service 端点的 `kube-proxy` 所产生的日志。 |
| 108 | + |
| 109 | +`/host/var/log/containerd.log` |
| 110 | +: 在节点上运行的 `containerd` 进程所产生的日志。 |
| 111 | + |
| 112 | +`/host/var/log/syslog` |
| 113 | +: 显示常规消息以及系统相关信息。 |
| 114 | + |
| 115 | +`/host/var/log/kern.log` |
| 116 | +: 显示内核日志。 |
| 117 | + |
| 118 | +<!-- |
| 119 | +When creating a debugging session on a Node, keep in mind that: |
| 120 | +
|
| 121 | +* `kubectl debug` automatically generates the name of the new pod, based on |
| 122 | + the name of the node. |
| 123 | +* The root filesystem of the Node will be mounted at `/host`. |
| 124 | +* Although the container runs in the host IPC, Network, and PID namespaces, |
| 125 | + the pod isn't privileged. This means that reading some process information might fail |
| 126 | + because access to that information is restricted to superusers. For example, `chroot /host` will fail. |
| 127 | + If you need a privileged pod, create it manually. |
| 128 | +--> |
| 129 | +当在节点上创建一个调试会话时,需谨记: |
| 130 | + |
| 131 | +* `kubectl debug` 根据节点的名称自动生成新 Pod 的名称。 |
| 132 | +* 节点的根文件系统将被挂载在 `/host`。 |
| 133 | +* 尽管容器运行在主机 IPC、Network 和 PID 名字空间中,但 Pod 没有特权。 |
| 134 | + 这意味着读取某些进程信息可能会失败,这是因为访问这些信息仅限于超级用户 (superuser)。 |
| 135 | + 例如,`chroot /host` 将失败。如果你需要一个有特权的 Pod,请手动创建。 |
| 136 | + |
| 137 | +## {{% heading "cleanup" %}} |
| 138 | + |
| 139 | +<!-- |
| 140 | +When you finish using the debugging Pod, delete it: |
| 141 | +--> |
| 142 | +当你使用正调试的 Pod 完成时,将其删除: |
| 143 | + |
| 144 | +```shell |
| 145 | +kubectl get pods |
| 146 | +``` |
| 147 | + |
| 148 | +```none |
| 149 | +NAME READY STATUS RESTARTS AGE |
| 150 | +node-debugger-mynode-pdx84 0/1 Completed 0 8m1s |
| 151 | +``` |
| 152 | + |
| 153 | +<!-- Change the pod name accordingly --> |
| 154 | +```shell |
| 155 | +# 相应更改 Pod 名称 |
| 156 | +kubectl delete pod node-debugger-mynode-pdx84 --now |
| 157 | +``` |
| 158 | + |
| 159 | +```none |
| 160 | +pod "node-debugger-mynode-pdx84" deleted |
| 161 | +``` |
| 162 | + |
| 163 | +{{< note >}} |
| 164 | +<!-- |
| 165 | +The `kubectl debug node` command won't work if the Node is down (disconnected |
| 166 | +from the network, or kubelet dies and won't restart, etc.). |
| 167 | +Check [debugging a down/unreachable node ](/docs/tasks/debug/debug-cluster/#example-debugging-a-down-unreachable-node) |
| 168 | +in that case. |
| 169 | +--> |
| 170 | +如果节点停机(网络断开或 kubelet 宕机且无法启动等),则 `kubectl debug node` 命令将不起作用。 |
| 171 | +这种情况下请检查[调试关闭/无法访问的节点](/zh-cn/docs/tasks/debug/debug-cluster/#example-debugging-a-down-unreachable-node)。 |
| 172 | +{{< /note >}} |
0 commit comments