你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

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

注意

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

群集配置

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

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

注意

有关为在不同云或本地运行的 Kubernetes 群集设置远程写入的信息,请参阅使用 Microsoft Entra 身份验证将 Prometheus 数据发送到 Azure Monitor

先决条件

支持的版本

托管标识身份验证需要至少为 v2.45 或以上的 Prometheus 版本。

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-20250326.1
    远程写入容器映像版本。
    <INGESTION-URL> Azure Monitor 工作区“概述”页中“指标引入终结点”的值。
    <MANAGED-IDENTITY-CLIENT-ID> 托管标识的“概述”页中的“客户端 ID”的值
    <CLUSTER-NAME> 运行 Prometheus 的群集的名称。

    重要

    对于 Azure 政府云,在 YAML 文件的 env 部分中添加以下环境变量:

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

  3. 打开 Azure Cloud Shell 并上传该 YAML 文件。

  4. 使用 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 托管服务远程写入

后续步骤