重要
本文介绍如何使用 Entra ID 身份验证和 Azure Monitor 提供的 sidecar 容器,在 Azure Monitor 托管的 Prometheus 服务中设置远程写入。 若要在不使用 sidecar 的情况下使用远程写入与 Entra ID 配合,请参阅 将自主管理的 Prometheus 连接到 Azure Monitor 的 Prometheus 托管服务。 自 2027 年 3 月 31 日起,Azure Monitor将弃用基于 sidecar 的远程写入解决方案,以便将 Prometheus 指标发送到Azure Monitor工作区。
本文介绍如何设置 remote write,以通过使用 Microsoft Entra 身份验证和 Azure Monitor 提供的 sidecar 容器,从在 Azure Kubernetes 服务 (AKS) 群集或启用 Azure Arc 的 Kubernetes 集群中运行的自管理 Prometheus 服务器发送数据。 还可以直接在 Prometheus 配置中配置远程写入功能。
注意
配置在 Kubernetes 集群上运行的 Prometheus,以便直接远程写入 Azure Monitor 工作区。 有关详细信息,请参阅 使用 Microsoft Entra ID 身份验证将 Prometheus 数据发送到 Azure Monitor。 本文中的步骤使用 Azure Monitor sidecar 容器,该容器在 2027 年 3 月 31 日之后将不再受支持。
群集配置
本文适用于以下群集配置:
- Azure Kubernetes 服务群集
- 已启用 Azure Arc 的 Kubernetes 群集
- 在不同云或本地运行的 Kubernetes 群集
注意
对于 AKS 群集或已启用 Azure Arc 的 Kubernetes 群集,建议使用托管标识身份验证。 有关详细信息,请参阅适用于 Prometheus 的 Azure Monitor 托管服务的托管标识远程写入。
先决条件
支持的版本
- Microsoft Entra ID 应用程序身份验证需要高于 v2.48 的 Prometheus 版本。
Azure Monitor 工作区
本文介绍如何将 Prometheus 指标发送到 Azure Monitor 工作区。 若要创建 Azure Monitor 工作区,请参阅管理 Azure Monitor 工作区。
权限
完成本文中的步骤需要群集或资源的管理员权限。
为 Microsoft Entra ID 设置应用程序
使用 Microsoft Entra 身份验证为应用程序设置 Prometheus 远程写入的过程涉及到以下任务的完成:
- 使用 Microsoft Entra ID 注册应用程序。
- 获取 Microsoft Entra 应用程序的客户端 ID。
- 为应用程序分配针对工作区数据收集规则的“监视指标发布者”角色。
- 创建 Azure 密钥保管库并生成证书。
- 将证书添加到 Microsoft Entra 应用程序。
- 为群集添加 CSI 驱动程序和存储。
- 部署sidecar容器以配置远程写入。
以下部分描述了这些任务。
使用 Microsoft Entra ID 注册应用程序
完成向 Microsoft Entra ID 注册应用程序并创建服务主体的步骤。
获取 Microsoft Entra 应用程序的客户端 ID
- 在 Azure 门户中,转到“Microsoft Entra ID”菜单,然后选择“应用注册”。
- 在应用程序列表中,复制已注册应用程序的“应用程序(客户端) ID”值。
为应用程序分配针对工作区数据收集规则的“监视指标发布者”角色
应用程序必须分配有针对与 Azure Monitor 工作区关联的数据收集规则的“监控指标发布者”角色。
在 Azure Monitor 工作区的资源菜单上,选择“概述”。 对于数据收集规则,请选择该链接。
在数据收集规则的资源菜单上,选择“访问控制(IAM)”。
选择添加,然后选择添加角色分配。
选择“监视指标发布者”角色,然后选择“下一步”。
选择“用户、组或服务主体”,然后选择“选择成员”。 选择你创建的应用程序,然后选择“选择”。
若要完成角色分配,请选择“查看 + 分配”。
创建 Azure 密钥保管库并生成证书
- 如果你没有 Azure 密钥保管库,请使用创建一个保管库。
- 使用将证书添加到 密钥保管库 中的指南创建证书。
- 使用从 密钥保管库 导出证书中的指南,以 CER 格式下载证书。
将证书添加到 Microsoft Entra 应用程序
警告
证书具有到期日期。 用户有责任使证书保持有效。
为群集添加 CSI 驱动程序和存储
注意
Azure 密钥保管库 CSI 驱动程序配置只是在 Pod 上装载证书的方法之一。 远程写入容器仅在步骤“部署一个边车容器来设置远程写入”中需要 Pod 中证书的本地路径。
只有在创建群集时未启用用于机密存储 CSI 驱动程序的 Azure 密钥保管库 提供程序的情况下,才需要执行此步骤。
若要为群集启用用于机密存储 CSI 驱动程序的 Azure 密钥保管库 提供程序,请运行以下 Azure CLI 命令:
az aks enable-addons --addons azure-keyvault-secrets-provider --name <aks-cluster-name> --resource-group <resource-group-name>若要授予对密钥保管库的标识访问权限,请运行以下命令:
# show client id of the managed identity of the cluster az aks show -g <resource-group> -n <cluster-name> --query addonProfiles.azureKeyvaultSecretsProvider.identity.clientId -o tsv # set policy to access keys in your key vault az keyvault set-policy -n <keyvault-name> --key-permissions get --spn <identity-client-id> # set policy to access secrets in your key vault az keyvault set-policy -n <keyvault-name> --secret-permissions get --spn <identity-client-id> # set policy to access certs in your key vault az keyvault set-policy -n <keyvault-name> --certificate-permissions get --spn <identity-client-id>通过将以下 YAML 保存到名为 secretproviderclass.yml 的文件来创建
SecretProviderClass。 替换userAssignedIdentityID、keyvaultName、tenantId的值以及要从密钥保管库中检索的对象。 有关应使用哪些值的信息,请参阅使用 Azure 密钥保管库 提供程序为 Secrets Store CSI 驱动程序提供访问标识。# This is a SecretProviderClass example using user-assigned identity to access your key vault apiVersion: secrets-store.csi.x-k8s.io/v1 kind: SecretProviderClass metadata: name: azure-kvname-user-msi spec: provider: azure parameters: usePodIdentity: "false" useVMManagedIdentity: "true" # Set to true for using managed identity userAssignedIdentityID: <client-id> # Set the client ID of the user-assigned managed identity to use keyvaultName: <key-vault-name> # Set to the name of your key vault cloudName: "" # [OPTIONAL for Azure] if not provided, the Azure environment defaults to AzurePublicCloud objects: | array: - | objectName: <name-of-cert> objectType: secret # object types: secret, key, or cert objectFormat: pfx objectEncoding: base64 objectVersion: "" tenantId: <tenant-id> # The tenant ID of the key vault通过对群集运行以下命令来应用
SecretProviderClass:kubectl apply -f secretproviderclass.yml
部署旁车容器以设置远程写入
复制以下 YAML 并将其保存到文件中。 YAML 使用端口 8081 作为侦听端口。 如果使用其他端口,请修改 YAML 中的该值。
prometheus: prometheusSpec: externalLabels: cluster: <CLUSTER-NAME> ## Azure Managed Prometheus currently exports some default mixins in Grafana. ## These mixins are compatible with data scraped by Azure Monitor agent on your ## Azure Kubernetes Service cluster. These mixins aren't compatible with Prometheus ## metrics scraped by the Kube Prometheus stack. ## To make these mixins compatible, uncomment the 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 ## https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write remoteWrite: - url: 'http://localhost:8081/api/v1/write' # Additional volumes on the output StatefulSet definition. # Required only for Microsoft Entra ID based auth volumes: - name: secrets-store-inline csi: driver: secrets-store.csi.k8s.io readOnly: true volumeAttributes: secretProviderClass: azure-kvname-user-msi containers: - name: prom-remotewrite image: <CONTAINER-IMAGE-VERSION> imagePullPolicy: Always # Required only for Microsoft Entra ID based auth volumeMounts: - name: secrets-store-inline mountPath: /mnt/secrets-store readOnly: true 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: aadApplication - name: AZURE_CLIENT_ID value: '<APP-REGISTRATION-CLIENT-ID>' - name: AZURE_TENANT_ID value: '<TENANT-ID>' - name: AZURE_CLIENT_CERTIFICATE_PATH value: /mnt/secrets-store/<CERT-NAME> - name: CLUSTER value: '<CLUSTER-NAME>'替换 YAML 文件中的以下值:
值 说明 <CLUSTER-NAME>AKS 群集的名称。 <CONTAINER-IMAGE-VERSION>mcr.microsoft.com/azuremonitor/containerinsights/ciprod/prometheus-remote-write/images:prom-remotewrite-20260414.1
远程写入容器映像版本。<INGESTION-URL>Azure Monitor 工作区的“概述”页中“指标引入终结点”的值。 <APP-REGISTRATION -CLIENT-ID>应用程序的客户端 ID。 <TENANT-ID>Microsoft Entra 应用程序的租户 ID。 <CERT-NAME>证书的名称。 <CLUSTER-NAME>Prometheus 运行所在群集的名称。 使用 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 远程写入托管服务。