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

使用 Microsoft Entra Pod 托管标识(预览版)身份验证将 Prometheus 数据发送到 Azure Monitor

本文介绍如何使用 Microsoft Entra Pod 托管标识(预览版)身份验证为 Prometheus 的 Azure Monitor 托管服务设置远程写入。

注意

只有在 Azure Kubernetes 服务 (AKS) 群集已启用 Microsoft Entra Pod 的情况下,才应使用以下步骤设置本文中描述的远程写入挎斗容器。 Microsoft Entra Pod 托管标识已被弃用,将由 Microsoft Entra 工作负载 ID 取代。 建议使用 Microsoft Entra 工作负载 ID 身份验证。

先决条件

支持的版本

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

Azure Monitor 工作区

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

权限

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

为 Microsoft Entra Pod 托管标识设置应用程序

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

  1. 使用 Microsoft Entra ID 注册用户分配的托管标识。
  2. 将“托管标识操作员”和“虚拟机参与者”角色分配给托管标识。
  3. 将“监视指标发布者”角色分配给用户分配的托管标识。
  4. 创建 Azure 标识绑定。
  5. 将 aadpodidbinding 标签添加到 Prometheus Pod。
  6. 部署挎斗容器以设置远程写入。

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

使用 Microsoft Entra ID 对托管标识进行注册

创建用户分配的托管标识或注册现有的用户分配的托管标识。

有关创建托管标识的信息,请参阅使用托管标识身份验证为适用于 Prometheus 的 Azure Monitor 托管服务设置远程写入

将“托管标识操作员”和“虚拟机参与者”角色分配给托管标识

az role assignment create --role "Managed Identity Operator" --assignee <managed identity clientID> --scope <NodeResourceGroupResourceId> 
          
az role assignment create --role "Virtual Machine Contributor" --assignee <managed identity clientID> --scope <Node ResourceGroup Id> 

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

将“监视指标发布者”角色分配给托管标识

az role assignment create --role "Monitoring Metrics Publisher" --assignee <managed identity clientID> --scope <NodeResourceGroupResourceId> 

创建 Azure 标识绑定

用户分配的托管标识需要标识绑定才能将标识用作 Pod 托管标识。

将以下 YAML 复制到 aadpodidentitybinding.yaml 文件:


apiVersion: "aadpodidentity.k8s.io/v1" 

kind: AzureIdentityBinding 
metadata: 
name: demo1-azure-identity-binding 
spec: 
AzureIdentity: “<AzureIdentityName>” 
Selector: “<AzureIdentityBindingSelector>” 

运行以下命令:

kubectl create -f aadpodidentitybinding.yaml 

将 aadpodidbinding 标签添加到 Prometheus Pod

必须将 aadpodidbinding 标签添加到 Prometheus Pod 才能使 Pod 托管标识生效。 可以通过更新 deployment.yaml 文件或在部署挎斗容器时注入标签来添加标签,如下一部分所述。

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

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

    prometheus: 
      prometheusSpec: 
        podMetadata: 
          labels: 
            aadpodidbinding: <AzureIdentityBindingSelector> 
        externalLabels: 
          cluster: <AKS-CLUSTER-NAME> 
        remoteWrite: 
        - url: 'http://localhost:8081/api/v1/write' 
        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-20240507.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. 使用 Helm 应用该 YAML 文件并更新 Prometheus 配置:

    # set the 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>
    

验证和故障排除

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

后续步骤