跳到主要内容

Google Cloud Monitoring 数据源配置

Google Cloud Monitoring(原名 Stackdriver)是 Google Cloud Platform(GCP)提供的监控服务,用于收集和分析云资源的性能数据。通过将 Google Cloud Monitoring 配置为 Grafana 的数据源,您可以在 Grafana 中可视化 GCP 的监控数据,从而更好地了解系统的运行状况。

本文将逐步指导您如何在 Grafana 中配置 Google Cloud Monitoring 数据源,并提供实际案例帮助您理解其应用场景。

1. 前提条件

在开始配置之前,请确保您已满足以下条件:

  • 已安装并运行 Grafana。
  • 拥有 Google Cloud Platform 账户,并启用了 Google Cloud Monitoring API。
  • 已创建一个服务账号,并为其分配了适当的权限(例如 Monitoring Viewer 角色)。

2. 创建服务账号密钥

首先,您需要为服务账号生成一个 JSON 密钥文件,以便 Grafana 能够访问 Google Cloud Monitoring 数据。

  1. 登录 Google Cloud Console。
  2. 导航到 IAM & Admin > Service Accounts
  3. 选择或创建一个服务账号。
  4. 点击 Keys > Add Key > Create new key,选择 JSON 格式并下载密钥文件。
备注

请妥善保管此密钥文件,因为它包含访问 Google Cloud Monitoring 的凭据。

3. 在 Grafana 中配置数据源

接下来,您需要在 Grafana 中添加 Google Cloud Monitoring 数据源。

  1. 登录 Grafana。
  2. 导航到 Configuration > Data Sources
  3. 点击 Add data source
  4. 在搜索框中输入 Google Cloud Monitoring,然后选择它。
  5. 在配置页面中,填写以下字段:
    • Name: 数据源的名称(例如 GCP Monitoring)。
    • Authentication Type: 选择 JWT File
    • JWT File: 上传之前下载的 JSON 密钥文件。
  6. 点击 Save & Test,确保配置正确。
提示

如果配置成功,您将看到 "Data source is working" 的提示。

4. 创建仪表板并添加面板

配置完成后,您可以创建一个新的仪表板,并添加面板来可视化 Google Cloud Monitoring 的数据。

  1. 导航到 Dashboards > New Dashboard

  2. 点击 Add new panel

  3. 在查询编辑器中,选择刚刚配置的 Google Cloud Monitoring 数据源。

  4. 使用 Google Cloud Monitoring 的查询语言(如 MQL 或 PromQL)编写查询语句。例如,以下查询用于获取 GCP 实例的 CPU 使用率:

    sql
    fetch gce_instance
    | metric 'compute.googleapis.com/instance/cpu/utilization'
    | group_by 1m, [value_utilization_mean: mean(value.utilization)]
    | every 1m
  5. 配置面板的显示选项(如图表类型、颜色等),然后点击 Apply

5. 实际案例:监控 GCP 虚拟机性能

假设您需要监控 GCP 中一组虚拟机的 CPU 使用率和网络流量。以下是具体步骤:

  1. 在 Grafana 中创建一个新的仪表板。

  2. 添加两个面板:

    • 第一个面板用于显示 CPU 使用率。
    • 第二个面板用于显示网络流量。
  3. 使用以下查询语句分别配置两个面板:

    CPU 使用率查询:

    sql
    fetch gce_instance
    | metric 'compute.googleapis.com/instance/cpu/utilization'
    | group_by 1m, [value_utilization_mean: mean(value.utilization)]
    | every 1m

    网络流量查询:

    sql
    fetch gce_instance
    | metric 'compute.googleapis.com/instance/network/received_bytes_count'
    | group_by 1m, [value_received_bytes_mean: mean(value.received_bytes_count)]
    | every 1m
  4. 保存仪表板并观察虚拟机的性能数据。

6. 总结

通过本文,您已经学会了如何在 Grafana 中配置 Google Cloud Monitoring 数据源,并创建仪表板来监控 GCP 资源的性能。Google Cloud Monitoring 提供了丰富的指标数据,结合 Grafana 的强大可视化功能,您可以轻松构建实时监控系统。

7. 附加资源与练习

练习:

  1. 尝试为其他 GCP 资源(如 Cloud Storage 或 BigQuery)创建监控面板。
  2. 探索 Grafana 的告警功能,为关键指标设置告警规则。
警告

请确保您的服务账号权限适中,避免过度授权导致安全风险。