403 "Request had insufficient authentication scopes" 在 gcloud 容器集群获取凭证期间

403 "Request had insufficient authentication scopes" during gcloud container cluster get-credentials

我从 GCE 中的虚拟机执行了以下操作

gcloud auth activate-service-account --key-file <blah>
# "blah" is a service account key file (JSON) I generated from the web interface
gcloud config set project <project-name>
gcloud config set compute/zone <zone-name>
gcloud set container/cluster <cluster-name>

然后当我尝试 运行

gcloud container clusters get-credentials <cluster-name>

失败并显示错误消息:

Error message: "ERROR: (gcloud.container.clusters.get-credentials) ResponseError: code=403, message=Request had insufficient authentication scopes."

VM 与 GKE 集群位于同一网络中。我尝试了同样的事情,使用来自 GCE 外部机器的相同服务帐户密钥文件,针对 "default" 网络上的 GKE 集群,它成功了...

要从 GCE 虚拟机使用 Google Kubernetes Engine API,您需要在创建虚拟机时将云平台范围(“https://www.googleapis.com/auth/cloud-platform”)添加到您的虚拟机。

如果您从 GCP 中的虚拟机使用 Google Kubernetes 引擎 API。您首先需要在 vm 级别 https://www.googleapis.com/auth/projecthosting,cloud-platform 添加所需的范围。这也可以通过 GCP 控制台完成。停止 VM 实例然后转到编辑选项,最后您将找到云 API 访问范围。

第 1 步:gcloud 初始化

第 2 步:Select [2] 创建新配置

第 3 步:输入配置名称。名称以小写字母开头且仅包含小写字母 a-z、数字 0-9 和连字符“-”:kubernetes-service-account

第 4 步:选择要用于执行此配置操作的帐户:[2]使用新帐户登录

第 5 步:是否要继续 (Y/n)? y

第 6 步:将 link 复制粘贴到 brwoser 并使用用于创建您的 google 云帐户

的 ID 登录

第七步:登录后复制google提供的验证码,粘贴到控制台

第 8 步:选择要使用的云项目:

第 9 步:是否要配置默认计算区域和区域? (Y/n)? y

第 10 步:请输入数字选择或文本值(必须与列表项完全匹配):8

您的 Google Cloud SDK 已配置完毕,可以使用了!

完成后,确保为 VM 配置的服务帐户具有执行所需任务的权限。

现在有一个解决方案(仅限 Beta 版和 Alpha 版)可以在现有 GCE VM 上设置范围。成功所需要做的就是在执行命令之前停止 VM。

首先,您应该知道(并复制)您 VM 的当前范围,以便您可以将它们与新范围一起设置,使用:

gcloud compute instances describe your-instance

在底部你应该看到范围列表,复制它们。

然后,阅读测试版此命令的文档(所有人都可以使用,但使用风险自负):https://cloud.google.com/sdk/gcloud/reference/beta/compute/instances/set-scopes

在执行此命令之前,请从 GCE 页面停止实例并等待其关闭。将出现一个可怕的警告,请注意,如果 VM 未在 90 秒内正常关闭(= 所有进程和服务已成功关闭),文件系统可能会在强制关闭 VM 时损坏。如果您对此感到不安全,请做好记录并备份重要文件。

对我来说,在现有范围加上新范围(云平台)的情况下,生成的 set-scopes 命令是:

gcloud beta compute instances set-scopes my-instance --zone=us-central1-a --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append,https://www.googleapis.com/auth/cloud-platform