将gsutil配置为在pod内使用kubernetes服务帐户凭据

时间:2018-10-08 21:00:52

标签: kubernetes boto gsutil

我有一个kubernetes Cronjob,它执行一些备份作业,并且备份文件需要上传到存储桶。该Pod在/var/run/secrets/kubernetes.io/serviceaccount中安装了Pod内部的服务帐户凭据,,但是如何指示gsutil在/var/run/secrets/kubernetes.io/中使用凭据服务帐户?

lrwxrwxrwx 1 root root   12 Oct  8 20:56 token -> ..data/token
lrwxrwxrwx 1 root root   16 Oct  8 20:56 namespace -> ..data/namespace
lrwxrwxrwx 1 root root   13 Oct  8 20:56 ca.crt -> ..data/ca.crt
lrwxrwxrwx 1 root root   31 Oct  8 20:56 ..data -> ..2018_10_08_20_56_04.686748281
drwxr-xr-x 2 root root  100 Oct  8 20:56 ..2018_10_08_20_56_04.686748281
drwxrwxrwt 3 root root  140 Oct  8 20:56 .
drwxr-xr-x 3 root root 4096 Oct  8 20:57 ..

2 个答案:

答案 0 :(得分:1)

简短的回答是,令牌没有gsutil知道如何使用的格式,因此您不能使用它。您将需要一个JSON密钥文件,如本教程中所述(除非您将无法使用GOOGLE_APPLICATION_CREDENTIALS环境变量):

https://cloud.google.com/kubernetes-engine/docs/tutorials/authenticating-to-cloud-platform

Gsutil而不是从GOOGLE_APPLICATION_CREDENTIALS环境变量中读取,而是使用Boto配置文件来加载凭据。它知道寻找这些Boto配置文件的常见位置是/etc/boto.cfg$HOME/.boto。注意,后一个值根据运行命令的用户而变化($HOME扩展为不同用户的不同值);由于cron作业通常以与设置配置文件不同的用户身份运行,因此我不建议依赖此路径。

因此,在您的Pod上,您首先需要创建一个引用密钥文件的Boto配置文件:

# This option is only necessary if you're running an installation of
# gsutil that came bundled with gcloud. It tells gcloud that you'll be
# managing credentials manually via your own Boto config files.

$ gcloud config set pass_credentials_to_gsutil False


# Set up your boto file at /path/to/my/boto.cfg - the setup will prompt
# you to supply the /path/to/your/keyfile.json.  Alternatively, to avoid
# interactive setup prompts, you could set up this config file beforehand
# and copy it to the pod.

$ gsutil config -e -o '/path/to/my/boto.cfg'

最后,每当您运行gsutil时,都需要告诉它在哪里可以找到引用您的JSON密钥文件的Boto配置文件(并确保运行该命令的用户有权读取Boto配置文件和JSON密钥文件)。如果您将Boto配置文件写入了我上面提到的知名路径之一,则gsutil会尝试自动找到它;如果没有,您可以通过在为cron作业提供的命令中导出BOTO_CONFIG环境变量来告诉gsutil在哪里可以找到Boto配置文件:

export BOTO_CONFIG=/path/to/my/boto.cfg; /path/to/gsutil cp <src> <dst>

修改

请注意,GCE VM映像随附于/etc/boto.cfg中的预填充文件。此配置文件告诉gsutil加载插件,该插件允许gsutil以这种方式与GCE元数据服务器联系并获取身份验证令牌(与该VM的default机械手服务帐户相对应)。如果您的Pod能够读取主机VM的/etc/boto.cfg文件,则可以联系GCE元数据服务器,并且可以接受由VM的default服务帐户执行的操作,解决方案应该是开箱即用的。

答案 1 :(得分:0)

请注意,您的Kubernetes服务帐户与Google Cloud Storage服务帐户不同。

... ans_line = [] for line in tri_2: split_line = line.split() input_numbers = map(int, split_line) result = is_triangle(*input_numbers) ans_line.append(result) ... 使用boto配置,因此您可以在gsutil/etc/boto.cfg

下安装Kubernetes机密。

您可以使用令牌或服务帐户向GCP进行身份验证。您可以使用~/.boto生成令牌,也可以使用gsutil config -f生成服务帐户凭据。它将生成一个gsutil config -e文件,然后您可以将其作为Kubernetes机密安装在您的Pod上。

更多信息here

相关问题