启用对 Kubernetes 群集的监视

本文介绍如何使用以下 Azure Monitor 功能启用 Kubernetes 群集的完整监视:

使用 Azure 门户,可以同时启用所有这些功能。 还可以使用 Azure CLI、Azure 资源管理器模板、Terraform 或 Azure Policy 单独启用它们。 本文介绍了上述每种方法。

重要

Kubernetes 群集会生成大量日志数据,如果你对收集的日志没有选择性,可能会导致高昂的成本。 在启用群集监视之前,请参阅以下文章,以确保环境针对成本进行了优化,并将日志收集限制为仅收集所需的数据:

支持的群集

本文就以下类型的群集提供加入指南。 相关部分中介绍了每种类型过程中的任何差异。

先决条件

权限

已启用 Arc 的 Kubernetes 群集先决条件

工作区

下表描述了支持容器见解所需的工作区。 可在载入过程中创建每个工作区,或者使用现有工作区。 请参阅设计 Log Analytics 工作区体系结构,获取有关要创建多少个工作区以及应在何处放置工作区的指导。

功能 工作区 说明
容器见解 Log Analytics 工作区 可以将 AKS 群集附加到同一 Microsoft Entra 租户的不同 Azure 订阅中的 Log Analytics 工作区,但必须使用 Azure CLI 或 Azure 资源管理器模板。 目前不能使用 Azure 门户执行此配置。

如果要将现有的 AKS 群集连接到其他订阅中的 Log Analytics 工作区,则必须在具有 Log Analytics 工作区的订阅中注册 Microsoft.ContainerService 资源提供程序。 有关详细信息,请参阅注册资源提供程序

启用容器见解

按照下述方法之一在群集中启用容器见解。 完成此操作后,请参阅配置容器见解的代理数据收集来自定义配置,确保收集的数据量不超过所需的量。

使用下述命令之一来启用对 AKS 和已启用 Arc 的群集的监视。 如果未指定现有的 Log Analytics 工作区,将使用资源组的默认工作区。 如果群集区域中尚不存在默认工作区,会使用 DefaultWorkspace-<GUID>-<Region> 格式的名称创建一个工作区。

先决条件

  • Azure CLI 版本 2.43.0 或更高版本
  • 托管标识身份验证是 CLI 版本 2.49.0 或更高版本中的默认设置。
  • Azure k8s-extension 版本 1.3.7 或更高版本
  • 托管标识身份验证是 k8s-extension 版本 1.43.0 或更高版本中的默认设置。
  • 使用 ARO (Azure Red Hat Openshift) 或 Windows 节点且已启用 Arc 的 Kubernetes 群集不支持托管标识身份验证。 使用旧式身份验证。
  • 对于 CLI 2.54.0 或更高版本,会使用 ConfigMap 将日志记录架构将配置为 ContainerLogV2

AKS 群集

### Use default Log Analytics workspace
az aks enable-addons -a monitoring -n <cluster-name> -g <cluster-resource-group-name>

### Use existing Log Analytics workspace
az aks enable-addons -a monitoring -n <cluster-name> -g <cluster-resource-group-name> --workspace-resource-id <workspace-resource-id>

示例

az aks enable-addons -a monitoring -n "my-cluster" -g "my-resource-group" --workspace-resource-id "/subscriptions/my-subscription/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace"

已启用 Arc 的群集

### Use default Log Analytics workspace
az k8s-extension create --name azuremonitor-containers --cluster-name <cluster-name> --resource-group <resource-group> --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers

### Use existing Log Analytics workspace
az k8s-extension create --name azuremonitor-containers --cluster-name <cluster-name> --resource-group <resource-group> --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers --configuration-settings logAnalyticsWorkspaceResourceID=<workspace-resource-id>

### Use managed identity authentication (default as k8s-extension version 1.43.0)
az k8s-extension create --name azuremonitor-containers --cluster-name <cluster-name> --resource-group <resource-group> --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers --configuration-settings amalogs.useAADAuth=true

### Use advanced configuration settings
az k8s-extension create --name azuremonitor-containers --cluster-name <cluster-name> --resource-group <resource-group> --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers --configuration-settings  amalogs.resources.daemonset.limits.cpu=150m amalogs.resources.daemonset.limits.memory=600Mi amalogs.resources.deployment.limits.cpu=1 amalogs.resources.deployment.limits.memory=750Mi

### With custom mount path for container stdout & stderr logs
### Custom mount path not required for Azure Stack Edge version > 2318. Custom mount path must be /home/data/docker for Azure Stack Edge cluster with version <= 2318
az k8s-extension create --name azuremonitor-containers --cluster-name <cluster-name> --resource-group <resource-group> --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers --configuration-settings amalogs.logsettings.custommountpath=<customMountPath>

请参阅 Helm 图表的资源请求和限制部分,了解可用的配置设置。

示例

az k8s-extension create --name azuremonitor-containers --cluster-name "my-cluster" --resource-group "my-resource-group" --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers --configuration-settings logAnalyticsWorkspaceResourceID="/subscriptions/my-subscription/resourceGroups/my-resource-group/providers/Microsoft.OperationalInsights/workspaces/my-workspace"

具有前向代理的已启用 Arc 的群集

如果群集配置了正向代理,则代理设置会自动应用于扩展。 对于使用 AMPLS + 代理的群集,应忽略代理配置。 使用配置设置 amalogs.ignoreExtensionProxySettings=true 加入扩展。

az k8s-extension create --name azuremonitor-containers --cluster-name <cluster-name> --resource-group <resource-group> --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers --configuration-settings amalogs.ignoreExtensionProxySettings=true

具有 ARO、OpenShift 或 Windows 节点的已启用 Arc 的群集

具有 ARO (Azure Red Hat Openshift) 或 Openshift 或 Windows 节点且已启用 Arc 的 Kubernetes 群集不支持托管标识身份验证。 通过指定 amalogs.useAADAuth=false 来使用旧式身份验证,如以下示例所示。

az k8s-extension create --name azuremonitor-containers --cluster-name <cluster-name> --resource-group <resource-group> --cluster-type connectedClusters --extension-type Microsoft.AzureMonitor.Containers --configuration-settings amalogs.useAADAuth=false

删除扩展实例

以下命令仅删除扩展实例,但不删除 Log Analytics 工作区。 Log Analytics 资源中的数据保持不变。

az k8s-extension delete --name azuremonitor-containers --cluster-type connectedClusters --cluster-name <cluster-name> --resource-group <resource-group>

使用 Azure 门户启用完整监视

使用 Azure 门户。

新建 AKS 群集

在 Azure 门户中创建新的 AKS 群集时,可以通过“集成”选项卡启用容器见解。在 Azure Monitor 部分中选择“默认配置”,如果想指定要使用的工作区,则选择“自定义配置”。 创建群集后,可以执行其他配置。

现有群集

此选项为现有 AKS 群集启用容器见解。

  1. 从群集菜单中选择“见解”,或者从“监视”菜单中选择“容器”,选择“未受监视的群集”选项卡,然后单击群集旁边的“启用”。

    1. 如果未为群集启用容器见解,则会看到一个用于标识已启用功能的屏幕。 单击“配置监视”

    屏幕截图显示群集配置屏幕。

    1. 如果群集上已启用容器见解,请选择“监视设置”按钮修改配置

    屏幕截图显示群集的监视设置按钮。

  2. 单击“高级设置”以选择备用工作区或创建新工作区。 使用“成本预设”设置可以修改默认集合详细信息以降低监视成本。 有关详细信息,请参阅在容器见解中启用成本优化设置

  3. 单击“配置”以保存配置。

验证部署

使用 kubectl 命令行工具验证是否已正确部署代理。

容器见解

验证是否已在 Linux 节点池上正确部署 DaemonSet

kubectl get ds ama-logs --namespace=kube-system

Pod 数应等于群集上的 Linux 节点数。 输出应与下面的示例类似:

User@aksuser:~$ kubectl get ds ama-logs --namespace=kube-system
NAME       DESIRED   CURRENT   READY     UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
ama-logs   2         2         2         2            2           <none>          1d

验证是否已正确部署 Windows 节点

kubectl get ds ama-logs-windows --namespace=kube-system

Pod 数应等于群集上的 Windows 节点数。 输出应与下面的示例类似:

User@aksuser:~$ kubectl get ds ama-logs-windows --namespace=kube-system
NAME                   DESIRED   CURRENT   READY     UP-TO-DATE   AVAILABLE   NODE SELECTOR     AGE
ama-logs-windows           2         2         2         2            2       <none>            1d

验证容器见解解决方案的部署

kubectl get deployment ama-logs-rs --namespace=kube-system

输出应与下面的示例类似:

User@aksuser:~$ kubectl get deployment ama-logs-rs --namespace=kube-system
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
ama-logs-rs   1/1     1            1           24d

使用 CLI 查看配置

使用 aks show 命令查看是否已启用解决方案,并查看 Log Analytics 工作区资源 ID 以及有关群集的摘要信息。

az aks show -g <resourceGroupofAKSCluster> -n <nameofAksCluster>

该命令将会返回有关解决方案的 JSON 格式信息。 addonProfiles 部分应包括有关 omsagent 的信息,如下例所示:

"addonProfiles": {
    "omsagent": {
        "config": {
            "logAnalyticsWorkspaceResourceID": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-workspace",
            "useAADAuth": "true"
        },
        "enabled": true,
        "identity": null
    },
}

预配的资源

启用监视时,会在你的订阅中创建以下资源:

资源名称 资源类型 资源组 区域/位置 说明
MSCI-<aksclusterregion>-<clustername> 数据收集规则 与群集相同 与 Log Analytics 工作区相同 此数据收集规则适用于 Azure Monitor 代理的日志收集,该代理使用 Log Analytics 工作区作为目标,并关联到 AKS 群集资源。
MSPROM-<aksclusterregion>-<clustername> 数据收集规则 与群集相同 与 Azure Monitor 工作区相同 此数据收集规则适用于指标加载项的 Prometheus 指标收集,该加载项将所选的 Azure Monitor 工作区作为目标,并且还关联到 AKS 群集资源
MSPROM-<aksclusterregion>-<clustername> 数据收集终结点 与群集相同 与 Azure Monitor 工作区相同 上述数据收集规则使用此数据收集终结点从指标加载项引入 Prometheus 指标

新建 Azure Monitor 工作区时,会创建以下附加资源作为工作区的一部分

资源名称 资源类型 资源组 区域/位置 说明
<azuremonitor-workspace-name> 数据收集规则 MA_<azuremonitor-workspace-name>_<azuremonitor-workspace-region>_managed 与 Azure Monitor 工作区相同 使用 OSS Prometheus 服务器远程写入 Azure Monitor 工作区时创建的 DCR。
<azuremonitor-workspace-name> 数据收集终结点 MA_<azuremonitor-workspace-name>_<azuremonitor-workspace-region>_managed 与 Azure Monitor 工作区相同 使用 OSS Prometheus 服务器远程写入 Azure Monitor 工作区时创建的 DCE。

Windows 群集与 Linux 群集之间的差异

监视 Windows Server 群集与监视 Linux 群集相比,主要差异包括:

  • Windows 没有内存 RSS 指标。 因此它不适用于 Windows 节点和容器。 可使用工作集指标。
  • 磁盘存储容量信息不适用于 Windows 节点。
  • 仅监视 Pod 环境,不监视 Docker 环境。
  • 使用预览版时,最多支持 30 个 Windows Server 容器。 此限制不适用于 Linux 容器。

注意

容器见解对 Windows Server 2022 操作系统的支持目前为公共预览版。

容器化 Linux 代理 (replicaset pod) 向群集内 Kubelet 安全端口 (10250) 上的所有 Windows 节点进行 API 调用,以收集与节点和容器性能相关的指标。 应在群集的虚拟网络中针对入站和出站打开 Kubelet 安全端口 (:10250),以便正常收集 Windows 节点和容器性能相关指标。

如果你有一个包含 Windows 节点的 Kubernetes 群集,请查看并配置网络安全组和网络策略,确保在群集的虚拟网络中针对入站和出站打开 Kubelet 安全端口 (:10250)。

后续步骤

  • 如果在尝试加入解决方案时遇到问题,请查看故障排除指南
  • 在启用了监视功能以收集 AKS 群集以及在其上运行的工作负荷的运行状况和资源利用率之后,请了解如何使用容器见解。