如何使用 gcloud 的多个服务帐户?
How to use multiple service accounts with gcloud?
我有两个 Google 云服务帐户;我的两个项目各一个。
# ACCOUNTS
editor@someproj-1.iam.gserviceaccount.com
editor@someproj-2.iam.gserviceaccount.com
我可以在执行命令之前告诉gcloud
我需要使用哪个帐户:
gcloud set account [ACCOUNT]
问题: 有什么方法可以配置 gcloud
和 gsutil
以便它们将用于在各自项目中执行的操作而无需我必须一直在这些帐户之间手动切换?
我在一个项目中管理实例,而我在另一个项目中 upload/download 存储桶中的文件。在命令之间一直执行 gcloud set_account [ACCOUNT]
变得非常乏味。
我需要在两个项目中同时运行ning long-运行ning命令这让我觉得如果我activate/de-activate我会掉坑用于这些命令的帐户。
也许我唯一的选择是从两个不同的 Docker 容器中 运行 google-cloud-sdk?
这里有几个选项:
Cloud SDK 遵守指定属性的环境变量。 gcloud config set account
对于gcloud config set core/account
是shorthand,所以对应的属性是CLOUDSDK_CORE_ACCOUNT
.
您可以这样做:
$ CLOUDSDK_CORE_ACCOUNT=email1@domain1.com gcloud ...
$ CLOUDSDK_CORE_ACCOUNT=email2@domain2.com gcloud ...
这应该能让您得到您感兴趣的结果。
如果您需要多个 属性 更改,Cloud SDK 提供了一个 named configuration 抽象。有关完整详细信息,请参阅文档,但您可以 运行:
$ gcloud config configurations create my-project1-config
$ gcloud config configurations activate my-project1-config
$ gcloud auth login # or activate-service-account
$ gcloud config set project project1 # and any other configuration you need to do
$
$ gcloud config configurations create my-project2-config
$ gcloud config configurations activate my-project2-config
$ gcloud auth login # or activate-service-account
$ gcloud config set project project2 # and any other configuration you need to do
$
$ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project1-config gcloud ...
$ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project2-config gcloud ...
在最极端的情况下,您可以维护单独的 Cloud SDK 配置目录。默认值(在 *nix 上)是 ~/.config/gcloud
:
$ CLOUDSDK_CONFIG=/tmp/tmpconfig1 gcloud auth login
$ CLOUDSDK_CONFIG=/tmp/tmpconfig2 gcloud auth login
Zachary 的回答非常有用,但有一种更简单的方法可以使用 gcloud 的配置。
运行 gcloud config configurations list
显示您的配置列表。如果您还没有进行任何操作,它只会列出 default
以及您当前处于活动状态的帐户、项目等。
使用 gcloud config configurations create [config name]
创建新配置:
> gcloud config configurations create testconfig
Created [testconfig].
Activated [testconfig].
新配置现在将生效,所以请继续使用 gcloud init
:
进行设置
> gcloud init
Welcome! This command will take you through the configuration of gcloud.
然后它会问你一系列问题:
- 当它要求您选择要使用的配置时,请选择
[1] Re-initialize this configuration [testconfig] with new settings
。
- 然后它会要求您选择或登录一个帐户。
- 然后它会要求您选择或创建一个项目。
- 最后,它会询问您是否要为项目设置默认区域。由你决定;在一切都在同一区域的项目上,继续进行设置。
Your Google Cloud SDK is configured and ready to use!
使用 gcloud config configurations activate [config name]
切换帐户。
我有两个 Google 云服务帐户;我的两个项目各一个。
# ACCOUNTS
editor@someproj-1.iam.gserviceaccount.com
editor@someproj-2.iam.gserviceaccount.com
我可以在执行命令之前告诉gcloud
我需要使用哪个帐户:
gcloud set account [ACCOUNT]
问题: 有什么方法可以配置 gcloud
和 gsutil
以便它们将用于在各自项目中执行的操作而无需我必须一直在这些帐户之间手动切换?
我在一个项目中管理实例,而我在另一个项目中 upload/download 存储桶中的文件。在命令之间一直执行 gcloud set_account [ACCOUNT]
变得非常乏味。
我需要在两个项目中同时运行ning long-运行ning命令这让我觉得如果我activate/de-activate我会掉坑用于这些命令的帐户。
也许我唯一的选择是从两个不同的 Docker 容器中 运行 google-cloud-sdk?
这里有几个选项:
Cloud SDK 遵守指定属性的环境变量。
gcloud config set account
对于gcloud config set core/account
是shorthand,所以对应的属性是CLOUDSDK_CORE_ACCOUNT
.您可以这样做:
$ CLOUDSDK_CORE_ACCOUNT=email1@domain1.com gcloud ... $ CLOUDSDK_CORE_ACCOUNT=email2@domain2.com gcloud ...
这应该能让您得到您感兴趣的结果。
如果您需要多个 属性 更改,Cloud SDK 提供了一个 named configuration 抽象。有关完整详细信息,请参阅文档,但您可以 运行:
$ gcloud config configurations create my-project1-config $ gcloud config configurations activate my-project1-config $ gcloud auth login # or activate-service-account $ gcloud config set project project1 # and any other configuration you need to do $ $ gcloud config configurations create my-project2-config $ gcloud config configurations activate my-project2-config $ gcloud auth login # or activate-service-account $ gcloud config set project project2 # and any other configuration you need to do $ $ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project1-config gcloud ... $ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project2-config gcloud ...
在最极端的情况下,您可以维护单独的 Cloud SDK 配置目录。默认值(在 *nix 上)是
~/.config/gcloud
:$ CLOUDSDK_CONFIG=/tmp/tmpconfig1 gcloud auth login $ CLOUDSDK_CONFIG=/tmp/tmpconfig2 gcloud auth login
Zachary 的回答非常有用,但有一种更简单的方法可以使用 gcloud 的配置。
运行 gcloud config configurations list
显示您的配置列表。如果您还没有进行任何操作,它只会列出 default
以及您当前处于活动状态的帐户、项目等。
使用 gcloud config configurations create [config name]
创建新配置:
> gcloud config configurations create testconfig
Created [testconfig].
Activated [testconfig].
新配置现在将生效,所以请继续使用 gcloud init
:
> gcloud init
Welcome! This command will take you through the configuration of gcloud.
然后它会问你一系列问题:
- 当它要求您选择要使用的配置时,请选择
[1] Re-initialize this configuration [testconfig] with new settings
。 - 然后它会要求您选择或登录一个帐户。
- 然后它会要求您选择或创建一个项目。
- 最后,它会询问您是否要为项目设置默认区域。由你决定;在一切都在同一区域的项目上,继续进行设置。
Your Google Cloud SDK is configured and ready to use!
使用 gcloud config configurations activate [config name]
切换帐户。