在 Google Cloud 中使用 Packer 时遇到 Debian Build 中的 SSH 连接问题

在 Google Cloud 中使用 Packer 时遇到 Debian Build 中的 SSH 连接问题

这是我的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"
}

答案1

Google Cloud Engine 上的某些映像默认禁用 root ssh 访问。Centos、Debian 和新的 Container-VM 映像似乎就属于其中。似乎可以通过指定要使用的用户名来解决:

"ssh_username": "anythingyoulike"

...它将在构建期间由 Packer 创建。

答案2

如果您在项目级别启用了 os login,则可能需要为 packer VM 禁用它,即将其添加到您的googlecompute构建器配置中:

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

答案3

这似乎与此错误报告在打包程序存储库上。这是一个相当新的错误,后来 Debian 8 映像出现故障debian-8-jessie-v20160329。作为一种解决方法,您可以指定 Debian 映像debian-8-jessie-v20160329并手动应用安全更新。

答案4

尝试使用 SHH Communicator:https://www.packer.io/docs/templates/communicator.html

SSH 通信器通过 SSH 连接到主机。如果您在运行 Packer 的机器上启用了 SSH 代理,它会自动将 SSH 代理转发到远程主机。

相关内容