日志和指标

本页面介绍了 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 日志,请使用控制台中的 Logs Explorer。 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_typeevictable(可由内核回收)或 non-evictable(不可回收)。
/machine/memory/utilization
  • 种类:GAUGE
  • 类型:DOUBLE
机器上的内存利用率百分比。范围是 0 到 1。 memory_typeevictable(可由内核回收)或 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的文件中。请注意,Service 具有注解 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. 点击资源

  3. 所有资源类型 列表中,选择 Kubernetes Container

  4. 对于集群名称,选择用户集群的名称。

  5. 对于命名空间名称 ,选择 default

  6. 点击添加 ,然后点击运行查询

  7. 查询结果 部分中,您可以查看来自 monitoring-example Deployment 的日志条目。例如:

    {
      "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 Deployment 的日志条目。例如:

    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 页面。

    转到 Metrics Explorer

  2. 对于 资源类型,选择 Kubernetes Pod

  3. 对于指标 ,选择 external/prometheus/example_monitoring_up

  4. 在图表中,您可以看到 example_monitoring_up 的重复值为 1。

后续步骤