如何实时查看 Kubernetes 日志、事件和 Pod 指标How to view Kubernetes logs, events, and pod metrics in real-time

适用于容器的 Azure Monitor 包括实时数据(预览版)功能,这是一项高级诊断功能,可通过它直接访问 Azure Kubernetes 服务 (AKS) 容器日志 (stdout/stderror)、事件和 Pod 指标。Azure Monitor for containers includes the Live Data (preview) feature, which is an advanced diagnostic feature allowing you direct access to your Azure Kubernetes Service (AKS) container logs (stdout/stderror), events, and pod metrics. 它公开对 kubectl logs -ckubectl get 事件和 kubectl top pods 的直接访问。It exposes direct access to kubectl logs -c, kubectl get events, and kubectl top pods. 控制台窗格显示由容器引擎生成的日志、事件和指标,可以进一步协助实时排查问题。A console pane shows the logs, events, and metrics generated by the container engine to further assist in troubleshooting issues in real-time.

本文详细介绍了此功能,并帮助你了解如何使用此功能。This article provides a detailed overview and helps you understand how to use this feature.

备注

此功能不支持以专用群集形式启用的 AKS 群集。AKS clusters enabled as private clusters are not supported with this feature. 此功能依赖于从浏览器通过代理服务器直接访问 Kubernetes API。This feature relies on directly accessing the Kubernetes API through a proxy server from your browser. 启用网络安全以阻止来自此代理的 Kubernetes API 阻止此流量。Enabling networking security to block the Kubernetes API from this proxy will block this traffic.

有关设置实时数据(预览版)功能或对其进行故障排除的帮助,请参阅安装指南For help setting up or troubleshooting the Live Data (preview) feature, review our setup guide. 该功能直接访问 Kubernetes API,有关身份验证模型的其他信息可以在此处找到。This feature directly access the Kubernetes API, and additional information about the authentication model can be found here.

实时数据(预览版)功能概述Live Data (preview) functionality overview

实时数据控制台窗格筛选器示例

实时数据(预览版)功能包括搜索功能。The Live Data (preview) feature includes search functionality. 在“搜索”字段中,可以通过键入关键字或字词来筛选结果,并突出显示任何匹配结果以允许快速查看。In the Search field, you can filter results by typing a key word or term and any matching results are highlighted to allow quick review. 在查看事件时,可以进一步对结果进行限制,只需使用在搜索栏右侧发现的“筛选器”药丸图标即可。While viewing events, you can additionally limit the results using the Filter pill found to the right of the search bar. 根据所选资源,药丸图标会列出可供选择的 Pod、命名空间或群集。Depending on what resource you have selected, the pill lists a Pod, Namespace, or cluster to chose from.

滚动锁定和暂停Scroll Lock and Pause

若要暂停自动滚动并控制窗格的行为,以便通过手动方式滚动浏览读取的新数据,可使用“滚动”选项。To suspend autoscroll and control the behavior of the pane, allowing you to manually scroll through the new data read, you can use the Scroll option. 若要重新启用自动滚动,只需再次选择“滚动”选项。To re-enable autoscroll, simply select the Scroll option again. 也可通过选择“暂停”选项暂停对日志或事件数据的检索,并在准备好继续时,只需选择“开始”即可 。You can also pause retrieval of log or event data by selecting the the Pause option, and when you are ready to resume, simply select Play.

实时数据控制台窗格的暂停实时视图

重要

在对问题进行故障排除时,建议只短暂地挂起或暂停自动滚动。We recommend only suspending or pausing autoscroll for a short period of time while troubleshooting an issue. 这些请求可能会影响群集上 Kubernetes API 的可用性和限制。These requests may impact the availability and throttling of the Kubernetes API on your cluster.

重要

此功能运行期间不会永久存储任何数据。No data is stored permanently during operation of this feature. 当你关闭或离开浏览器时,将删除在此会话期间捕获的所有信息。All information captured during the session is deleted when you close your browser or navigate away from it. 数据只在指标功能的五分钟窗口内显示,任何超过五分钟的指标也将删除。Data only remains present for visualization inside the five minute window of the metrics feature; any metrics older than five minutes are also deleted. 实时数据(预览版)在合理内存使用量限制内缓冲查询。The Live Data (preview) buffer queries within reasonable memory usage limits.

查看日志View logs

可以在“节点”、“控制器”和“容器”视图中查看容器引擎生成的实时日志数据 。You can view real-time log data as they are generated by the container engine from the Nodes, Controllers, and Containers view. 若要查看日志数据,请执行以下步骤。To view log data, perform the following steps.

  1. 在 Azure 门户中,浏览到 AKS 群集资源组,然后选择 AKS 资源。In the Azure portal, browse to the AKS cluster resource group and select your AKS resource.

  2. 在 AKS 群集仪表板上,在左侧的“监视”下,选择“见解” 。On the AKS cluster dashboard, under Monitoring on the left-hand side, choose Insights.

  3. 选择“节点”、“控制器”或“容器”选项卡 。Select either the Nodes, Controllers, or Containers tab.

  4. 从性能网格中选择一个对象,然后在右侧的属性窗格中选择“查看实时数据(预览版)”选项。Select an object from the performance grid, and on the properties pane found on the right side, select View live data (preview) option. 如果使用 Azure AD 为 AKS 群集配置了单一登录,系统会在该浏览器会话期间向你提示进行首次使用所需的身份验证。If the AKS cluster is configured with single sign-on using Azure AD, you are prompted to authenticate on first use during that browser session. 选择帐户,然后使用 Azure 完成身份验证。Select your account and complete authentication with Azure.

    备注

    通过从属性窗格中选择“在分析中查看”选项从 Log Analytics 工作区中查看数据时,日志搜索结果可能会显示可能不再存在的节点、守护程序集、副本集、作业、Cron 作业、Pod 和容器 。When viewing the data from your Log Analytics workspace by selecting the View in analytics option from the properties pane, the log search results will potentially show Nodes, Daemon Sets, Replica Sets, Jobs, Cron Jobs, Pods, and Containers which may no longer exist. 尝试搜索在 kubectl 中不可用的容器的日志也会失败。Attempting to search logs for a container which isn't available in kubectl will also fail here. 查看在分析中查看功能,了解有关查看历史日志、事件和指标的详细信息。Review the View in analytics feature to learn more about viewing historical logs, events and metrics.

成功进行身份验证后,实时数据(预览版)控制台窗格将显示在性能数据网格下,你可以在其中查看连续流中的日志数据。After successfully authenticating, the Live Data (preview) console pane will appear below the performance data grid where you can view log data in a continuous stream. 如果提取状态指示器显示绿色复选标记(位于窗格最右侧),则表示可以检索数据并开始将其流式传输到控制台。If the fetch status indicator shows a green check mark, which is on the far right of the pane, it means data can be retrieved and it begins streaming to your console.

节点属性窗格的“查看数据”选项

窗格标题会显示进行容器分组时所使用的 Pod 的名称。The pane title shows the name of the pod the container is grouped with.

查看事件View events

选择容器、Pod、节点、ReplicaSet、DaemonSet、作业、CronJob 或部署后,可以从“节点”、“控制器”、“容器和部署(预览版)”视图中查看由容器引擎生成的实时事件数据 。You can view real-time event data as they are generated by the container engine from the Nodes, Controllers, Containers, and Deployments (preview) view when a container, pod, node, ReplicaSet, DaemonSet, job, CronJob or Deployment is selected. 若要查看事件,请执行以下步骤。To view events, perform the following steps.

  1. 在 Azure 门户中,浏览到 AKS 群集资源组,然后选择 AKS 资源。In the Azure portal, browse to the AKS cluster resource group and select your AKS resource.

  2. 在 AKS 群集仪表板上,在左侧的“监视”下,选择“见解” 。On the AKS cluster dashboard, under Monitoring on the left-hand side, choose Insights.

  3. 选择“节点”、“控制器”、“容器”或“部署(预览版)”选项卡 。Select either the Nodes, Controllers, Containers, or Deployments (preview) tab.

  4. 从性能网格中选择一个对象,然后在右侧的属性窗格中选择“查看实时数据(预览版)”选项。Select an object from the performance grid, and on the properties pane found on the right side, select View live data (preview) option. 如果使用 Azure AD 为 AKS 群集配置了单一登录,系统会在该浏览器会话期间向你提示进行首次使用所需的身份验证。If the AKS cluster is configured with single sign-on using Azure AD, you are prompted to authenticate on first use during that browser session. 选择帐户,然后使用 Azure 完成身份验证。Select your account and complete authentication with Azure.

    备注

    通过从属性窗格中选择“在分析中查看”选项从 Log Analytics 工作区中查看数据时,日志搜索结果可能会显示可能不再存在的节点、守护程序集、副本集、作业、Cron 作业、Pod 和容器 。When viewing the data from your Log Analytics workspace by selecting the View in analytics option from the properties pane, the log search results will potentially show Nodes, Daemon Sets, Replica Sets, Jobs, Cron Jobs, Pods, and Containers which may no longer exist. 尝试搜索在 kubectl 中不可用的容器的日志也会失败。Attempting to search logs for a container which isn't available in kubectl will also fail here. 查看在分析中查看功能,了解有关查看历史日志、事件和指标的详细信息。Review the View in analytics feature to learn more about viewing historical logs, events and metrics.

成功进行身份验证后,实时数据(预览版)控制台窗格将显示在性能数据网格下。After successfully authenticating, the Live Data (preview) console pane will appear below the performance data grid. 如果提取状态指示器显示绿色复选标记(位于窗格最右侧),则表示可以检索数据并开始将其流式传输到控制台。If the fetch status indicator shows a green check mark, which is on the far right of the pane, it means data can be retrieved and it begins streaming to your console.

如果所选对象是容器,请在窗格中选择“事件”选项。If the object you selected was a container, select the Events option in the pane. 如果选择了节点、Pod 或控制器,则会自动选择“查看事件”。If you selected a Node, Pod, or controller, viewing events is automatically selected.

控制器属性窗格查看事件

窗格标题会显示进行容器分组时所使用的 Pod 的名称。The pane title shows the name of the Pod the container is grouped with.

筛选事件Filter events

在查看事件时,可以进一步对结果进行限制,只需使用在搜索栏右侧发现的“筛选器”药丸图标即可。While viewing events, you can additionally limit the results using the Filter pill found to the right of the search bar. 根据所选资源,药丸图标会列出可供选择的 Pod、命名空间或群集。Depending on what resource you have selected, the pill lists a Pod, Namespace, or cluster to chose from.

查看指标View metrics

仅当选择了 Pod 时,才能从“节点”或“控制器”视图中查看容器引擎生成的实时指标数据 。You can view real-time metric data as they are generated by the container engine from the Nodes or Controllers view only when a Pod is selected. 若要查看指标,请执行以下步骤。To view metrics, perform the following steps.

  1. 在 Azure 门户中,浏览到 AKS 群集资源组,然后选择 AKS 资源。In the Azure portal, browse to the AKS cluster resource group and select your AKS resource.

  2. 在 AKS 群集仪表板上,在左侧的“监视”下,选择“见解” 。On the AKS cluster dashboard, under Monitoring on the left-hand side, choose Insights.

  3. 选择“节点”或“控制器”选项卡 。Select either the Nodes or Controllers tab.

  4. 从性能网格中选择“Pod”,然后在右侧的属性窗格中选择“查看实时数据 (预览版)”选项 。Select a Pod object from the performance grid, and on the properties pane found on the right side, select View live data (preview) option. 如果使用 Azure AD 为 AKS 群集配置了单一登录,系统会在该浏览器会话期间向你提示进行首次使用所需的身份验证。If the AKS cluster is configured with single sign-on using Azure AD, you are prompted to authenticate on first use during that browser session. 选择帐户,然后使用 Azure 完成身份验证。Select your account and complete authentication with Azure.

    备注

    通过从属性窗格中选择“在分析中查看”选项从 Log Analytics 工作区中查看数据时,日志搜索结果可能会显示可能不再存在的节点、守护程序集、副本集、作业、Cron 作业、Pod 和容器 。When viewing the data from your Log Analytics workspace by selecting the View in analytics option from the properties pane, the log search results will potentially show Nodes, Daemon Sets, Replica Sets, Jobs, Cron Jobs, Pods, and Containers which may no longer exist. 尝试搜索在 kubectl 中不可用的容器的日志也会失败。Attempting to search logs for a container which isn't available in kubectl will also fail here. 查看在分析中查看功能,了解有关查看历史日志、事件和指标的详细信息。Review the View in analytics feature to learn more about viewing historical logs, events and metrics.

成功进行身份验证后,实时数据(预览版)控制台窗格将显示在性能数据网格下。After successfully authenticating, the Live Data (preview) console pane will appear below the performance data grid. 检索指标数据并开始流式传输到控制台,以便在两个图表中显示。Metric data is retrieved and begins streaming to your console for presentation in the two charts. 窗格标题会显示进行容器分组时所使用的 Pod 的名称。The pane title shows the name of the pod the container is grouped with.

查看 Pod 指标示例

后续步骤Next steps