記錄檔和指標

本頁面說明 Google Distributed Cloud 如何記錄各種作業資訊,以及如何查看這些資訊。收集部分類型的記錄和指標會產生額外費用。詳情請參閱「記錄和指標的計費方式」。

設定記錄和監控功能

開始收集記錄和指標前,請先完成下列步驟:

  1. 使用下列指令啟用記錄 API:

    gcloud services enable opsconfigmonitoring.googleapis.com --project PROJECT_ID
    gcloud services enable logging.googleapis.com --project PROJECT_ID
    gcloud services enable monitoring.googleapis.com --project PROJECT_ID
    

    PROJECT_ID 替換為目標 Google Cloud 專案的 ID。

  2. 授予寫入記錄和指標所需的角色:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --role roles/opsconfigmonitoring.resourceMetadata.writer \
        --member "serviceAccount:PROJECT_ID.svc.id.goog[kube-system/metadata-agent]"
    
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --role roles/logging.logWriter \
         --member "serviceAccount:PROJECT_ID.svc.id.goog[kube-system/stackdriver-log-forwarder]"
    
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --role roles/monitoring.metricWriter \
        --member "serviceAccount:PROJECT_ID.svc.id.goog[kube-system/gke-metrics-agent]"
    

    PROJECT_ID 替換為目標 Google Cloud 專案的 ID。

記錄

本節列出 Distributed Cloud 支援的 Cloud Logging 資源類型。如要查看 Distributed Cloud 記錄,請使用 Google Cloud 控制台中的記錄檔探索工具。Distributed Cloud 記錄一律會啟用。

Distributed Cloud 記錄的資源類型如下列標準 Kubernetes 資源:

  • k8s_container
  • k8s_node

您也可以使用 Cloud Logging API 擷取及擷取 Distributed Cloud 記錄。如要瞭解如何設定這項記錄機制,請參閱 Cloud Logging 用戶端程式庫的說明文件。

指標

本節列出 Distributed Cloud 支援的 Cloud Monitoring 指標。如要查看 Distributed Cloud 指標,請使用Google Cloud 控制台中的 Metrics Explorer

Distributed Cloud 叢集指標

對於 Distributed Cloud 叢集,Distributed Cloud 提供下列 Distributed Cloud 節點產生的指標類型:

  • 資源指標提供 Distributed Cloud 節點和 Pod 效能相關資訊,例如 CPU 負載和記憶體用量。
  • 系統應用程式指標提供 Distributed Cloud 系統工作負載的相關資訊,例如 coredns

如需這類指標的清單,請參閱「Google Distributed Cloud 和 Google Distributed Cloud 指標」。

Distributed Cloud 不會提供與 Distributed Cloud 叢集相關聯的 Kubernetes 控制層所產生的指標。

Distributed Cloud 硬體指標

Distributed Cloud 會使用下列資源類型,提供 Distributed Cloud 硬體的指標:

  • edgecontainer.googleapis.com/Machine
  • edgecontainer.googleapis.com/Rack

Machine 資源指標

Distributed Cloud 會為 edgecontainer.googleapis.com/Machine 資源寫入下列 Cloud Monitoring API 指標:

指標 說明
/machine/cpu/total_cores
  • 類型:GAUGE
  • 類型:INT
機器上的實體處理器核心總數。
/machine/cpu/usage_time
  • 類型:CUMULATIVE
  • 類型:DOUBLE
  • 單位:Seconds
機器上所有核心的累計 CPU 使用時間。類型可以是 workload (客戶工作負載) 或 system (其他所有項目)。
/machine/cpu/utilization
  • 類型:GAUGE
  • 類型:DOUBLE
機器的 CPU 使用率百分比。範圍為 0 到 1。類型可以是 workload (客戶工作負載) 或 system (其他所有項目)。
/machine/memory/total_bytes
  • 類型:GAUGE
  • 類型:INT64
機器上的記憶體總量 (以位元組為單位)。
/machine/memory/used_bytes
  • 類型:GAUGE
  • 類型:INT64
機器上已用記憶體的位元組數。memory_type 可以是 evictable (可由核心回收) 或 non-evictable (不可回收)。
/machine/memory/utilization
  • 類型:GAUGE
  • 類型:DOUBLE
電腦的記憶體使用率百分比。範圍為 0 到 1。 memory_type 可以是 evictable (可由核心回收) 或 non-evictable (不可回收)。
/machine/network/up
  • 類型:GAUGE
  • 類型:BOOL
指出網路介面是否正常運作。包括主卡、次要卡和連接埠。
/machine/network/link_speed
  • 類型:GAUGE
  • 類型:DOUBLE
  • 單位:每秒位元組
主要網路介面卡的連結速度。
/machine/network/received_bytes_count
  • 類型:CUMULATIVE
  • 類型:DOUBLE
主要網路介面卡接收的位元組數。
/machine/network/sent_bytes_count
  • 類型:CUMULATIVE
  • 類型:DOUBLE
主要網路介面卡的傳送位元組數。
/machine/network/connectivity
  • 類型:GAUGE
  • 類型:BOOL
指出主要網路介面卡是否已連上網際網路。
/machine/disk/total_bytes
  • 類型:GAUGE
  • 類型:INT64
機器上的磁碟空間總位元組數。
/machine/disk/used_bytes
  • 類型:GAUGE
  • 類型:INT64
機器上已使用的磁碟空間位元組數。
/machine/disk/utilization
  • 類型:GAUGE
  • 類型:DOUBLE
電腦的磁碟空間使用率百分比。範圍為 0 到 1。
/machine/restart_count
  • 類型:CUMULATIVE
  • 類型:INT
裝置重新啟動的次數。
/machine/uptime
  • 類型:GAUGE
  • 類型:INT
  • 單位:Seconds
機器自上次重新啟動後已運作的時間。
/machine/connected
  • 類型:GAUGE
  • 類型:INT64
指出電腦是否已連上 Google Cloud。

Rack 資源指標

Distributed Cloud 會為 edgecontainer.googleapis.com/Rack 資源寫入下列 Cloud Monitoring API 指標:

指標 說明
/router/bgp_up
  • 類型:GAUGE
  • 類型:BOOL
指出路由器上的 BGP 對等互連工作階段是否正常運作。router_id 可識別特定路由器 (每個機架最多 2 個)。
/router/connected
  • 類型:GAUGE
  • 類型:BOOL
指出 BGP 路由器是否已連線至 Google Cloud。 router_id 可識別特定路由器 (每個機架最多 2 個)。

匯出自訂應用程式記錄和指標

Distributed Cloud 會自動匯出在 Distributed Cloud 工作負載上執行的應用程式記錄。如要匯出在 Distributed Cloud 工作負載上執行的應用程式指標,請按照下一節的說明進行註解。

為工作負載加上註解,啟用指標匯出功能

如要啟用應用程式的自訂指標收集功能,請在應用程式的 Service 或 Deployment 資訊清單中加入下列註解:

  • prometheus.io/scrape: "true"
  • prometheus.io/path: "ENDPOINT_PATH"。請將 ENDPOINT_PATH 替換為目標應用程式指標端點的完整路徑。
  • prometheus.io/port: "PORT_NUMBER":應用程式指標端點監聽連線的通訊埠。

執行範例應用程式

在本節中,您將建立可寫入自訂記錄並公開自訂指標端點的應用程式。

  1. 將下列 Service 和 Deployment 資訊清單儲存至名為 my-app.yaml 的檔案。請注意,服務具有註解 prometheus.io/scrape: "true"

    kind: Service
    apiVersion: v1
    metadata:
      name: "monitoring-example"
      namespace: "default"
      annotations:
        prometheus.io/scrape: "true"
    spec:
      selector:
        app: "monitoring-example"
      ports:
        - name: http
          port: 9090
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: "monitoring-example"
      namespace: "default"
      labels:
        app: "monitoring-example"
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: "monitoring-example"
      template:
        metadata:
          labels:
            app: "monitoring-example"
        spec:
          containers:
          - image: gcr.io/google-samples/prometheus-dummy-exporter:latest
            name: prometheus-example-exporter
            imagePullPolicy: Always
            command:
            - /bin/sh
            - -c
            - ./prometheus-dummy-exporter --metric-name=example_monitoring_up --metric-value=1 --port=9090
            resources:
              requests:
                cpu: 100m
    
  2. 建立 Deployment 和 Service:

    kubectl --kubeconfig apply -f my-app.yaml

查看應用程式記錄檔

控制台

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

    前往 Logs Explorer

  2. 按一下「Resource」(資源)

  3. 在「所有資源類型」清單中,選取「Kubernetes Container」。

  4. 在「叢集名稱」中,選取使用者叢集的名稱。

  5. 在「命名空間名稱」部分,選取 default

  6. 按一下「新增」,然後點選「執行查詢」。

  7. 「查詢結果」部分會顯示來自 monitoring-example 部署作業的記錄項目。例如:

    {
      "textPayload": "2020/11/14 01:24:24 Starting to listen on :9090\n",
      "insertId": "1oa4vhg3qfxidt",
      "resource": {
        "type": "k8s_container",
        "labels": {
          "pod_name": "monitoring-example-7685d96496-xqfsf",
          "cluster_name": ...,
          "namespace_name": "default",
          "project_id": ...,
          "location": "us-west1",
          "container_name": "prometheus-example-exporter"
        }
      },
      "timestamp": "2020-11-14T01:24:24.358600252Z",
      "labels": {
        "k8s-pod/pod-template-hash": "7685d96496",
        "k8s-pod/app": "monitoring-example"
      },
      "logName": "projects/.../logs/stdout",
      "receiveTimestamp": "2020-11-14T01:24:39.562864735Z"
    }
    

gcloud

  1. 使用 gcloud logging read 指令:

    gcloud logging read 'resource.labels.project_id="PROJECT_ID" AND \
        resource.type="k8s_container" AND resource.labels.namespace_name="default"'

    PROJECT_ID 替換為專案 ID。

  2. 在輸出內容中,您可以看到 monitoring-example 部署作業的記錄項目。例如:

    insertId: 1oa4vhg3qfxidt
    labels:
      k8s-pod/app: monitoring-example
      k8s- pod/pod-template-hash: 7685d96496
    logName: projects/.../logs/stdout
    receiveTimestamp: '2020-11-14T01:24:39.562864735Z'
    resource:
      labels:
        cluster_name: ...
        container_name: prometheus-example-exporter
        location: us-west1
        namespace_name: default
        pod_name: monitoring-example-7685d96496-xqfsf
        project_id: ...
      type: k8s_container
    textPayload: |
      2020/11/14 01:24:24 Starting to listen on :9090
    timestamp: '2020-11-14T01:24:24.358600252Z'
    

查看應用程式指標

範例應用程式會公開名為 example_monitoring_up 的自訂指標。您可以在 Google Cloud 控制台中查看該指標的值。

  1. 前往 Google Cloud 控制台的「指標探索器」頁面。

    前往 Metrics Explorer

  2. 在「Resource type」(資源類型) 部分,選取「Kubernetes Pod」。

  3. 在「指標」部分選取 external/prometheus/example_monitoring_up

  4. 在圖表中,您可以看到 example_monitoring_up 的值重複出現 1。

後續步驟