从Cloud Build容器到Google Compute Engine实例的数据传输问题

时间:2018-09-06 12:32:54

标签: google-cloud-platform continuous-integration google-compute-engine continuous-deployment google-cloud-build

当前,我正在使用 Cloud Build 生成一些需要部署到GCE实例的工件。为此,我尝试将 gcloud 构建器与以下参数一起使用:

- name: 'gcr.io/cloud-builders/gcloud' 
  args: ['compute', 'scp', '--zone=<zone_id>', '<local_path>', '<google compute engine instance name>:<instance_path>']

,构建失败并出现以下错误:

ERROR: (gcloud.compute.scp) 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.

我已经在实例上打开了端口 22 ,但这并没有帮助我。
你们可以帮我解决这个问题吗?
我需要在构建定义中检查/修复哪些点?
 可能是可以给我一个建议,而不是我可以用来将数据从 Cloud Build 容器传送到 GCE 实例的 gcloud 吗?

2 个答案:

答案 0 :(得分:1)

一些尝试:

1。确保可以正常使用此way

Troubleshooting SSH,如果第一步失败。

2。尝试将SSH目标从“ instancename”更改为“ username @ instance”,以指示VM内的用户名称,例如

  

username @ InstanceName

答案 1 :(得分:0)

您必须找到一种方法来生成和定位SSH Key Files,供构建器连接到 GCE实例

  • google_compute
  • google_compute.pub
  • google_compute_known_hosts

它们与您用于直接从 Cloud Shell 或从 Local Computer 连接到实例的实例相同,但是这次必须完成连接由建造者自行完成。

按照SSH Key Generation中所述,以交互方式使文件进入构建器的身份路径(通过cd ~ && pwd对其进行测试,通常是:/builder/home/.ssh)。

建立连接后,请通过gsutil将这些文件复制到 Google Cloud Storage 。此步骤仅需要执行一次。

steps:

- name: 'gcr.io/cloud-builders/gsutil'
  args: ['cp', '-rP', '${_BUIKDER_HOME}', 'gs://${_BUCKET_NAME}/builder/']

substitutions:
  _BUCKET_NAME: <bucket_name>
  _BUIKDER_HOME: <builder_home>

timeout: "60s"

您可以将这些密钥文件带到工作区。如果您愿意,则需要将它们保留在存储中。

此放置的目的是将它们用于重新连接到实例,因为每次启动构建器时,它将被配置回默认阶段,因此文件将不再存在。

密钥文件准备就绪后,您可以按照以下步骤进行scp transfer

steps:

- name: 'gcr.io/cloud-builders/gsutil'
  args: ['cp', '-rP',  'gs://${_BUCKET_NAME}/builder/.ssh'], '_${_BUILDER_HOME}']

- name: 'gcr.io/cloud-builders/gcloud' 
  args: ['compute', 'scp', '--recurse', '--zone', '${_ZONE}', '${_LOCAL_PATH}', '${_USER_NAME}@${_INSTANCE_NAME}:${INSTANCE_PATH}']

substitutions:
  _ZONE: <zone>
  _USER_NAME: <user_name>
  _LOCAL_PATH: <local_path>
  _BUCKET_NAME: <bucket_name>
  _BUILDER_HOME: : <builder_home>
  _INSTANCE_NAME: <instance_name>
  _INSTANCE_PATH: <instance_path>

timeout: "60s"

注意:对copy a directory使用'--recurse'标志或不使用标志仅复制文件。

相关问题