將 Connect Agent 指標匯出至 Cloud Monitoring

本頁說明如何從 Google Distributed Cloud、GKE on AWS 或任何其他已註冊的 Kubernetes 叢集,將 Connect Agent 指標匯出至 Cloud Monitoring。

總覽

在 Google Distributed Cloud 或 GKE on AWS 叢集中,Prometheus 會收集指標並儲存在叢集內。向機群註冊外部叢集時,系統會在叢集中建立名為 Connect Agent 的 Deployment。 Google Cloud Prometheus 會從 Connect Agent 收集實用指標,例如連線至 Google 時發生的錯誤,以及開啟的連線數量。如要讓 Cloud Monitoring 取得這些指標,請完成下列步驟:

  • 使用 Service 公開 Connect Agent。
  • 部署 prometheus-to-sd,這是一個簡單的元件,可抓取 Prometheus 指標並匯出至 Cloud Monitoring。

之後,您可以使用Google Cloud 控制台中的 Monitoring 服務查看指標,也可以轉送服務的連接埠並使用 curl

為 Connect 代理程式的命名空間建立變數

Connect 代理程式通常會在 gke-connect 命名空間中執行。

Connect Agent 有標籤,hub.gke.io/project。HTTP 伺服器會監聽通訊埠 8080。

為命名空間建立變數 AGENT_NS

AGENT_NS=$(kubectl get ns --kubeconfig KUBECONFIG -o jsonpath={.items..metadata.name} -l hub.gke.io/project=PROJECT_ID)

更改下列內容:

  • KUBECONFIG:叢集的 kubeconfig 檔案
  • PROJECT_ID:專案 ID

公開 Connect 代理程式 Deployment

  1. 將下列設定複製到名為 gke-connect-agent.yaml 的 YAML 檔案中。這項設定會建立 Service gke-connect-agent,用於公開 Connect Agent Deployment。

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: gke-connect-agent
      name: gke-connect-agent
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
      selector:
        app: gke-connect-agent
      type: ClusterIP
  2. 將 YAML 檔案套用至叢集中的 Connect Agent 命名空間,其中 KUBECONFIG 是叢集的 kubeconfig 檔案路徑:

    kubectl apply -n ${AGENT_NS} --kubeconfig KUBECONFIG -f gke-connect-agent.yaml
  3. roles/monitoring.metricWriter IAM 角色繫結至車隊 Google 服務帳戶:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/monitoring.metricWriter"

部署中 prometheus-to-sd

  1. 將下列設定複製到名為 prometheus-to-sd.yaml 的 YAML 檔案,其中:

    • PROJECT_ID 是您的 Google Cloud 專案 ID。瞭解如何找到這個值
    • CLUSTER_NAME 是 Kubernetes 叢集的名稱,Connect Agent 會在該叢集執行。
    • REGION 是叢集執行所在位置附近的地理位置。選擇與叢集實際位置相近的Google Cloud 可用區
    • ZONE 是您地端部署資料中心附近的區域。選擇靠近流量來源地理位置的 Google Cloud 區域。

    這項設定會建立兩項資源:

    • ConfigMap (prom-to-sd-user-config),其中宣告了數個變數,供 Deployment 使用
    • Deployment prometheus-to-monitoring,在單一 Pod 中執行 prometheus-to-sd
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: prom-to-sd-user-config
    data:
      # The project that the Connect Agent uses. Accepts ID or number.
      project: PROJECT_ID
      # A name for the cluster, which shows up in Cloud Monitoring.
      cluster_name: CLUSTER_NAME
      # cluster_location must be valid (e.g. us-west1-a); shows up in Cloud Monitoring.
      cluster_location: REGION
      # A zone name to report (e.g. us-central1-a).
      zone: ZONE
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prometheus-to-monitoring
    spec:
      progressDeadlineSeconds: 600
      replicas: 1
      revisionHistoryLimit: 2
      selector:
        matchLabels:
          run: prometheus-to-monitoring
      template:
        metadata:
          labels:
            run: prometheus-to-monitoring
        spec:
          containers:
            - args:
                - /monitor
                # 'gke-connect-agent' is the text that will show up in the Cloud Monitoring metric name.
                - --source=gke-connect-agent:http://gke-connect-agent:8080
                - --monitored-resource-types=k8s
                - --stackdriver-prefix=custom.googleapis.com
                - --project-id=$(PROM_PROJECT)
                - --cluster-name=$(PROM_CLUSTER_NAME)
                - --cluster-location=$(PROM_CLUSTER_LOCATION)
                - --zone-override=$(PROM_ZONE)
                # A node name to report. This is a dummy value.
                - --node-name=MyGkeConnectAgent
              env:
                - name: GOOGLE_APPLICATION_CREDENTIALS
                  value: /etc/creds/creds-gcp.json
                - name: PROM_PROJECT
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: project
                - name: PROM_CLUSTER_NAME
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: cluster_name
                - name: PROM_CLUSTER_LOCATION
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: cluster_location
                - name: PROM_ZONE
                  valueFrom:
                    configMapKeyRef:
                      name: prom-to-sd-user-config
                      key: zone
              image: gcr.io/google-containers/prometheus-to-sd:v0.7.1
              imagePullPolicy: IfNotPresent
              name: prometheus-to-monitoring
              resources: {}
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
              volumeMounts:
                - mountPath: /etc/creds
                  name: creds-gcp
                  readOnly: true
          restartPolicy: Always
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
          volumes:
            - name: creds-gcp
              secret:
                defaultMode: 420
                # This secret is already set up for the Connect Agent.
                secretName: creds-gcp
  2. 將 YAML 檔案套用至叢集中的 Connect Agent 命名空間,其中 KUBECONFIG 是叢集的 kubeconfig 檔案路徑:

    kubectl apply -n ${AGENT_NS} --kubeconfig KUBECONFIG -f prometheus-to-sd.yaml

查看指標

控制台

  1. 前往 Google Cloud 控制台的「Monitoring」頁面。

    前往「監控」頁面

  2. 按一下左選單中的「指標探索工具」

  3. Connect Agent 的指標會加上 custom.googleapis.com/gke-connect-agent/ 前置字串,其中 gke-connect-agent--source 引數中指定的字串。例如: custom.googleapis.com/gke-connect-agent/gkeconnect_dialer_connection_errors_total

cURL

  1. 在殼層中,使用 kubectlgke-connect-monitoring 服務轉送至通訊埠:

    kubectl -n ${AGENT_NS} port-forward svc/gke-connect-monitoring 8080
  2. 開啟另一個殼層,然後執行下列指令:

    curl localhost:8080/metrics

正在清除所用資源

如要刪除您在本主題中建立的資源:

AGENT_NS=$(kubectl get ns --kubeconfig KUBECONFIG -o jsonpath={.items..metadata.name} -l hub.gke.io/project)
kubectl delete configmap prom-to-sd-user-config --kubeconfig KUBECONFIG -n ${AGENT_NS}
kubectl delete service gke-connect-agent --kubeconfig KUBECONFIG -n ${AGENT_NS}
kubectl delete deployment prometheus-to-monitoring --kubeconfig KUBECONFIG -n ${AGENT_NS}