gcloud 实例上 stackdrvier 导出器的 403 响应

403 response with stackdrvier exporter on gcloud instance

我花了一整天的时间来解决这个问题,但我只是看不出我做错了什么,而且我在其他帖子中找不到我的问题的答案,所以我寻求帮助。

我有一个已经安装了 prometheus 的计算实例和一个 role/viewer 已经配置为自动发现和工作的服务帐户。

我需要将 stackdrivers 监控指标添加到指标中,因此我在实例上添加了 stackdriver 导出器,服务仍然没有问题 运行,我可以使用 prometheus 对其发起请求。

服务是这样启动的:

/usr/local/bin/stackdriver_exporter --google.project-id PROJECTID --monitoring.metrics-type-prefixes \
cloudsql.googleapis.com/,compute.googleapis.com/,vpn.googleapis.com/,loadbalancing.googleapis.com/,storage.googleapis.com/,redis.googleapis.com/

那里似乎没有问题,我对项目进行了三次检查以防我失明并且看起来不错。

我的问题是我总是遇到这个错误:

Nov 30 16:15:34 INSTANCENAME stackdriver_exporter[19149]: time="2018-11-30T16:15:34Z" leve
l=error msg="Error while getting Google Stackdriver Monitoring metrics: googleapi: Error 403: Request had
 insufficient authentication scopes., forbidden" source="monitoring_collector.go:132"

我想到了一个角色问题,所以我添加了monitoring viewer然后monitoring admin甚至role/owner 但那没有做任何事情。

我也尝试设置 GOOGLE_APPLICATION_CREDENTIALS env 变量来确定帐户,但仍然是同样的错误。

我检查并启用了 Stackdriver APIStackdriver 监控 API,我一定是遗漏了什么但我就是不能指手画脚,如有任何帮助,我们将不胜感激。

请注意,在 gcloud 中,最终权限是服务帐户 roles/permissions 和实例范围之间的 "mask"。

When you set up an instance to run as a service account, the level of access the service account has is determined by the combination of access scopes granted to the instance and IAM roles granted to the service account.

因此,如果您 运行 在没有范围 https://www.googleapis.com/auth/monitoring.read 的实例中使用角色 monitoring viewer 的服务帐户,您将无法读取监控指标。

1.- 使用 gcloud compute instances describe INSTANCE 检查实例的范围。您会看到类似这样的内容:

- email: 487724816353-compute@developer.gserviceaccount.com scopes: - https://www.googleapis.com/auth/monitoring.write - https://www.googleapis.com/auth/logging.write - https://www.googleapis.com/auth/devstorage.read_only 你能在那里看到正确的示波器吗?

2.- 如果没有尝试设置您需要的范围: (您需要为此停止实例)

gcloud compute instances set-service-account INSTANCE \ --service-account SERVICE_ACCOUNT@xxxxxxxx.iam.gserviceaccount.com \ --scopes cloud-platform

3.- 按照 gcloud 最佳实践 (https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#best_practices) 中的建议,上一个命令允许完全访问实例中的所有 APIS。然后您可以控制服务帐户的 adding/removing 角色的最终权限。