将 Connect Agent 指标导出到 Cloud Monitoring

本页面介绍如何将 Connect Agent 指标从 GKE on VMware、GKE on AWS 或任何其他已注册的 Kubernetes 集群导出到 Cloud Monitoring。

概览

在 GKE on VMware 或 GKE on AWS 集群中,Prometheus 会收集指标并将其本地存储在集群内。向 Google Cloud 控制台注册集群会在集群中创建一个名为 Connect Agent 的 Deployment。Prometheus 会从 Connect Agent 收集有用的指标,例如 Google 连接错误和打开的连接数等。如需将这些指标提供给 Cloud Monitoring,您必须执行以下操作:

  • 使用 Service 公开 Connect Agent。
  • 部署 prometheus-to-sd,这是一个用于抓取 Prometheus 指标并将其导出到 Cloud Monitoring 的简单组件。

之后,您可以通过在 Google Cloud 控制台中使用 Monitoring 或者通过对 Service 执行端口转发并使用 curl 来查看这些指标。

为 Connect Agent 的命名空间创建变量

Connect Agent 通常在命名空间 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 Agent Deployment

  1. 将以下配置复制到名为 gke-connect-agent.yaml 的 YAML 文件。此配置会创建 gke-connect-agent Service,用于公开 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 是运行 Connect Agent 的 Kubernetes 集群的名称。
    • REGION 是在地理上靠近集群运行位置的位置。请选择在地理上靠近集群实际位置的 Google Cloud 可用区
    • ZONE 是靠近您的本地数据中心的位置。请选择在地理上靠近流量流经位置的 Google Cloud 可用区。

    此配置会创建两个资源:

    • 一个是 prom-to-sd-user-config ConfigMap,用于声明供 Deployment 使用的几个变量
    • 另一个是 prometheus-to-monitoring Deployment,用于在单个 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 Console 中的“Monitoring”页面。

    转到“监控”页面

  2. 在左侧菜单中,点击 Metrics Explorer

  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. 在 Shell 中,使用 kubectlgke-connect-monitoring Service 执行端口转发:

    kubectl -n ${AGENT_NS} port-forward svc/gke-connect-monitoring 8080
  2. 打开另一个 Shell,然后运行以下命令:

    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}