使用 Microsoft Entra 工作負載識別碼 (預覽版) 驗證將 Prometheus 資料傳送至 Azure 監視器
本文說明如何使用 Microsoft Entra 工作負載 ID 驗證,設定遠端寫入以從 Azure 監視器受控 Prometheus 叢集傳送數據。
必要條件
Microsoft Entra ID 應用程式驗證需要大於 v2.48 的 Prometheus 版本。
具有 OpenID 連線 (OIDC) 和 OIDC 簽發者 URL 專屬功能旗標的叢集:
- 如需受控叢集(Azure Kubernetes Service、Amazon Elastic Kubernetes Service 和 Google Kubernetes Engine),請參閱受控叢集 - Microsoft Entra 工作負載 ID。
- 如需自我管理叢集,請參閱自我管理叢集 - Microsoft Entra 工作負載 ID。
已安裝的變動許可 Webhook。 如需詳細資訊,請參閱變動許可 Webhook - Microsoft Entra 工作負載 ID。
在叢集中執行的 Prometheus。 本文假設 Prometheus 叢集是使用 kube-prometheus 堆棧來設定,但您可以使用其他方法來設定 Prometheus。
設定 Microsoft Entra 工作負載 ID 的工作負載
使用 Microsoft Entra 工作負載 ID 驗證來設定工作負載的 Prometheus 遠端寫入程式牽涉到完成下列工作:
- 設定工作負載身分識別。
- 建立 Microsoft Entra 應用程式或使用者指派的受控識別,並授與許可權。
- 依據工作區資料收集規則,將監視計量發行者角色指派給應用程式。
- 建立或更新 Kubernetes 服務帳戶 Prometheus Pod。
- 建立身分識別與服務帳戶簽發者和主體之間的同盟身分識別認證。
- 部署 Sidecar 容器以設定遠端寫入。
這些工作將於下列各節中說明。
設定工作負載身分識別
若要設定工作負載身分識別,請匯出下列環境變數:
# [OPTIONAL] Set this if you're using a Microsoft Entra application
export APPLICATION_NAME="<your application name>"
# [OPTIONAL] Set this only if you're using a user-assigned managed identity
export USER_ASSIGNED_IDENTITY_NAME="<your user-assigned managed identity name>"
# Environment variables for the Kubernetes service account and federated identity credential
export SERVICE_ACCOUNT_NAMESPACE="<namespace of Prometheus pod>"
export SERVICE_ACCOUNT_NAME="<name of service account associated with Prometheus pod>"
export SERVICE_ACCOUNT_ISSUER="<your service account issuer URL>"
針對 SERVICE_ACCOUNT_NAME
,請檢查服務帳戶是否已經與 Prometheus Pod 相關聯(與 預設 服務帳戶分開)。 在 Prometheus Pod 的 中spec
尋找 或 serviceAccount
的值serviceaccountName
(已淘汰)。 如果存在,請使用此值。 如果 serviceaccountName
和 serviceAccount
不存在,請輸入您想要與 Prometheus Pod 建立關聯的服務帳戶名稱。
建立 Microsoft Entra 應用程式或使用者指派的受控識別,並授與許可權
建立 Microsoft Entra 應用程式或使用者指派的受控識別,並授與將計量發佈至 Azure 監視器工作區的許可權:
# create a Microsoft Entra application
az ad sp create-for-rbac --name "${APPLICATION_NAME}"
# create a user-assigned managed identity if you use a user-assigned managed identity for this article
az identity create --name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RESOURCE_GROUP}"
將工作區數據收集規則上的監視計量發行者角色指派給應用程式或受控識別
如需指派角色的相關信息,請參閱 將工作區數據收集規則上的監視計量發行者角色指派給受控識別。
建立或更新 Kubernetes 服務帳戶 Prometheus Pod
Kubernetes 服務帳戶通常會建立並與執行 Prometheus 容器的 Pod 相關聯。 如果您使用 kube-prometheus 堆棧,程式代碼會自動建立 prometheus-kube-prometheus-prometheus 服務帳戶。
如果沒有 Kubernetes 服務帳戶,但預設服務帳戶與 Prometheus 相關聯,請特別為執行 Prometheus 的 Pod 建立新的服務帳戶。
若要建立服務帳戶,請執行下列 kubectl 命令:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: service account
metadata:
annotations:
azure.workload.identity/client-id: ${APPLICATION_CLIENT_ID:-$USER_ASSIGNED_IDENTITY_CLIENT_ID}
name: ${SERVICE_ACCOUNT_NAME}
namespace: ${SERVICE_ACCOUNT_NAMESPACE}
EOF
如果預設服務帳戶以外的 Kubernetes 服務帳戶與您的 Pod 相關聯,請將下列批注新增至您的服務帳戶:
kubectl annotate sa ${SERVICE_ACCOUNT_NAME} -n ${SERVICE_ACCOUNT_NAMESPACE} azure.workload.identity/client-id="${APPLICATION_OR_USER_ASSIGNED_IDENTITY_CLIENT_ID}" –overwrite
如果您的 Microsoft Entra 應用程式或使用者指派的受控識別不在與叢集相同的租使用者中,請將下列批注新增至您的服務帳戶:
kubectl annotate sa ${SERVICE_ACCOUNT_NAME} -n ${SERVICE_ACCOUNT_NAMESPACE} azure.workload.identity/tenant-id="${APPLICATION_OR_USER_ASSIGNED_IDENTITY_TENANT_ID}" –overwrite
建立身分識別與服務帳戶簽發者和主體之間的同盟身分識別認證
使用 Azure CLI 建立同盟認證。
使用者指派的受控識別
az identity federated-credential create \
--name "kubernetes-federated-credential" \
--identity-name "${USER_ASSIGNED_IDENTITY_NAME}" \
--resource-group "${RESOURCE_GROUP}" \
--issuer "${SERVICE_ACCOUNT_ISSUER}" \
--subject "system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}"
Microsoft Entra 應用程式
# Get the ObjectID of the Microsoft Entra app.
export APPLICATION_OBJECT_ID="$(az ad app show --id ${APPLICATION_CLIENT_ID} --query id -otsv)"
# Add a federated identity credential.
cat <<EOF > params.json
{
"name": "kubernetes-federated-credential",
"issuer": "${SERVICE_ACCOUNT_ISSUER}",
"subject": "system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}",
"description": "Kubernetes service account federated credential",
"audiences": [
"api://AzureADTokenExchange"
]
}
EOF
az ad app federated-credential create --id ${APPLICATION_OBJECT_ID} --parameters @params.json
部署 Sidecar 容器以設定遠端寫入
重要
Prometheus Pod 必須具有下列標籤: azure.workload.identity/use: "true"
遠端寫入 Sidecar 容器需要下列環境值:
INGESTION_URL
:Azure 監視器工作區 [概觀] 頁面上顯示的計量擷取端點LISTENING_PORT
:8081
(支援任何連接埠)IDENTITY_TYPE
:workloadIdentity
複製下列 YAML 並儲存至檔案。 YAML 使用埠 8081 做為接聽埠。 如果您使用不同的連接埠,請修改 YAML 中的值。
prometheus: prometheusSpec: externalLabels: cluster: <AKS-CLUSTER-NAME> podMetadata: labels: azure.workload.identity/use: "true" ## https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write remoteWrite: - url: 'http://localhost:8081/api/v1/write' containers: - name: prom-remotewrite image: <CONTAINER-IMAGE-VERSION> imagePullPolicy: Always ports: - name: rw-port containerPort: 8081 env: - name: INGESTION_URL value: <INGESTION_URL> - name: LISTENING_PORT value: '8081' - name: IDENTITY_TYPE value: workloadIdentity
取代 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 監視器工作區的 [概觀] 頁面擷取計量擷取端點的值。 使用 Helm 套用 YAML 檔案,以更新 Prometheus 設定:
# set a 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 監視器受控服務進行疑難解答。