在 GKE 中使用独立的“gsutil”

Jam*_*ett 5 gsutil service-accounts kubernetes google-kubernetes-engine

我正在尝试gsutil从 GKE 集群中运行的容器中使用独立工具,但我无法让它工作。我相信集群有足够的权限(见下文)。然而,运行

./gsutil ls gs://my-bucket/
Run Code Online (Sandbox Code Playgroud)

产量

ServiceException: 401 Anonymous users does not have storage.objects.list access to bucket my-bucket.
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?我没有.boto文件,因为我认为它不应该是必要的——或者是吗?这是集群和节点池具有的范围列表:

- https://www.googleapis.com/auth/compute
- https://www.googleapis.com/auth/devstorage.full_control
- https://www.googleapis.com/auth/logging.write
- https://www.googleapis.com/auth/monitoring.write
- https://www.googleapis.com/auth/pubsub
- https://www.googleapis.com/auth/servicecontrol
- https://www.googleapis.com/auth/service.management.readonly
- https://www.googleapis.com/auth/trace.append
Run Code Online (Sandbox Code Playgroud)

Rob*_*bbe 7

您可以使用服务帐户或您自己的凭据在 GKE 上的 docker 容器内使用 gsutil 。

服务帐号

1)service-account.json文件添加到您的项目中。

2).boto在你的项目中添加一个指向该service-account.json文件的文件:

[Credentials]
gs_service_key_file = /path/to/service-account.json
Run Code Online (Sandbox Code Playgroud)

3)在您的 Dockerfile 中,将BOTO_CONFIG环境变量设置为指向此.boto文件:

ENV BOTO_CONFIG=/path/to/.boto
Run Code Online (Sandbox Code Playgroud)


自己的凭证

1) 在本地,运行 gcloud auth login。.boto将在~/.config/gcloud/legacy_credentials/your@account.com/.boto 中创建一个文件,其结构如下:

[OAuth2]
client_id = <id>.apps.googleusercontent.com
client_secret = <secret>

[Credentials]
gs_oauth2_refresh_token = <token>
Run Code Online (Sandbox Code Playgroud)

2)将此.boto文件复制到您的项目中

3)在您的 Dockerfile 中,将BOTO_CONFIG环境变量设置为指向此.boto文件:

ENV BOTO_CONFIG=/path/to/.boto
Run Code Online (Sandbox Code Playgroud)


我使用 pip install gsutil 在 docker 容器中安装了独立的 gsutil


mho*_*lum 3

简短的回答:
是的,您需要某种boto 文件。

长答案:
一般来说,对于 GCE 实例,您不需要~/.boto文件,因为该/etc/boto.cfg文件已经存在 - GSUtil 使用的 Boto 库知道默认情况下会查找该文件。在 Debian 映像上,它包含以下几行:

# This file is automatically created at boot time by the /usr/lib/python
# 2.7/dist-packages/google_compute_engine/boto/boto_config.pyc script.
# Do not edit this file directly. If you need to add items to this file,
# create or edit /etc/boto.cfg.template instead and then re-run the
# script.

[GSUtil]
default_project_id = <PROJECT NUMBER HERE>
default_api_version = 2

[GoogleCompute]
service_account = default

[Plugin]
plugin_directory = /usr/lib/python2.7/dist-packages/google_compute_engine/boto
Run Code Online (Sandbox Code Playgroud)

如果您想在 GKE 容器上模仿此行为,则必须安装google-compute-enginepython 软件包,以及一个 boto 文件,该文件告诉 gsutil 从安装位置加载该插件,如上所示。在 GCE 上(我假设也是 GKE,尽管我还没有测试过),该插件允许虚拟机与其元数据服务器通信以获取指定服务帐户的凭据。