我有一个 GCP 项目。在那里,我使用 GKE 并运行 Teamcity 容器。此 Teamcity 容器是我的构建服务器,也是我运行构建步骤/脚本的位置。
构建步骤之一是将 Docker 镜像推送到 Google 容器注册表。这样做时会失败,并导致此错误:
denied: Token exchange failed for project 'coopr-mod'. Caller does not have permission 'storage.buckets.create'. To configure permissions, follow instructions at: https://cloud.google.com/container-registry/docs/access-control
我阅读了提到的说明链接,但只是找不到如何解决我的案例中的问题。
为了完成,我特此记下执行的构建步骤:
第 1 步:
# Create environment variable for correct distribution
export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)"
# Add the Cloud SDK distribution URI as a package source
echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
# Import the Google Cloud Platform public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
# Update the package list and install the Cloud SDK
sudo apt-get -y update && sudo apt-get -y install google-cloud-sdk
第 2 步:
gcloud --quiet auth configure-docker
第 3 步: `docker 构建 myimage:1
第 4 步:
docker 标签 myimage:1 eu.gcr.io/my-project/myimage:1
第 5 步:(失败的步骤)
docker Push eu.gcr.io/coopr-mod/myimage:1
结果:
denied: Token exchange failed for project 'coopr-mod'. Caller does not have permission 'storage.buckets.create'. To configure permissions, follow instructions at: https://cloud.google.com/container-registry/docs/access-control
我读到过有关授予 GKE 对 Google Storage 的读写
权限的信息,但我找不到告诉我“如何”执行此操作的指南。
最佳答案
还有像样的documentation有关如何使用 GCR 和 GKE 推送和拉取镜像的信息。另外,this answer对于常规 GCE 实例来说,这是一个类似的答案。
假设您的节点池使用默认 GCE 帐户配置了实例,那么在创建池时使用读写
访问范围配置该池就很简单了。
有几种方法可以做到这一点:
- 当您使用 gcloud 创建节点池时,请(另外)指定
--scopes https://www.googleapis.com/auth/devstorage.read_write
(或者,您可以启用“所有范围” ' 使用此值:https://www.googleapis.com/auth/cloud-platform
,但这是非常宽松的) - 在控制台中配置节点池时,请专门选择范围(默认为仅读取)或选择“所有范围”,例如:
...省略了许多范围选择...
如果出于某种原因,您不能直接拆除节点池,请参阅有关如何将作业迁移到 new machine type 的说明。应该适合您(在这种情况下,“新机器类型”仅具有新的访问权限)。基本步骤是:
- 创建新节点池
- 封锁(标记为不可调度)现有节点池(并耗尽现有作业)
- 等待作业迁移
- 删除现有节点池
也就是说,如果您不希望集群上的任何 pod这种访问方式。同样,这不需要销毁并重新创建节点池。
这要复杂得多,但步骤大致是:
- 创建一个具有必要角色的 IAM 帐户(可能是“存储对象管理员”,但您需要能够在首次推送时创建存储桶)。
- 为此 IAM 账户生成 key 。
- 将 key 文件与您的作业一起部署(可能通过 GKE key )
- Authenticate to docker with the key file :
cat keyfile.json | docker登录-u _json_key --password-stdin https://eu.gcr.io
(或者任何适合您的正确 GCR 存储库主机名)
关于docker - 将 Docker 镜像从 GKE 中运行的容器推送到 Google 容器注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58780536/