使用 Microsoft Entra Pod 受控識別 (預覽版) 驗證將 Prometheus 資料傳送至 Azure 監視器

本文說明如何使用 Microsoft Entra Pod 受控識別(預覽版) 驗證,為 Prometheus 設定適用於 Prometheus 的 Azure 監視器受控服務遠端寫入。

注意

本文所述的遠端寫入 Sidecar 容器應該只使用下列步驟來設定,且只有在 Azure Kubernetes Service (AKS) 叢集已啟用 Microsoft Entra Pod 時才設定。 Microsoft Entra Pod 受控識別已被取代為 Microsoft Entra Workload ID。 建議您使用 Microsoft Entra 工作負載識別碼驗證。

必要條件

支援的版本

受控識別驗證需要大於 v2.45 的 Prometheus 版本。

Azure 監視器工作區

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

權限

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

設定 Microsoft Entra Pod 受控識別的應用程式

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

  1. 使用 Microsoft Entra ID 註冊使用者指派的受控識別。
  2. 將受控識別操作員和虛擬機參與者角色指派給受控識別。
  3. 指派監視計量發行者角色至使用者指派的受控識別。
  4. 建立 Azure AD 身分識別繫結。
  5. 將 aadpodidbinding 標籤新增至 Prometheus Pod。
  6. 部署 Sidecar 容器以設定遠端寫入。

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

使用 Microsoft Entra ID 驗證受控註冊

建立使用者指派的受控識別,或註冊現有的使用者指派受控識別。

如需建立受控是別的資訊,請參閱使用受控識別驗證為 Prometheus 設定適用於 Azure 監視器受控服務的遠端寫入

將受控識別操作員和虛擬機參與者角色指派給受控識別

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 AD 身分識別繫結

使用者指派的受控識別需要身分識別繫結,才能將身分識別當做 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 檔案,或在部署 Sidecar 容器時插入標籤,以新增標籤,如下一節所述。

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

  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 中的下列值:

    Description
    <AKS-CLUSTER-NAME> AKS 叢集的名稱。
    <CONTAINER-IMAGE-VERSION> mcr.microsoft.com/azuremonitor/containerinsights/ciprod/prometheus-remote-write/images:prom-remotewrite-20240507.1
    這是遠端寫入容器映像版本。
    <INGESTION-URL> Azure 監視器工作區的 [概觀] 頁面中的 [計量擷取端點] 的值。
    <MANAGED-IDENTITY-CLIENT-ID> 受控識別的 [概觀] 頁面中 [用戶端識別碼] 的值。
    <CLUSTER-NAME> Prometheus 執行所在的叢集名稱。

    重要

    針對 Azure Government 雲端,請在 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>
    

驗證與疑難排解

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

下一步