你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 Microsoft Entra 身份验证将 Prometheus 数据发送到 Azure Monitor
本文介绍如何设置远程写入,以使用 Microsoft Entra 身份验证从 Azure Kubernetes 服务 (AKS) 群集或已启用 Azure Arc 的 Kubernetes 群集中运行的自托管 Prometheus 服务器发送数据。
群集配置
本文适用于以下群集配置:
- 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 驱动程序和存储。
- 部署挎斗容器以设置远程写入。
以下部分描述了这些任务。
使用 Microsoft Entra ID 注册应用程序
完成向 Microsoft Entra ID 注册应用程序并创建服务主体的步骤。
获取 Microsoft Entra 应用程序的客户端 ID
- 在 Azure 门户中,转到“Microsoft Entra ID”菜单,然后选择“应用注册”。
- 在应用程序列表中,复制已注册应用程序的“应用程序(客户端) ID”值。
为应用程序分配针对工作区数据收集规则的“监视指标发布者”角色
应用程序必须分配有针对与 Azure Monitor 工作区关联的数据收集规则的“监视指标发布者”角色。
在 Azure Monitor 工作区的资源菜单上,选择“概述”。 对于“数据收集规则”,请选择该链接。
在数据收集规则的资源菜单上,选择“访问控制(IAM)”。
依次选择“添加”、“添加角色分配”。
选择“监视指标发布者”角色,然后选择“下一步”。
选择“用户、组或服务主体”,然后选择“选择成员”。 选择你创建的应用程序,然后选择“选择”。
若要完成角色分配,请选择“查看 + 分配”。
创建 Azure 密钥保管库并生成证书
- 如果你没有 Azure 密钥保管库,请使用创建一个保管库。
- 使用将证书添加到 Key Vault 中的指南创建证书。
- 使用从 Key Vault 导出证书中的指南,以 CER 格式下载证书。
将证书添加到 Microsoft Entra 应用程序
警告
证书具有到期日期。 用户有责任使证书保持有效。
为群集添加 CSI 驱动程序和存储
注意
Azure Key Vault CSI 驱动程序配置只是在 Pod 上装载证书的方法之一。 远程写入容器只需要 Pod 中证书的本地路径,用于“部署挎斗容器以设置远程写入”步骤中的 <AZURE_CLIENT_CERTIFICATE_PATH>
值。
只有在创建群集时未启用用于机密存储 CSI 驱动程序的 Azure Key Vault 提供程序的情况下,才需要执行此步骤。
若要为群集启用用于机密存储 CSI 驱动程序的 Azure Key Vault 提供程序,请运行以下 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
的值以及要从密钥保管库中检索的对象。 有关要使用的值的信息,请参阅提供标识以访问用于机密存储 CSI 驱动程序的 Azure Key Vault 提供程序。# 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-20240507.1
远程写入容器映像版本。<INGESTION-URL>
Azure Monitor 工作区的“概述”页中“指标引入终结点”的值。 <APP-REGISTRATION -CLIENT-ID>
应用程序的客户端 ID。 <TENANT-ID>
Microsoft Entra 应用程序的租户 ID。 <CERT-NAME>
证书的名称。 <CLUSTER-NAME>
运行 Prometheus 的群集的名称。 打开 Azure Cloud Shell 并上传该 YAML 文件。
使用 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 的托管服务远程写入。