[英]Configure gsutil to use kubernetes service account credentials inside of pod
我有一个执行某些备份作业的kubernetes Cronjob,并且备份文件需要上载到存储桶中。 该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 ..
简短的答案是,令牌没有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
服务帐户执行操作,那么此解决方案应该可以解决-of即装即用。
请注意,您的Kubernetes服务帐户与Google Cloud Storage服务帐户不同。
gsutil
使用boto配置,因此您可以在/etc/boto.cfg
或~/.boto
下安装Kubernetes机密
您可以使用令牌或服务帐户向GCP进行身份验证。 您可以使用gsutil config -f
生成令牌,也可以使用gsutil config -e
生成服务帐户凭据。 它将生成一个~/.boto
文件,然后您可以将其作为Kubernetes秘密安装在您的Pod上。
更多信息在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.