共用方式為

使用托管标识身份验证将 Prometheus 数据发送到 Azure Monitor

重要

本文介绍了如何在 Azure Monitor 为 Prometheus 提供的托管服务中,通过托管身份验证和 Azure Monitor 提供的 sidecar 容器设置远程写入。 在不使用 sidecar 的情况下,可以使用托管标识结合远程写入,参考 将自托管的 Prometheus 连接到 Azure Monitor 管理服务的指南

本文介绍如何设置远程写入,以使用托管标识身份验证和 Azure Monitor 提供的挎斗容器从 Azure Kubernetes 服务 (AKS) 群集或已启用 Azure Arc 的 Kubernetes 群集中运行的自托管 Prometheus 服务器发送数据。 可以使用 AKS 创建的现有标识,也可以创建自己的标识。 此处对这两个选项均有介绍。

注意

如果使用用户分配的托管标识,建议直接将 Kubernetes 群集上运行的 Prometheus 配置为远程写入 Azure Monitor 工作区。 请参阅使用用户分配的托管标识将 Prometheus 数据发送到 Azure Monitor,了解详细信息。 以下步骤使用 Azure Monitor 挎斗容器。

群集配置

本文适用于以下群集配置:

  • Azure Kubernetes 服务群集
  • 已启用 Azure Arc 的 Kubernetes 群集

先决条件

支持的版本

托管标识身份验证需要高于 v2.45 的 Prometheus 版本。

  • 用户分配的托管身份验证需要使用 v2.45 以上的 Prometheus 版本。
  • Microsoft Entra ID 应用程序身份验证需要高于 v2.48 的 Prometheus 版本。
  • Prometheus 版本必须为 v3.50 或更高,以满足系统分配的托管标识身份验证要求。

Azure Monitor 工作区

本文介绍如何将 Prometheus 指标发送到 Azure Monitor 工作区。 若要创建 Azure Monitor 工作区,请参阅管理 Azure Monitor 工作区

权限

完成本文中的步骤需要群集或资源的管理员权限。

为托管标识设置应用程序

使用托管标识身份验证为应用程序设置 Prometheus 远程写入的过程涉及到以下任务的完成:

  1. 获取群集节点资源组的名称。
  2. 获取用户分配的托管标识的客户端 ID。
  3. 为托管标识分配针对工作区数据收集规则的“监视指标发布者”角色。
  4. 授予群集对托管标识的访问权限。
  5. 部署挎斗容器以设置远程写入。

以下部分描述了这些任务。

获取群集节点资源组的名称

群集的节点资源组包含在此过程中其他步骤中使用的资源。 此资源组名为 MC_<RESOURCE-GROUP>_<CLUSTER-NAME>_<REGION>。 可以使用 Azure 门户中的“资源组”菜单找到资源组名称。

屏幕截图显示资源组列表。

获取用户分配的托管标识的客户端 ID

需要获取要使用的标识的客户端 ID。 复制客户端 ID,以便稍后在进程中使用。

可以使用 AKS 创建的标识之一,而不用创建自己的客户端 ID。 若要详细了解标识,请参阅在 Azure Kubernetes 服务中使用托管标识

本文使用 kubelet 标识。 此标识的名称是 <CLUSTER-NAME>-agentpool,它位于群集的节点资源组中。

屏幕截图显示节点资源组中的资源的列表。

选择 <CLUSTER-NAME>-agentpool 托管标识。 在“概述”页上,复制客户端 ID 的值。 有关详细信息,请参阅管理用户分配的托管标识

屏幕截图显示托管标识的概述页上的客户端 ID。

为托管标识分配针对工作区数据收集规则的“监视指标发布者”角色

托管标识必须分配有针对与 Azure Monitor 工作区关联的数据收集规则的“监视指标发布者”角色。

  1. 在 Azure Monitor 工作区的资源菜单上,选择“概述”。 对于“数据收集规则”,请选择该链接

    屏幕截图显示与 Azure Monitor 工作区关联的数据收集规则。

  2. 在数据收集规则的资源菜单上,选择“访问控制(IAM)”

  3. 依次选择“添加”、“添加角色分配”。

    显示在“访问控制”页上添加角色分配的屏幕截图。

  4. 选择“监视指标发布者”角色,然后选择“下一步”

    显示角色分配列表的屏幕截图。

  5. 选择“托管标识”,然后选择“选择成员”。 选择包含用户分配的标识的订阅,然后选择“用户分配的托管标识”。 选择要使用的用户分配的标识,然后选择“选择”。

    屏幕截图显示如何选择用户分配的托管标识。

  6. 若要完成角色分配,请选择“查看 + 分配”。

授予 AKS 群集对托管标识的访问权限

如果使用 AKS 标识,则不需要此步骤。 AKS 标识已有访问群集的权限。

重要

要完成本部分中的步骤,须有对群集的所有者或用户访问管理员权限。

  1. 确定群集 节点资源组 中的虚拟机规模集。

    显示节点资源组中的虚拟机规模集的屏幕截图。

  2. 在 Azure CLI 中对每个虚拟机规模集运行以下命令:

    az vmss identity assign -g <NODE-RESOURCE-GROUP> -n <VMSS-NAME> --identities <USER-ASSIGNED-IDENTITY-RESOURCE-ID>
    

部署挎斗容器以设置远程写入

  1. 复制以下 YAML 并将其保存到文件中。 YAML 使用端口 8081 作为侦听端口。 如果使用其他端口,请修改 YAML 中的该端口。

    prometheus:
      prometheusSpec:
        externalLabels:
              cluster: <AKS-CLUSTER-NAME>
    
        ## https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write    
        remoteWrite:
        - url: 'http://localhost:8081/api/v1/write'
      ## Azure Managed Prometheus currently exports some default mixins in Grafana. 
      ## These mixins are compatible with Azure Monitor agent on your Azure Kubernetes Service cluster. 
      ## However, these mixins aren't compatible with Prometheus metrics scraped by the Kube Prometheus stack. 
      ## In order to make these mixins compatible, uncomment remote write relabel configuration below:
    
      ## writeRelabelConfigs:
      ##   - sourceLabels: [metrics_path]
      ##     regex: /metrics/cadvisor
      ##     targetLabel: job
      ##     replacement: cadvisor
      ##     action: replace
      ##   - sourceLabels: [job]
      ##     regex: 'node-exporter'
      ##     targetLabel: job
      ##     replacement: node
      ##     action: replace
        containers:
        - name: prom-remotewrite
          image: <CONTAINER-IMAGE-VERSION>
          imagePullPolicy: Always
          ports:
            - name: rw-port
              containerPort: 8081
          livenessProbe:
            httpGet:
              path: /health
              port: rw-port
            initialDelaySeconds: 10
            timeoutSeconds: 10
          readinessProbe:
            httpGet:
              path: /ready
              port: rw-port
            initialDelaySeconds: 10
            timeoutSeconds: 10
          env:
          - name: INGESTION_URL
            value: <INGESTION_URL>
          - name: LISTENING_PORT
            value: '8081'
          - name: IDENTITY_TYPE
            value: userAssigned
          - name: AZURE_CLIENT_ID
            value: <MANAGED-IDENTITY-CLIENT-ID>
          # Optional parameter
          - name: CLUSTER
            value: <CLUSTER-NAME>
    
  2. 替换 YAML 中的以下值:

    说明
    <AKS-CLUSTER-NAME> AKS 群集的名称。
    <CONTAINER-IMAGE-VERSION> mcr.microsoft.com/azuremonitor/containerinsights/ciprod/prometheus-remote-write/images:prom-remotewrite-20250814.1
    远程写入容器映像版本。
    <INGESTION-URL> Azure Monitor 工作区的“概述”页中“指标引入终结点”的值
    <MANAGED-IDENTITY-CLIENT-ID> 托管标识的“概述”页中的“客户端 ID”的值
    <CLUSTER-NAME> 运行 Prometheus 的群集的名称。

    重要

    对于由世纪互联云运营的 Microsoft Azure,请在 YAML 文件的 env 部分中添加以下环境变量:

    - name: INGESTION_AAD_AUDIENCE value: https://monitor.azure.cn/

  1. 使用 Helm 应用该 YAML 文件并更新 Prometheus 配置:

    # set context to your cluster 
    az aks get-credentials -g <aks-rg-name> -n <aks-cluster-name> 
    
    # use Helm to update your remote write config 
    helm upgrade -f <YAML-FILENAME>.yml prometheus prometheus-community/kube-prometheus-stack --namespace <namespace where Prometheus pod resides> 
    

验证和故障排除

有关验证和故障排除信息,请参阅 Prometheus 远程写入的 Azure Monitor 托管服务

后续步骤

验证远程写入是否正常工作

使用以下方法验证 Prometheus 数据是否正在发送到 Azure Monitor 工作区。

Kubectl 命令

使用以下命令查看挎斗容器中的日志。 如果 avgBytesPerRequestavgRequestDuration 的输出值为非零,则说明远程写入数据在传输中。

kubectl logs <Prometheus-Pod-Name> <Azure-Monitor-Side-Car-Container-Name> --namespace <namespace-where-Prometheus-is-running>
# example: kubectl logs prometheus-prometheus-kube-prometheus-prometheus-0 prom-remotewrite --namespace monitoring

此命令的输出具有以下格式:

time="2022-11-02T21:32:59Z" level=info msg="Metric packets published in last 1 minute" avgBytesPerRequest=19713 avgRequestDurationInSec=0.023 failedPublishing=0 successfullyPublished=122

使用 PromQL 的 Azure Monitor 性能指标浏览器

若要检查指标是否流向 Azure Monitor 工作区,请在 Azure 门户中的 Azure Monitor 工作区中选择“指标”。 使用指标资源管理器查询你期望从自托管 Prometheus 环境中获得的指标。 有关详细信息,请参阅指标浏览器

Azure Monitor 工作区中的 Prometheus 探索器

Prometheus 资源管理器提供了在 Azure 环境中与 Prometheus 指标交互的便捷方法,使监视和故障排除更高效。 若要使用 Prometheus 资源管理器,请转到 Azure 门户中的 Azure Monitor 工作区,然后选择“Prometheus 资源管理器”以查询你期望从自托管 Prometheus 环境中获得的指标。 有关详细信息,请参阅 Prometheus 浏览器

Grafana

在 Grafana 中使用 PromQL 查询,并验证结果是否返回预期数据。 若要详细了解如何配置适用于 Prometheus 的 Azure 托管服务的 Grafana,请参阅使用托管系统标识将适用于 Prometheus 的 Azure Monitor 托管服务用作 Grafana 的数据源