Share via


使用 Microsoft Entra 驗證將 Prometheus 資料傳送至 Azure 監視器

本文說明如何使用 Microsoft Entra 驗證,設定 遠端寫入 ,以從 Azure Kubernetes Service (AKS) 叢集中執行的自我管理 Prometheus 伺服器或已啟用 Azure Arc 的 Kubernetes 叢集傳送數據。

叢集組態

本文適用於下列叢集組態:

  • Azure Kubernetes Service 叢集
  • 已啟用 Azure Arc 的 Kubernetes 叢集
  • 在不同的雲端或內部部署中執行的 Kubernetes 叢集

注意

針對 AKS 叢集或已啟用 Azure Arc 的 Kubernetes 叢集,建議您使用受控識別驗證。 如需詳細資訊,請參閱 適用於 Prometheus 遠端寫入受控識別的 Azure 監視器受控服務。

必要條件

支援的版本

  • Microsoft Entra ID 應用程式驗證需要大於 v2.48 的 Prometheus 版本。

Azure 監視器工作區

本文涵蓋將 Prometheus 計量傳送至 Azure 監視器工作區。 若要建立 Azure 監視器工作區,請參閱 管理 Azure 監視器工作區

權限

需要叢集或資源的 管理員 istrator 許可權,才能完成本文中的步驟。

設定 Microsoft Entra 識別碼的應用程式

使用 Microsoft Entra 驗證為應用程式設定 Prometheus 遠端寫入的程序包括完成下列工作:

  1. 使用 Microsoft Entra ID 註冊應用程式。
  2. 取得 Microsoft Entra 應用程式的用戶端識別碼。
  3. 依據工作區資料收集規則,將監視計量發行者角色指派給應用程式。
  4. 建立 Azure 金鑰保存庫併產生憑證。
  5. 將憑證新增至 Microsoft Entra 應用程式。
  6. 新增叢集的 CSI 驅動程式和記憶體。
  7. 部署 Sidecar 容器以設定遠端寫入。

這些工作將於下列各節中說明。

使用 Microsoft Entra ID 註冊應用程式

完成使用 Microsoft Entra ID 註冊應用程式中的步驟,並建立服務主體。

取得 Microsoft Entra 應用程式的用戶端識別碼

  1. 在 Azure 入口網站中,移至 [Microsoft Entra ID] 功能表,然後選取 [應用程式註冊]
  2. 在應用程式清單中,複製已註冊應用程式的 [應用程式 (用戶端) 識別碼] 值。

顯示 Microsoft Entra 應用程式之應用程式或用戶端識別碼的螢幕快照。

將工作區數據收集規則上的監視計量發行者角色指派給應用程式

依據與 Azure 監視器工作區相關聯的資料收集規則,必須將監視計量發行者角色指派給應用程式。

  1. 在 Azure 監視器工作區的資源功能表上,選取 [概觀]。 在 [資料收集規則] 中,選取連結。

    此螢幕快照顯示 Azure 監視器工作區所使用的數據收集規則。

  2. 在資料收集規則的資源功能表上,選取 [存取控制 (IAM)]

  3. 選取 [新增],然後選取 [新增角色指派]。

    顯示在訪問控制頁面上新增角色指派的螢幕快照。

  4. 選取 [監視計量發行者] 角色,然後選取 [下一步]

    顯示角色指派清單的螢幕快照。

  5. 選取 [使用者、群組或服務主體],然後選擇 [選取成員]。 選取您建立的應用程式,然後選擇 [選取]

    顯示選取應用程式的螢幕快照。

  6. 若要完成角色指派,請選取 [檢閱 + 指派]

建立 Azure 金鑰保存庫併產生憑證

  1. 如果您還沒有 Azure 金鑰保存庫, 請建立保存庫
  2. 使用將憑證新增至 金鑰保存庫 中的指引來建立憑證。
  3. 使用從 金鑰保存庫 匯出憑證中的指引,下載 CER 格式的憑證。

將憑證新增至 Microsoft Entra 應用程式

  1. 在 Microsoft Entra 應用程式的資源功能表上,選取 [ 憑證與秘密]。

  2. 在 [ 憑證] 索引標籤上,選取 [上傳憑證 ],然後選取您下載的憑證。

    顯示上傳 Microsoft Entra 應用程式的憑證的螢幕快照。

警告

憑證有到期日。 用戶有責任讓憑證保持有效。

新增叢集的 CSI 驅動程式和記憶體

注意

Azure 金鑰保存庫 CSI 驅動程式設定只是在 Pod 上掛接憑證的其中一種方式。 遠端寫入容器只需要 Pod 中憑證的本機路徑,才能在<AZURE_CLIENT_CERTIFICATE_PATH>部署 Sidecar 容器以設定遠端寫入步驟中的值。

只有在您建立叢集時未開啟 Azure 金鑰保存庫 Provider for Secrets Store CSI Driver 時,才需要此步驟。

  1. 若要開啟適用於叢集的 Azure 金鑰保存庫 提供者 CSI 驅動程式,請執行下列 Azure CLI 命令:

    az aks enable-addons --addons azure-keyvault-secrets-provider --name <aks-cluster-name> --resource-group <resource-group-name>
    
  2. 若要提供金鑰保存庫的身分識別存取權,請執行下列命令:

    # 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>
    
  3. 將下列 YAML 儲存至名為 secretproviderclass.yml檔案,以建立 SecretProviderClass 。 取代、keyvaultNametenantId和 要從金鑰保存庫擷取的物件值userAssignedIdentityID。 如需要使用哪些值的資訊,請參閱提供身分識別來存取 Azure 金鑰保存庫 Provider for Secrets Store CSI Driver

    # 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
    
  4. 在叢集上執行下列命令來套用 SecretProviderClass

    kubectl apply -f secretproviderclass.yml
    

部署 Sidecar 容器以設定遠端寫入

  1. 複製下列 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>'
    
  2. 取代 YAML 檔案中的下列值:

    Description
    <CLUSTER-NAME> AKS 叢集的名稱。
    <CONTAINER-IMAGE-VERSION> mcr.microsoft.com/azuremonitor/containerinsights/ciprod/prometheus-remote-write/images:prom-remotewrite-20240507.1
    遠端寫入容器映像版本。
    <INGESTION-URL> Azure 監視器工作區的 [概觀] 頁面擷取計量擷取端點的值
    <APP-REGISTRATION -CLIENT-ID> 應用程式的用戶端識別碼。
    <TENANT-ID> Microsoft Entra 應用程式的租用戶標識碼。
    <CERT-NAME> 憑證的名稱。
    <CLUSTER-NAME> Prometheus 正在執行的叢集名稱。
  3. 開啟 Azure Cloud Shell 並上傳 YAML 檔案。

  4. 使用 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> 
    

驗證與疑難排解

如需驗證和疑難解答資訊,請參閱針對 Prometheus 遠端寫入的遠端寫入和 Azure 監視器受控服務進行疑難解答。

下一步