|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.27: 使用 Kubelet API 查询节点日志" |
| 4 | +date: 2023-04-21 |
| 5 | +slug: node-log-query-alpha |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- |
| 9 | +layout: blog |
| 10 | +title: "Kubernetes 1.27: Query Node Logs Using The Kubelet API" |
| 11 | +date: 2023-04-21 |
| 12 | +slug: node-log-query-alpha |
| 13 | +--> |
| 14 | + |
| 15 | +<!-- |
| 16 | +**Author:** Aravindh Puthiyaparambil (Red Hat) |
| 17 | +--> |
| 18 | +**作者:** Aravindh Puthiyaparambil (Red Hat) |
| 19 | + |
| 20 | +**译者:** Xin Li (DaoCloud) |
| 21 | + |
| 22 | +<!-- |
| 23 | +Kubernetes 1.27 introduced a new feature called _Node log query_ that allows |
| 24 | +viewing logs of services running on the node. |
| 25 | +--> |
| 26 | +Kubernetes 1.27 引入了一个名为**节点日志查询**的新功能, |
| 27 | +可以查看节点上运行的服务的日志。 |
| 28 | + |
| 29 | +<!-- |
| 30 | +## What problem does it solve? |
| 31 | +Cluster administrators face issues when debugging malfunctioning services |
| 32 | +running on the node. They usually have to SSH or RDP into the node to view the |
| 33 | +logs of the service to debug the issue. The _Node log query_ feature helps with |
| 34 | +this scenario by allowing the cluster administrator to view the logs using |
| 35 | +_kubectl_. This is especially useful with Windows nodes where you run into the |
| 36 | +issue of the node going to the ready state but containers not coming up due to |
| 37 | +CNI misconfigurations and other issues that are not easily identifiable by |
| 38 | +looking at the Pod status. |
| 39 | +--> |
| 40 | +## 它解决了什么问题? |
| 41 | + |
| 42 | +集群管理员在调试节点上运行的表现不正常的服务时会遇到问题。 |
| 43 | +他们通常必须通过 SSH 或 RDP 进入节点以查看服务日志以调试问题。 |
| 44 | +**节点日志查询**功能通过允许集群管理员使用 **kubectl** |
| 45 | +查看日志的方式来帮助解决这种情况。这对于 Windows 节点特别有用, |
| 46 | +在 Windows 节点中,你会遇到节点进入就绪状态但由于 CNI |
| 47 | +错误配置和其他不易通过查看 Pod 状态来辨别的问题而导致容器无法启动的情况。 |
| 48 | + |
| 49 | +<!-- |
| 50 | +## How does it work? |
| 51 | +
|
| 52 | +The kubelet already has a _/var/log/_ viewer that is accessible via the node |
| 53 | +proxy endpoint. The feature supplements this endpoint with a shim that shells |
| 54 | +out to `journalctl`, on Linux nodes, and the `Get-WinEvent` cmdlet on Windows |
| 55 | +nodes. It then uses the existing filters provided by the commands to allow |
| 56 | +filtering the logs. The kubelet also uses heuristics to retrieve the logs. |
| 57 | +If the user is not aware if a given system services logs to a file or to the |
| 58 | +native system logger, the heuristics first checks the native operating system |
| 59 | +logger and if that is not available it attempts to retrieve the first logs |
| 60 | +from `/var/log/<servicename>` or `/var/log/<servicename>.log` or |
| 61 | +`/var/log/<servicename>/<servicename>.log`. |
| 62 | +--> |
| 63 | +## 它是如何工作的? |
| 64 | + |
| 65 | +kubelet 已经有一个 **/var/log/** 查看器,可以通过节点代理端点访问。 |
| 66 | +本功能特性通过一个隔离层对这个端点进行增强,在 Linux 节点上通过 |
| 67 | +`journalctl` Shell 调用获得日志,在 Windows 节点上通过 `Get-WinEvent` CmdLet 获取日志。 |
| 68 | +然后它使用命令提供的过滤器来过滤日志。kubelet 还使用启发式方法来检索日志。 |
| 69 | +如果用户不知道给定的系统服务是记录到文件还是本机系统记录器, |
| 70 | +启发式方法首先检查本机操作系统记录器,如果不可用,它会尝试先从 `/var/log/<servicename>` |
| 71 | +或 `/var/log/<servicename>.log` 或 `/var/log/<servicename>/<servicename>.log` 检索日志。 |
| 72 | + |
| 73 | + |
| 74 | +<!-- |
| 75 | +On Linux we assume that service logs are available via journald, and that |
| 76 | +`journalctl` is installed. On Windows we assume that service logs are available |
| 77 | +in the application log provider. Also note that fetching node logs is only |
| 78 | +available if you are authorized to do so (in RBAC, that's **get** and |
| 79 | +**create** access to `nodes/proxy`). The privileges that you need to fetch node |
| 80 | +logs also allow elevation-of-privilege attacks, so be careful about how you |
| 81 | +manage them. |
| 82 | +--> |
| 83 | +在 Linux 上,我们假设服务日志可通过 journald 获得, |
| 84 | +并且安装了 `journalctl`。 在 Windows 上,我们假设服务日志在应用程序日志提供程序中可用。 |
| 85 | +另请注意,只有在你被授权的情况下才能获取节点日志(在 RBAC 中, |
| 86 | +这是对 `nodes/proxy` 的 **get** 和 **create** 访问)。 |
| 87 | +获取节点日志所需的特权也允许特权提升攻击(elevation-of-privilege), |
| 88 | +因此请谨慎管理它们。 |
| 89 | + |
| 90 | +<!-- |
| 91 | +## How do I use it? |
| 92 | +
|
| 93 | +To use the feature, ensure that the `NodeLogQuery` |
| 94 | +[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is |
| 95 | +enabled for that node, and that the kubelet configuration options |
| 96 | +`enableSystemLogHandler` and `enableSystemLogQuery` are both set to true. You can |
| 97 | +then query the logs from all your nodes or just a subset. Here is an example to |
| 98 | +retrieve the kubelet service logs from a node: |
| 99 | +--> |
| 100 | +## 该如何使用它 |
| 101 | + |
| 102 | +要使用该功能,请确保为该节点启用了 `NodeLogQuery` |
| 103 | +[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/), |
| 104 | +并且 kubelet 配置选项 `enableSystemLogHandler` 和 `enableSystemLogQuery` 都设置为 true。 |
| 105 | +然后,你可以查询所有节点或部分节点的日志。下面是一个从节点检索 kubelet 服务日志的示例: |
| 106 | + |
| 107 | +```shell |
| 108 | +# Fetch kubelet logs from a node named node-1.example |
| 109 | +kubectl get --raw "/api/v1/nodes/node-1.example/proxy/logs/?query=kubelet" |
| 110 | +``` |
| 111 | + |
| 112 | +<!-- |
| 113 | +You can further filter the query to narrow down the results: |
| 114 | +--> |
| 115 | +你可以进一步过滤查询以缩小结果范围: |
| 116 | + |
| 117 | +```shell |
| 118 | +# Fetch kubelet logs from a node named node-1.example that have the word "error" |
| 119 | +kubectl get --raw "/api/v1/nodes/node-1.example/proxy/logs/?query=kubelet&pattern=error" |
| 120 | +``` |
| 121 | + |
| 122 | +<!-- |
| 123 | +You can also fetch files from `/var/log/` on a Linux node: |
| 124 | +--> |
| 125 | +你还可以从 Linux 节点上的 `/var/log/` 获取文件: |
| 126 | + |
| 127 | +```shell |
| 128 | +kubectl get --raw "/api/v1/nodes/<insert-node-name-here>/proxy/logs/?query=/<insert-log-file-name-here>" |
| 129 | +``` |
| 130 | + |
| 131 | +<!-- |
| 132 | +You can read the |
| 133 | +[documentation](/docs/concepts/cluster-administration/system-logs/#log-query) |
| 134 | +for all the available options. |
| 135 | +--> |
| 136 | +你可以阅读[文档](/zh-cn/docs/concepts/cluster-administration/system-logs/#log-query)获取所有可用选项。 |
| 137 | + |
| 138 | +<!-- |
| 139 | +## How do I help? |
| 140 | +
|
| 141 | +Please use the feature and provide feedback by opening GitHub issues or |
| 142 | +reaching out to us on the |
| 143 | +[#sig-windows](https://kubernetes.slack.com/archives/C0SJ4AFB7) channel on the |
| 144 | +Kubernetes Slack or the SIG Windows |
| 145 | +[mailing list](https://groups.google.com/g/kubernetes-sig-windows). |
| 146 | +--> |
| 147 | +## 如何提供帮助 |
| 148 | + |
| 149 | +请使用该功能并通过在 GitHub 上登记问题或通过 Kubernetes Slack |
| 150 | +的 [#sig-windows](https://kubernetes.slack.com/archives/C0SJ4AFB7) 频道 |
| 151 | +或 SIG Windows [邮件列表](https://groups.google.com/g/kubernetes-sig-windows) |
| 152 | +联系我们来提供反馈。 |
0 commit comments