【问题标题】:SSH into Compute Engine VM from CD Pipeline从 CD 管道通过 SSH 连接到 Compute Engine 虚拟机
【发布时间】:2018-07-15 19:33:16
【问题描述】:

让我的 Google 服务帐户从 bitbucket 管道通过 ssh 连接到我们的 Compute Engine 实例,我很费时间。

Script:
gcloud compute ssh instance1 --zone us-central1-a --quiet --verbosity=info --command="gracefully shutdown processes" && \
gcloud compute scp ~/ instance1:~/ --zone us-central1-a --quiet --recurse --compress && \
gcloud compute ssh instance1 --zone us-central1-a --quiet --verbosity=info --command="start the services"

ERROR: (gcloud.compute.ssh) Could not SSH into the instance.  It is possible that your SSH key has not propagated to the instance yet. Try running this command again.  If you still cannot connect, verify that the firewall and instance are set to accept ssh traffic.
Permission denied (publickey).
lost connection
ERROR: (gcloud.compute.scp) [/usr/bin/scp] exited with return code [1].

我什至尝试过不使用 gcloud 的老式 SSH 方式。我使用metadata 将公钥添加到实例中。我可以在我的本地机器上通过 SSH 登录,但是使用 bitbucket 管道时,我收到了权限被拒绝错误。

script:
        - echo $PRIVATE_SSH_KEY > ./ssh_key
        - chmod 400 ssh_key
        - ssh -t -t -i ssh_key bitbucket-service-account@<IP>

Permission denied (publickey).

我可能做错了什么?

【问题讨论】:

    标签: ssh google-compute-engine bitbucket-pipelines


    【解决方案1】:

    需要角色https://www.googleapis.com/auth/compute。 将该角色添加到您的 VM 的服务帐户[1]。 遗憾的是,尚未在 Google Cloud 文档中记录。

    [1]https://cloud.google.com/iam/docs/granting-roles-to-service-accounts

    【讨论】:

    • 很遗憾,该角色已附加到服务帐户
    • 检查发布密钥是否与实例 1 中的“./.ssh/authorized_keys”内的密钥匹配,否则您可以通过删除 ~/. ssh/ 目录并再次运行 gcloud ssh 这将生成一个新的 pub 密钥。请记住,由 gcloud 命令创建的私钥将 ~/.ssh/google_compute_engine 。因此手动命令将是 ssh -i ~/.ssh/google_compute_engine用户@ip
    【解决方案2】:

    感谢您帮助 Alessio。经过一番挖掘,我终于弄明白了。

    1. 使用 bitbucket ssh keygen 工具创建 bitbucket 密钥对
    2. 将第 1 步中的公钥复制到 Compute Engine 实例,但请记住在密钥末尾粘贴您的 bitbucket 用户名
    3. ssh -i ~/.ssh/config &lt;Bitbucket-Username&gt;@&lt;IP-OF-VM&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2021-04-05
      • 2021-02-18
      • 2013-07-01
      相关资源
      最近更新 更多