简体   繁体   English

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

[英]Configure gsutil to use kubernetes service account credentials inside of pod

I have a kubernetes Cronjob that performs some backup jobs, and the backup files needs to be uploaded to a bucket. 我有一个执行某些备份作业的kubernetes Cronjob,并且备份文件需要上载到存储桶中。 The pod have the service account credentials mounted inside the pod at /var/run/secrets/kubernetes.io/serviceaccount, but how can I instruct gsutil to use the credentials in /var/run/secrets/kubernetes.io/serviceaccount? 该Pod在/var/run/secrets/kubernetes.io/serviceaccount内的Pod中安装了服务帐户凭据, 但是如何指示gsutil在/var/run/secrets/kubernetes.io/serviceaccount中使用凭据?

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 ..

The short answer is that the token there is not in a format that gsutil knows how to use, so you can't use it. 简短的答案是,令牌没有gsutil知道如何使用的格式,因此您不能使用它。 You'll need a JSON keyfile, as mentioned in the tutorial here (except that you won't be able to use the GOOGLE_APPLICATION_CREDENTIALS environment variable): 您将需要一个JSON密钥文件(如此处的教程中所述)(但您将无法使用GOOGLE_APPLICATION_CREDENTIALS环境变量):

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

Rather than reading from the GOOGLE_APPLICATION_CREDENTIALS environment variable, Gsutil uses Boto configuration files to load credentials. Gsutil不会读取GOOGLE_APPLICATION_CREDENTIALS环境变量,而是使用Boto配置文件加载凭据。 The common places that it knows to look for these Boto config files are /etc/boto.cfg and $HOME/.boto . 它知道查找这些Boto配置文件的常见位置是/etc/boto.cfg$HOME/.boto Note that the latter value changes depending on the user running the command ( $HOME expands to different values for different users); 请注意,后一个值会根据运行命令的用户而变化( $HOME会针对不同的用户扩展为不同的值); since cron jobs usually run as a different user than the one who set up the config file, I wouldn't recommend relying on this path. 由于cron作业通常以与设置配置文件不同的用户身份运行,因此我不建议依赖此路径。

So, on your pod, you'll need to first create a Boto config file that references the keyfile: 因此,在您的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'

And finally, whenever you run gsutil, you need to tell it where to find that Boto config file which references your JSON keyfile (and also make sure that the user running the command has permission to read both the Boto config file and the JSON keyfile). 最后,每当您运行gsutil时,都需要告诉它在哪里可以找到引用您的JSON密钥文件的Boto配置文件(并确保运行该命令的用户具有读取Boto配置文件和JSON密钥文件的权限) 。 If you wrote your Boto config file to one of the well-known paths I mentioned above, gsutil will attempt to find it automatically; 如果您将Boto配置文件写入了我上面提到的知名路径之一,则gsutil会尝试自动找到它; if not, you can tell gsutil where to find the Boto config file by exporting the BOTO_CONFIG environment variable in the commands you supply for your cron job: 如果没有,您可以通过在为cron作业提供的命令中导出BOTO_CONFIG环境变量来告诉gsutil在哪里可以找到Boto配置文件:

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

Edit : 编辑

Note that GCE VM images come with a pre-populated file at /etc/boto.cfg. 请注意,GCE VM映像随附于/etc/boto.cfg中的预填充文件。 This config file tells gsutil to load a plugin that allows gsutil to contact the GCE metadata server and fetch auth tokens (corresponding to the default robot service account for that VM) that way. 该配置文件告诉gsutil加载插件,该插件允许gsutil与GCE元数据服务器联系并以这种方式获取身份验证令牌(对应于该VM的default机器人服务帐户)。 If your pod is able to read the host VM's /etc/boto.cfg file, you're able to contact the GCE metadata server, and you're fine with operations being performed by the VM's default service account, this solution should work out-of-the-box. 如果您的Pod能够读取主机VM的/etc/boto.cfg文件,则可以与GCE元数据服务器联系,并且可以通过VM的default服务帐户执行操作,那么此解决方案应该可以解决-of即装即用。

Note that your Kubernetes Service Account is different from your Google Cloud Storage service account. 请注意,您的Kubernetes服务帐户与Google Cloud Storage服务帐户不同。

gsutil uses the boto config so you can mount a Kubernetes secret under /etc/boto.cfg or ~/.boto gsutil使用boto配置,因此您可以在/etc/boto.cfg~/.boto下安装Kubernetes机密

You can authenticate with GCP using a token or a service account. 您可以使用令牌或服务帐户向GCP进行身份验证。 You can generate a token using gsutil config -f you can generate service account credentials using gsutil config -e . 您可以使用gsutil config -f生成令牌,也可以使用gsutil config -e生成服务帐户凭据。 It will generate a ~/.boto file and then you can mount that as Kubernetes secret on your pods. 它将生成一个~/.boto文件,然后您可以将其作为Kubernetes秘密安装在您的Pod上。

More information here . 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM