Share via


Microsoft Entra 授權 Proxy

Microsoft Entra 授權 Proxy 是反向 Proxy,可用來使用 Microsoft Entra 是別碼來驗證要求。 此 Proxy 可用來驗證支援 Microsoft Entra 驗證的任何服務要求。 使用此 Proxy 來驗證對 Prometheus 的 Azure 監視器受控服務的要求。

必要條件

  • Azure 監視器工作區。 如果您沒有工作區,請使用 Azure 入口網站建立一個。
  • 安裝在叢集上的 Prometheus。

注意

本文中的遠端寫入範例會使用 Prometheus 遠端寫入將資料寫入 Azure 監視器。 將 AKS 叢集上線至 Prometheus 會自動在您的叢集上安裝 Prometheus,並將資料傳送至您的工作區。

部署

您可以使用發行映像或 helm 圖表,使用自訂範本來部署 Proxy。 這兩個部署都包含相同的可自訂參數。 參數資料表將說明這些參數。

如需詳細資訊,請參閱 Microsoft Entra 驗證 Proxy 專案。

下列範例示範如何部署 Proxy 以進行遠端寫入,以及從 Azure 監視器查詢資料。

注意

此範例示範如何使用 Proxy 來驗證對適用於 Prometheus 的 Azure 監視器受控服務進行遠端寫入的要求。 Prometheus 遠端寫入有專用的遠端撰寫側車,這是實作遠端寫入的建議方法。

部署 Proxy 之前,請尋找您的受控識別,並為其指派 Azure 監視器工作區資料收集規則的 Monitoring Metrics Publisher 角色。

  1. 針對您的 AKS 叢集受控識別找出 clientId。 受控識別可用來向 Azure 監視器工作區進行驗證。 建立 AKS 叢集時會建立受控識別。

    # Get the identity client_id
    az aks show -g <AKS-CLUSTER-RESOURCE-GROUP> -n <AKS-CLUSTER-NAME> --query "identityProfile"
    

    輸出具備下列格式:

    {
      "kubeletidentity": {
        "clientId": "abcd1234-1243-abcd-9876-1234abcd5678",
        "objectId": "12345678-abcd-abcd-abcd-1234567890ab",
        "resourceId": "/subscriptions/def0123-1243-abcd-9876-1234abcd5678/resourcegroups/MC_rg-proxytest-01_proxytest-01_eastus/providers/Microsoft.ManagedIdentity/userAssignedIdentities/proxytest-01-agentpool"
      }
    
  2. 尋找 Azure 監視器工作區的資料收集規則 (DCR) 識別碼。
    規則名稱與工作區名稱相同。 資料收集規則的資源群組名稱會遵循格式:MA_<workspace-name>_<REGION>_managed,例如 MA_amw-proxytest_eastus_managed。 使用下列命令來尋找資料收集規則識別碼:

    az monitor data-collection rule show --name <dcr-name> --resource-group <resource-group-name> --query "id"
    
  3. 或者,您也可以使用 Azure 監視器工作區概觀頁面上的 Azure 入口網站,找到 DCR 標識符和計量擷取端點。

    選取工作區 [概觀] 索引標籤上的 [資料收集規則],然後選取 [JSON 檢視] 以查看 [資源識別符]

    A screenshot showing the overview page for an Azure Monitor workspace.

  4. Monitoring Metrics Publisher 角色指派給受控識別的 clientId,以便寫入 Azure 監視器工作區資料收集規則。

    az role assignment create /
    --assignee <clientid>  /
    --role "Monitoring Metrics Publisher" /
    --scope <workspace-dcr-id>
    

    例如:

    az role assignment create \
    --assignee abcd1234-1243-abcd-9876-1234abcd5678  \
    --role "Monitoring Metrics Publisher" \
    --scope /subscriptions/ef0123-1243-abcd-9876-1234abcd5678/resourceGroups/MA_amw-proxytest_eastus_managed/providers/Microsoft.Insights/dataCollectionRules/amw-proxytest
    
  5. 使用下列 YAML 檔案來部署 Proxy 以進行遠端寫入。 修改下列參數:

    • TARGET_HOST - 您要轉送要求的目標主機。 若要將資料傳送至 Azure 監視器工作區,請使用工作區概觀頁面中 Metrics ingestion endpoint 的主機名部分。 例如,http://amw-proxytest-abcd.eastus-1.metrics.ingest.monitor.azure.com
    • AAD_CLIENT_ID - 已指派 Monitoring Metrics Publisher 角色受控識別的 clientId
    • AUDIENCE - 若要將計量擷取至 Azure 監視器工作區,請將 AUDIENCE 設定為 https://monitor.azure.com/.default
    • 如果您未使用 OpenTelemetry,請移除 OTEL_GRPC_ENDPOINTOTEL_SERVICE_NAME

    如需參數的詳細資訊,請參閱資料表中的參數

    proxy-ingestion.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
        labels:
            app: azuremonitor-ingestion
        name: azuremonitor-ingestion
        namespace: observability
    spec:
        replicas: 1
        selector:
            matchLabels:
                app: azuremonitor-ingestion
        template:
            metadata:
                labels:
                    app: azuremonitor-ingestion
                name: azuremonitor-ingestion
            spec:
                containers:
                - name: aad-auth-proxy
                  image: mcr.microsoft.com/azuremonitor/auth-proxy/prod/aad-auth-proxy/images/aad-auth-proxy:0.1.0-main-05-24-2023-b911fe1c
                  imagePullPolicy: Always
                  ports:
                  - name: auth-port
                    containerPort: 8081
                  env:
                  - name: AUDIENCE
                    value: https://monitor.azure.com/.default
                  - name: TARGET_HOST
                    value: http://<workspace-endpoint-hostname>
                  - name: LISTENING_PORT
                    value: "8081"
                  - name: IDENTITY_TYPE
                    value: userAssigned
                  - name: AAD_CLIENT_ID
                    value: <clientId>
                  - name: AAD_TOKEN_REFRESH_INTERVAL_IN_PERCENTAGE
                    value: "10"
                  - name: OTEL_GRPC_ENDPOINT
                    value: <YOUR-OTEL-GRPC-ENDPOINT> # "otel-collector.observability.svc.cluster.local:4317"
                  - name: OTEL_SERVICE_NAME
                    value: <YOUE-SERVICE-NAME>
                  livenessProbe:
                    httpGet:
                      path: /health
                      port: auth-port
                    initialDelaySeconds: 5
                    timeoutSeconds: 5
                  readinessProbe:
                    httpGet:
                      path: /ready
                      port: auth-port
                    initialDelaySeconds: 5
                    timeoutSeconds: 5
    ---
    apiVersion: v1
    kind: Service
    metadata:
        name: azuremonitor-ingestion
        namespace: observability
    spec:
        ports:
            - port: 80
              targetPort: 8081
        selector:
            app: azuremonitor-ingestion
    
  6. 使用命令部署 Proxy:

    # create the namespace if it doesn't already exist
    kubectl create namespace observability 
    
    kubectl apply -f proxy-ingestion.yaml -n observability
    
  7. 或者,您可以使用 helm 部署 Proxy,如下所示:

    helm install aad-auth-proxy oci://mcr.microsoft.com/azuremonitor/auth-proxy/prod/aad-auth-proxy/helmchart/aad-auth-proxy \
    --version 0.1.0-main-05-24-2023-b911fe1c \
    -n observability \
    --set targetHost=https://proxy-test-abc123.eastus-1.metrics.ingest.monitor.azure.com \
    --set identityType=userAssigned \
    --set aadClientId= abcd1234-1243-abcd-9876-1234abcd5678 \
    --set audience=https://monitor.azure.com/.default
    
  8. 設定遠端寫入 url。
    URL 主機名是由下列格式 <ingestion service name>.<namespace>.svc.cluster.local 的擷取服務名稱和命名空間所組成。 在此範例中,成本是 azuremonitor-ingestion.observability.svc.cluster.local
    使用 Azure 監視器工作區 [概觀] 頁面 Metrics ingestion endpoint 的路徑來設定 URL 路徑。 例如: dataCollectionRules/dcr-abc123d987e654f3210abc1def234567/streams/Microsoft-PrometheusMetrics/api/v1/write?api-version=2021-11-01-preview

    prometheus:
      prometheusSpec:
        externalLabels:
          cluster: <cluster name to be used in the workspace>
        ## https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write
        ##
        remoteWrite:
        - url: "http://azuremonitor-ingestion.observability.svc.cluster.local/dataCollectionRules/dcr-abc123d987e654f3210abc1def234567/streams/Microsoft-PrometheusMetrics/api/v1/write?api-version=2021-11-01-preview" 
    
  9. 套用遠端寫入組態。

注意

如需最新 Proxy 映像版本,請參閱版本資訊

檢查 Proxy 是否擷取資料

藉由檢查 Pod 的記錄,或查詢 Azure 監視器工作區,檢查 Proxy 是否成功擷取計量。

執行下列命令來檢查狀態檢查 Pod 記錄:

# Get the azuremonitor-ingestion pod ID
 kubectl get pods -A | grep azuremonitor-ingestion
 #Using the returned pod ID, get the logs
 kubectl logs --namespace observability <pod ID> --tail=10

成功擷取計量會產生 StatusCode=200 的記錄,與下方類似:

time="2023-05-16T08:47:27Z" level=info msg="Successfully sent request, returning response back." ContentLength=0 Request="https://amw-proxytest-05-t16w.eastus-1.metrics.ingest.monitor.azure.com/dataCollectionRules/dcr-688b6ed1f2244e098a88e32dde18b4f6/streams/Microsoft-PrometheusMetrics/api/v1/write?api-version=2021-11-01-preview" StatusCode=200

若要查詢 Azure 監視器工作區,請遵循下列步驟:

  1. 從您的 Azure 監視器工作區中,選取 [活頁簿]

  2. 選取 Prometheus Explorer 圖格。 A screenshot showing the workbooks gallery for an Azure Monitor workspace.

  3. 在總管頁面上,在查詢方塊中輸入 up

  4. 選取 [格線] 索引標籤以查看結果。

  5. 檢查 [叢集] 資料行,以查看是否顯示叢集。 A screenshot showing the Prometheus explorer query page.

參數

映像參數 Helm 圖表參數名稱 描述 支援值 必要
TARGET_HOST targetHost 您要轉送要求的目標主機。
將資料傳送至 Azure 監視器工作區時,請使用工作區概觀頁面中的 Metrics ingestion endpoint
從 Azure 監視器工作區讀取資料時,請使用工作區概觀頁面中的 Query endpoint
Yes
IDENTITY_TYPE identityType 用來驗證要求的識別類型。 此 Proxy 支援三種類型的身分識別。 systemassigneduserassignedaadapplication Yes
AAD_CLIENT_ID aadClientId 使用的身分識別的用戶端識別碼。 這是用於 userassignedaadapplication 身分識別類型。 使用 az aks show -g <AKS-CLUSTER-RESOURCE-GROUP> -n <AKS-CLUSTER-NAME> --query "identityProfile" 擷取用戶端識別碼 適用於 userassignedaadapplication
AAD_TENANT_ID aadTenantId 使用的身分識別的租用戶端識別碼。 租用戶識別碼用於 aadapplication 身分識別類型。 適用於 aadapplication
AAD_CLIENT_CERTIFICATE_PATH aadClientCertificatePath Proxy 可找到 aadapplication 憑證的路徑。 此路徑應該可透過 Proxy 存取,而且應該是包含私密金鑰的 pfx 或 pem 憑證。 僅限 aadapplication 識別類型
AAD_TOKEN_REFRESH_INTERVAL_IN_PERCENTAGE aadTokenRefreshIntervalInMinutes 權杖會根據權杖到期前的時間百分比重新整理。 預設值為到期前 10% 的時間。 No
AUDIENCE audience 權杖的對象 No
LISTENING_PORT listeningPort 在此連接埠上接聽的 Proxy Yes
OTEL_SERVICE_NAME otelServiceName OTEL 追蹤和計量的服務名稱。 預設值:aad_auth_proxy No
OTEL_GRPC_ENDPOINT otelGrpcEndpoint Proxy 會將 OTEL 遙測推送至此端點。 預設值:http://localhost:4317 No

疑難排解

  • Proxy 容器不會啟動。
    執行下列命令以顯示 Proxy 容器的任何錯誤。

    kubectl --namespace <Namespace> describe pod <Prometheus-Pod-Name>`
    
  • Proxy 未啟動 - 設定錯誤

    Proxy 會檢查有效的身分識別,以在啟動期間擷取權杖。 如果無法擷取權杖,啟動就會失敗。 您可以執行下列命令來檢視記錄的錯誤:

    kubectl --namespace <Namespace> logs <Proxy-Pod-Name>
    

    範例輸出:

    time="2023-05-15T11:24:06Z" level=info msg="Configuration settings loaded:" AAD_CLIENT_CERTIFICATE_PATH= AAD_CLIENT_ID=abc123de-be75-4141-a1e6-abc123987def AAD_TENANT_ID= AAD_TOKEN_REFRESH_INTERVAL_IN_PERCENTAGE=10 AUDIENCE="https://prometheus.monitor.azure.com" IDENTITY_TYPE=userassigned LISTENING_PORT=8082 OTEL_GRPC_ENDPOINT= OTEL_SERVICE_NAME=aad_auth_proxy TARGET_HOST=proxytest-01-workspace-orkw.eastus.prometheus.monitor.azure.com
    2023-05-15T11:24:06.414Z [ERROR] TokenCredential creation failed:Failed to get access token: ManagedIdentityCredential authentication failed
    GET http://169.254.169.254/metadata/identity/oauth2/token
    --------------------------------------------------------------------------------
    RESPONSE 400 Bad Request
    --------------------------------------------------------------------------------
    {
      "error": "invalid_request",
      "error_description": "Identity not found"
    }
    --------------------------------------------------------------------------------