【问题标题】:Adding multiple GCP accounts to gcloud, configuring them and switching between them, by example将多个 GCP 帐户添加到 gcloud,配置它们并在它们之间切换,例如
【发布时间】:2021-10-31 07:19:51
【问题描述】:

我有两个不同的 GCP 项目,由两个不同的组织管理,它们都与我的电子邮件相关联。这意味着当我登录 GCP 控制台时,我会在我的项目下拉菜单中看到它们。

我以某种方式(在加入第一个组织时)配置了我的 gcloud 安装以成功连接到第一个组织/项目:

myuser ~ % gcloud config list
[compute]
region = us-east4
zone = us-east1-b
[core]
account = myemail@example.com
disable_usage_reporting = True
project = project-1

Your active configuration is: [default]


Updates are available for some Cloud SDK components.  To install them,
please run:
  $ gcloud components update



To take a quick anonymous survey, run:
  $ gcloud survey

我现在正尝试将配置/项目添加到我的 gcloud 配置中,以便我可以根据需要全天在两个项目之间来回切换:

myuser ~ % gcloud config configurations create project-2
Created [project-2].
Activated [project-2].

但是现在,当我列出我的可用配置时,我只看到project-2

myuser ~ % gcloud config list                               
[core]
disable_usage_reporting = True

Your active configuration is: [project-2]

我现在可以运行什么命令来查看这两个项目配置?以及如何将project-2 连接到我的电子邮件 (myemail@example.com) 和凭据?

【问题讨论】:

    标签: gcloud


    【解决方案1】:

    我建议使用 project-2 以外的名称进行配置,因为这可能会造成混淆。

    注意 我原来的答案不正确。在配置之间切换会在活动帐户之间切换,但经过身份验证的帐户列表会跨配置保留。

    一个配置封装了一个gcloud“环境”。因此,在创建配置后,您需要例如gcloud config set account 将任何现有(经过身份验证的)帐户设置为活动帐户,或gcloud auth login添加新帐户添加到列表中。

    注意在任何配置下运行 gcloud auth login 后,所有配置都会保留帐户列表,但每个配置都会保留活动帐户。 p>

    设置配置的一个挑战是gcloud 命令变得隐含,例如如果您不小心处理gcloud config 集,使用配置的gcloud compute instances delete my-instance 可以使用管理员帐户从生产项目中删除。

    配置的替代方法(也是我的偏好)是始终明确指定配置。是的,它更乏味,是的,你仍然可以自满,但我发现它更方便:

    即总是等等。

    gcloud do-something \
    --project=${PROJECT} \
    --account=${ACCOUNT}
    

    我强烈建议您在编写gcloud 命令时设置标志而不是使用配置。在这种情况下,额外的打字是一种前期成本,具有长期的精度优势。

    使用配置的另一个原因。配置是全局适用的。如果您在多个 shell 中使用 gcloud,则在一个 shell 中更改配置,因为它是全局的,将更改跨 shell 的设置(在同一台机器上)。

    【讨论】:

    • 谢谢@DazWilikin (+1),有没有gcloud 命令列出我在我的机器上设置的所有配置(活动或非活动)?
    • 不客气! gcloud config configurations list
    猜你喜欢
    • 1970-01-01
    • 2015-06-19
    • 2012-09-05
    • 2016-10-10
    • 1970-01-01
    • 2014-04-18
    • 2013-12-27
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多