Ssh

將 Packer 與 Google Cloud 一起使用並通過 Debian Build 獲得 SSH 連接問題

  • April 27, 2020

這是我的 Packer 文件。

   {
   "variables": {
     "account_json": "{{env `packer_account_json`}}"
   },
   "builders": [
     {
       "type": "googlecompute",
       "account_file": "{{user `account_json`}}",
       "project_id": "united-course-124523",
       "source_image": "debian-8-jessie-v20160711",
       "zone": "us-central1-a",
       "instance_name": "hub-{{timestamp}}",
       "image_name": "hub-{{uuid}}",
       "image_description": "Elasticsearch 2.3.4."
     }
   ],
   "provisioners": [
     {
       "type": "shell",
       "inline": [
         "sleep 20",
         "echo \"slept for 20 seconds.\""
       ]
     },
     {
       "type": "file",
       "source": "../scripts/install-elastic.sh",
       "destination": "../scripts/install-elastic.sh"
     },
     {
       "type": "shell",
       "script": "../scripts/install-elastic.sh",
       "pause_before": "3s"
     }
   ]
 }

我執行它,然後得到一個如圖所示的 SSH 錯誤(使用執行命令)

$ packer build elastic-2.3.4.json
googlecompute output will be in this color.

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Creating instance...
   googlecompute: Loading zone: us-central1-a
   googlecompute: Loading image: debian-8-jessie-v20160711 in project united-course-124523
   googlecompute: Loading machine type: n1-standard-1
   googlecompute: Loading network: default
   googlecompute: Requesting instance creation...
   googlecompute: Waiting for creation operation to complete...
   googlecompute: Instance has been created!
==> googlecompute: Waiting for the instance to become running...
   googlecompute: IP: 104.197.225.237
==> googlecompute: Waiting for SSH to become available...
==> googlecompute: Error waiting for SSH: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
==> googlecompute: Deleting instance...
   googlecompute: Instance has been deleted!
==> googlecompute: Deleting disk...
   googlecompute: Disk has been deleted!
Build 'googlecompute' errored: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

我一直在查看有關文件上傳的 Packer 文件https://www.packer.io/docs/provisioners/file.html,並沒有看到任何關於建立 SSH 連接的資訊,並且也一直在檢查遠端 ssh 上的文件在這裡https://www.packer.io/docs/provisioners/shell.html但仍然看不到究竟是什麼問題導致圖像出現錯誤sh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain消息。

我還按照@tekjava 的建議將通信器密鑰和值添加到 ssh,但仍然是相同的錯誤。添加後我的建構器看起來像這樣。

{
 "type": "googlecompute",
 "account_file": "{{user `account_json`}}",
 "project_id": "united-course-124523",
 "source_image": "debian-8-jessie-v20160711",
 "zone": "us-central1-a",
 "instance_name": "{{user `instance_name`}}",
 "image_name": "elastic-{{uuid}}",
 "image_description": "Elasticsearch 2.3.4.",
 "communicator": "ssh"
}

Google Cloud Engine 上有一些圖像預設禁用 root ssh 訪問。Centos、Debian 和新的 Container-VM 映像似乎就在其中。似乎可以通過指定要使用的使用者名來解決:

"ssh_username": "anythingyoulike"

…然後由 Packer 在建構期間創建。

如果您在項目級別啟用了 os login,您可能需要為 packer VM 禁用它,即將它添加到您的googlecompute建構器配置中:

     "metadata": {
       "enable-oslogin": "FALSE"
     }

引用自:https://serverfault.com/questions/792328