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

使用 Microsoft Entra 工作负载 ID(预览版)身份验证将 Prometheus 数据发送到 Azure Monitor

本文介绍了如何设置远程写入,以使用 Microsoft Entra 工作负载 ID 身份验证从 Azure Monitor 管理的 Prometheus 群集发送数据。

先决条件

设置 Microsoft Entra 工作负载 ID 的工作负载

使用 Microsoft Entra 工作负载 ID 身份验证为工作负载设置 Prometheus 远程写入的过程需要完成以下任务:

  1. 设置工作负载标识。
  2. 创建一个 Microsoft Entra 应用程序或用户分配的托管标识并授予权限。
  3. 为应用程序分配针对工作区数据收集规则的“监视指标发布者”角色。
  4. 创建或更新 Kubernetes 服务帐户 Prometheus Pod。
  5. 在标识与服务帐户颁发者和使用者之间建立联合标识凭据。
  6. 部署挎斗容器以设置远程写入。

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

设置工作负载标识

若要设置工作负载标识,请导出以下环境变量:

# [OPTIONAL] Set this if you're using a Microsoft Entra application
export APPLICATION_NAME="<your application name>"
    
# [OPTIONAL] Set this only if you're using a user-assigned managed identity
export USER_ASSIGNED_IDENTITY_NAME="<your user-assigned managed identity name>"
    
# Environment variables for the Kubernetes service account and federated identity credential
export SERVICE_ACCOUNT_NAMESPACE="<namespace of Prometheus pod>"
export SERVICE_ACCOUNT_NAME="<name of service account associated with Prometheus pod>"
export SERVICE_ACCOUNT_ISSUER="<your service account issuer URL>"

对于 SERVICE_ACCOUNT_NAME,请检查是否有某个服务帐户(除了默认服务帐户之外)已与 Prometheus pod 相关联。 在 Prometheus pod 的 spec 中查找 serviceaccountNameserviceAccount(已弃用)值。 如果此值存在,则使用此值。 如果 serviceaccountNameserviceAccount 都不存在,请输入要与 Prometheus Pod 关联的服务帐户的名称。

创建一个 Microsoft Entra 应用程序或用户分配的托管标识并授予权限

创建一个 Microsoft Entra 应用程序或用户分配的托管标识,并授予将指标发布到 Azure Monitor 工作区的权限:

# create a Microsoft Entra application
az ad sp create-for-rbac --name "${APPLICATION_NAME}"

# create a user-assigned managed identity if you use a user-assigned managed identity for this article
az identity create --name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RESOURCE_GROUP}"

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

有关分配该角色的信息,请参阅为托管标识分配对工作区数据收集规则的“监视指标发布者”角色

创建或更新 Kubernetes 服务帐户 Prometheus Pod

通常情况下,将会创建一个 Kubernetes 服务帐户并将其与运行 Prometheus 容器的 Pod 相关联。 如果你使用的是 kube-prometheus 堆栈,则代码会自动创建 prometheus-kube-prometheus-prometheus 服务帐户。

如果除了默认服务帐户外没有 Kubernetes 服务帐户与 Prometheus 相关联,请专门为运行 Prometheus 的 Pod 创建一个新的服务帐户。

若要创建服务帐户,请运行以下 kubectl 命令:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: service account
metadata:
  annotations:
    azure.workload.identity/client-id: ${APPLICATION_CLIENT_ID:-$USER_ASSIGNED_IDENTITY_CLIENT_ID}
  name: ${SERVICE_ACCOUNT_NAME}
  namespace: ${SERVICE_ACCOUNT_NAMESPACE}
EOF

如果除了默认服务帐户外还存在与 Pod 关联的 Kubernetes 服务帐户,请将以下注释添加到你的服务帐户:

kubectl annotate sa ${SERVICE_ACCOUNT_NAME} -n ${SERVICE_ACCOUNT_NAMESPACE} azure.workload.identity/client-id="${APPLICATION_OR_USER_ASSIGNED_IDENTITY_CLIENT_ID}" –overwrite

如果你的 Microsoft Entra 应用程序或用户分配的托管标识与群集不在同一租户中,请将以下注释添加到你的服务帐户:

kubectl annotate sa ${SERVICE_ACCOUNT_NAME} -n ${SERVICE_ACCOUNT_NAMESPACE} azure.workload.identity/tenant-id="${APPLICATION_OR_USER_ASSIGNED_IDENTITY_TENANT_ID}" –overwrite

在标识与服务帐户颁发者和使用者之间建立联合标识凭据

使用 Azure CLI 创建联合凭据

用户分配的托管标识

az identity federated-credential create \
   --name "kubernetes-federated-credential" \
   --identity-name "${USER_ASSIGNED_IDENTITY_NAME}" \
   --resource-group "${RESOURCE_GROUP}" \
   --issuer "${SERVICE_ACCOUNT_ISSUER}" \
   --subject "system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}"

Microsoft Entra 应用程序

# Get the ObjectID of the Microsoft Entra app.

export APPLICATION_OBJECT_ID="$(az ad app show --id ${APPLICATION_CLIENT_ID} --query id -otsv)"

# Add a federated identity credential.

cat <<EOF > params.json
{
  "name": "kubernetes-federated-credential",
  "issuer": "${SERVICE_ACCOUNT_ISSUER}",
  "subject": "system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}",
  "description": "Kubernetes service account federated credential",
  "audiences": [
    "api://AzureADTokenExchange"
  ]
}
EOF

az ad app federated-credential create --id ${APPLICATION_OBJECT_ID} --parameters @params.json

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

重要

Prometheus Pod 必须具有以下标签:azure.workload.identity/use: "true"

远程写入挎斗容器需要以下环境值:

  • INGESTION_URL:Azure Monitor 工作区的“概述”页上显示的指标引入终结点
  • LISTENING_PORT8081(支持任何端口)
  • IDENTITY_TYPE: workloadIdentity
  1. 复制以下 YAML 并将其保存到文件中。 YAML 使用端口 8081 作为侦听端口。 如果使用其他端口,请修改 YAML 中的该值。

    prometheus:
      prometheusSpec:
        externalLabels:
              cluster: <AKS-CLUSTER-NAME>
        podMetadata:
            labels:
                azure.workload.identity/use: "true"
        ## https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write    
        remoteWrite:
        - url: 'http://localhost:8081/api/v1/write'
    
        containers:
        - name: prom-remotewrite
          image: <CONTAINER-IMAGE-VERSION>
          imagePullPolicy: Always
          ports:
            - name: rw-port
              containerPort: 8081
          env:
          - name: INGESTION_URL
            value: <INGESTION_URL>
          - name: LISTENING_PORT
            value: '8081'
          - name: IDENTITY_TYPE
            value: workloadIdentity
    
  2. 替换 YAML 中的以下值:

    说明
    <CLUSTER-NAME> AKS 群集的名称。
    <CONTAINER-IMAGE-VERSION> mcr.microsoft.com/azuremonitor/prometheus/promdev/prom-remotewrite:prom-remotewrite-20230906.1
    远程写入容器映像版本。
    <INGESTION-URL> Azure Monitor 工作区的“概述”页中的“指标引入终结点”的值
  3. 使用 Helm 应用该 YAML 文件并更新 Prometheus 配置:

    # set a 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 的托管服务远程写入

后续步骤