本页面介绍了 Google Distributed Cloud 如何记录有关其运行的各种类型的信息,以及如何查看这些信息。收集某些类型的日志和指标会产生额外费用。如需了解详情,请参阅日志和指标的结算。
配置日志记录和监控
您必须先执行以下操作,然后才能开始收集日志和指标:
使用以下命令启用日志记录 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。授予写入日志和指标所需的角色:
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 日志记录始终处于启用状态。
分布式云记录的资源类型是以下标准 Kubernetes 资源:
k8s_containerk8s_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/Machineedgecontainer.googleapis.com/Rack
Machine 资源指标
Distributed Cloud 会为 edgecontainer.googleapis.com/Machine 资源写入以下 Cloud Monitoring API 指标:
| 指标 | 说明 |
|---|---|
/machine/cpu/total_cores
|
机器上存在的物理处理器核心总数。 |
/machine/cpu/usage_time
|
机器上所有核心的累计 CPU 使用时间。类型可以是 workload(客户工作负载)或 system(其他所有内容)。 |
/machine/cpu/utilization
|
机器上的 CPU 利用率百分比。范围为 0 到 1。类型可以是 workload(客户工作负载)或 system(其他所有内容)。 |
/machine/memory/total_bytes
|
机器上总内存的字节数。 |
/machine/memory/used_bytes
|
机器上已用内存的字节数。memory_type 是 evictable(可由内核回收)或 non-evictable(不可回收)。 |
/machine/memory/utilization
|
机器上的内存利用率百分比。范围为 0 到 1。
memory_type 为 evictable(可由内核回收)或 non-evictable(不可回收)。 |
/machine/network/up
|
指示网络接口是否已启动并正在运行。包括主卡、辅助卡和端口。 |
/machine/network/link_speed
|
主网络接口卡的链路速度。 |
/machine/network/received_bytes_count
|
主要网卡接收的字节数。 |
/machine/network/sent_bytes_count
|
主要网卡已发送的字节数。 |
/machine/network/connectivity
|
指示主网卡是否已连接到互联网。 |
/machine/disk/total_bytes
|
机器上总磁盘空间的字节数。 |
/machine/disk/used_bytes
|
相应机器上已用磁盘空间的字节数。 |
/machine/disk/utilization
|
机器上的磁盘空间利用率百分比。范围为 0 到 1。 |
/machine/restart_count
|
机器的重启次数。 |
/machine/uptime
|
自上次重新启动以来,机器的正常运行时间。 |
/machine/connected
|
指示设备是否已连接到 Google Cloud。 |
Rack 资源指标
Distributed Cloud 会为 edgecontainer.googleapis.com/Rack 资源写入以下 Cloud Monitoring API 指标:
| 指标 | 说明 |
|---|---|
/router/bgp_up
|
指明路由器上的 BGP 对等互联会话是否已启动且运行正常。
router_id 用于标识特定路由器(每个机架最多 2 个)。 |
/router/connected
|
指示 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":应用的指标端点监听连接的端口。
运行一个示例应用
在本部分中,您将创建一个应用,用于编写自定义日志并公开自定义指标端点。
将以下 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创建 Deployment 和 Service:
kubectl --kubeconfig apply -f my-app.yaml
查看应用日志
控制台
在 Google Cloud 控制台中,前往 Logs Explorer 页面。
点击资源。
在所有资源类型列表中,选择
Kubernetes Container。对于集群名称,请选择用户集群的名称。
对于命名空间名称,请选择
default。点击添加,然后点击运行查询。
在查询结果部分,您可以查看来自
monitoring-exampleDeployment 的日志条目。例如:{ "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
使用
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。在输出中,您可以看到来自
monitoring-exampleDeployment 的日志条目。例如: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 控制台中查看该指标的值。
在 Google Cloud 控制台中,前往 Metrics Explorer 页面。
对于资源类型,请选择
Kubernetes Pod。对于指标,请选择
external/prometheus/example_monitoring_up。在图表中,您可以看到
example_monitoring_up的重复值为 1。