共用方式為


教學課程:使用受控私人端點連線到 AKS 叢集上的自我裝載 Prometheus 服務

本指南會逐步引導您在 Azure Kubernetes Service (AKS) 叢集上安裝 Prometheus (開放原始碼監視和警示工具組)。 然後,使用 Azure 受控 Grafana 的受控私人端點來連線到此 Prometheus 伺服器,並在 Grafana 儀表板中顯示 Prometheus 資料。

在本教學課程中,您會了解如何:

  • 建立 Azure Kubernetes Service 叢集
  • 安裝 Prometheus
  • 將私人連結服務新增至 Prometheus 伺服器
  • 使用受控私人端點進行連線
  • 在 Grafana 儀表板中顯示 Prometheus 資料

必要條件

開始之前,請確定您具備下列項目:

建立 Azure Kubernetes Service 叢集

  1. 執行 az login 命令來登入 Azure CLI。

    az login
    

    如果您有多個 Azure 訂用帳戶,請使用命令 az account set -s <your-azure-subscription-id> 選取您的 Azure 訂用帳戶。

  2. 安裝或更新 kubectl。

    az aks install-cli
    
  3. 建立兩個 bash/zsh 變數,我們將在後續命令中使用這些變數。 如果您是使用另一個殼層,請變更下列語法。

    RESOURCE_GROUP=myResourceGroup 
    AKS_NAME=myaks
    
  4. 建立資源群組。 在此範例中,我們會在美國中西部 Azure 區域中建立資源群組。

    az group create --name $RESOURCE_GROUP --location westcentralus
    
  5. 使用 az aks create 命令來建立新的 AKS 叢集。 在這裡,我們會使用 B 系列可高載虛擬機器類型建立三節點叢集,此類型符合成本效益,並適用於這類小型測試/開發工作負載。

    az aks create --resource-group $RESOURCE_GROUP \
      --name $AKS_NAME \
      --node-count 3 \
      --node-vm-size Standard_B2s \
      --generate-ssh-keys
    

    此作業可能需要幾分鐘才能完成。

  6. 向您所建立的叢集進行驗證。

    az aks get-credentials \
      --resource-group $RESOURCE_GROUP \
      --name $AKS_NAME
    

    您現在可以使用 kubectl 存取 Kubernetes 叢集。

  7. 使用 kubectl 查看您已建立的節點。

    kubectl get nodes
    

安裝 Prometheus

安裝 Prometheus 的熱門方式是透過 prometheus-operator,其會提供 Prometheus 的 Kubernetes 原生部署和管理,以及相關監視元件。 在本教學課程中,我們使用 kube-prometheus-stack Helm 圖表,來部署 prometheus-operator。

  1. 新增 helm-charts 存放庫,然後更新您的存放庫清單。

    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm repo update
    
  2. 將 Helm 圖表安裝至稱為監視的命名空間。 此命名空間是自動建立的。

    helm install prometheus \
      prometheus-community/kube-prometheus-stack \
      --namespace monitoring \
      --create-namespace
    
  3. Helm 命令會提示您檢查所部署 Pod 的狀態。 執行下列命令。

    kubectl --namespace monitoring get pods
    
  4. 繼續之前,請確定 Pod 全部「執行中」。 如果在不太可能的情況下,它們沒有達到執行中狀態,您可能會想要針對它們進行疑難排解。

Azure Private Link 服務可讓您跨不同的 Azure 虛擬網路透過私人連結來取用 Kubernetes 服務。 AKS 具有與 Azure Private Link Service 的原生整合,並可協助您標註 Kubernetes 服務物件,以在 Azure 內建立對應的私人連結服務。

請參閱下列 pls-prometheus-svc.yaml 檔案的內容:

apiVersion: v1
kind: Service
metadata:
  name: prom-pls-svc
  annotations:
    service.beta.kubernetes.io/azure-load-balancer-internal: "true" # Use an internal LB with PLS
    service.beta.kubernetes.io/azure-pls-create: "true"
    service.beta.kubernetes.io/azure-pls-name: promManagedPls
    service.beta.kubernetes.io/azure-pls-proxy-protocol: "false"
    service.beta.kubernetes.io/azure-pls-visibility: "*"
spec:
  type: LoadBalancer
  selector:
    # app: myApp
    app.kubernetes.io/name: prometheus
    prometheus: prometheus-kube-prometheus-prometheus # note that this is related to the release name
  ports:
    - name: http-web
      protocol: TCP
      port: 9090
      targetPort: 9090
  1. 執行下列命令,將私人連結服務新增至 Prometheus 伺服器。

    kubectl --namespace monitoring apply -f pls-prometheus-svc.yaml
    
  2. 即會在 AKS 受控資源群組中建立名稱為 promManagedPls 的私人連結服務。 此程序會需要幾分鐘的時間。

使用受控私人端點進行連線

  1. 如果您還沒有 Azure 受控 Grafana 工作區,請遵循 Azure 受控 Grafana 快速入門建立一個。

  2. 開啟您的 Azure 受控 Grafana 工作區,然後移至 [網路]>[受控私人端點]>[新增]

    Azure 平台的螢幕擷取畫面,其中顯示 Azure 受控 Grafana 資源內的 [受控私人端點] 頁面。

  3. 為您的受控私人端點輸入名稱,然後選取您的 Azure 訂用帳戶。

  4. 針對 [資源類型],選取 [Microsoft.Network/privateLinkServices (私人連結服務)],以及針對 [目標資源],選取在上述步驟中建立的 promManagedPls 私人連結服務。 每個受控私人端點都會取得私人 IP 位址。 您也可以為此受控私人端點提供網域名稱。 Azure 受控 Grafana 服務確保此網域解析為 Azure 受控 Grafana 環境內受控私人端點的私人 IP。 例如,將網域設定為 *.prom.my-own-domain.com

    Azure 平台的螢幕擷取畫面,其中顯示針對新受控私人端點輸入的 Prometheus 資訊。

  5. 藉由移至 promManagedPls 資源來核准私人端點連線。 在 [設定] 下,移至 [私人端點連線]、使用核取方塊選取您的連線,然後選取 [核准]

    Azure 平台的螢幕擷取畫面,其中顯示 [核准連線] 動作。

  6. 在核准私人端點連線之後,請回到您的 Azure 受控 Grafana 資源,然後選取 [受控私人端點] 索引標籤中的 [重新整理] 按鈕,以同步 Connection state。 其現在應該顯示為 [已核准]

    Azure 平台的螢幕擷取畫面,其中顯示 [重新整理] 按鈕。

在 Grafana 儀表板中顯示 Prometheus 資料

  1. 從 Grafana 入口網站將 Prometheus 資料來源新增至 Grafana。 如需詳細資訊,請移至新增資料來源。 我們的 Prometheus URL 為 http://prom-service.prom.my-own-domain.com:9090

    Grafana 平台的螢幕擷取畫面,其中顯示將 Prometheus 新增為資料來源。

  2. 若要利用自我裝載 Prometheus 資料來源,請嘗試使用節點匯出工具完整儀表板,識別碼為 1860。 如需更多指導方針,請移至從 Grafana Labs 匯入儀表板

    Azure Grafana 平台的螢幕擷取畫面,其中顯示範例 Prometheus 儀表板。

後續步驟

了解如何使用服務帳戶