简体   繁体   English

如何将 Packer ssh 配置到 GCP VM 中以构建镜像?

[英]How to configure Packer ssh into GCP VM for building image?

I am building GCP image with packer.我正在使用加壳器构建 GCP 映像。 I created service account of "Compute Instance Admin v1" and "Service Account User".我创建了“Compute Instance Admin v1”和“Service Account User”的服务帐户。 It can successfully create the VM but cannot ssh into the instance to proceed further for the custom image.它可以成功创建 VM,但不能 ssh 进入实例以进一步处理自定义映像。

Error message错误信息

Build 'googlecompute.custom-image' errored after 2 minutes 20 seconds: Packer experienced an authentication error when trying to connect via SSH. This can happen if your username/password are wrong. You may want to double-check your credentials as part of your debugging process. original error: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

build file source code (packer.pkr.hcl)构建文件源代码 (packer.pkr.hcl)

locals {
  project_id              = "project-id"  
  source_image_family     = "rocky-linux-8"           
  source_image_project_id = ["rocky-linux-cloud"]     
  ssh_username            = "packer"                  
  machine_type            = "e2-medium"              
  zone                    = "us-central1-a"  
}

source "googlecompute" "custom-image" {

  image_name = "custom-image"  # Name of image to be created
  image_description       = "Custom Image 1"    # Description for image to be created
  project_id              = "${local.project_id}"
  source_image_family     = "${local.source_image_family}"
  source_image_project_id = "${local.source_image_project_id}"
  ssh_username            = "${local.ssh_username}"
  machine_type            = "${local.machine_type}"
  zone                    = "${local.zone}"
}

build {
  sources = ["source.googlecompute.custom-image"]

  #
  # Run arbitrary shell script file
  #
  provisioner "shell" {
    execute_command = "sudo su - root -c \"sh {{ .Path }} \""
    script          = "foo.sh"
  }

}

It appears that you are having trouble connecting via SSH to the Packer-created instance for your GCP image.您似乎无法通过 SSH 连接到 Packer 为您的 GCP 映像创建的实例。 If the username and password are incorrect or if the necessary permissions are not granted, this error message indicates that the authentication process failed.如果用户名和密码不正确或未授予必要的权限,则此错误消息表示身份验证过程失败。 Check to see if the Compute Instance Admin v1 and Service Account User roles have the necessary access rights to resolve this issue.检查 Compute Instance Admin v1 和 Service Account User 角色是否具有解决此问题所需的访问权限。 In addition, the project's firewall rules may need to be set up to allow incoming SSH connections on the port you're using.此外,项目的防火墙规则可能需要设置为允许在您使用的端口上传入 SSH 连接。 You can refer to the official GCP documentation for more information regarding the configuration of firewall rules.您可以参考官方GCP 文档以获取有关防火墙规则配置的更多信息。 You can also connect to the instance and continue troubleshooting the issue by using the "gcloud compute ssh" command.您还可以连接到实例并使用“gcloud compute ssh”命令继续解决问题。

Attaching troubleshooting ssh for reference. 附上故障排除ssh供参考。

The problem is associated with Qwiklab.该问题与 Qwiklab 有关。 I was using the lab environment provided by Qwiklab for testing packer and GCP.我正在使用 Qwiklab 提供的实验室环境来测试加壳器和 GCP。

Once I deployed the same thing on regular GCP project.一旦我在常规 GCP 项目上部署了同样的东西。 The packer ran successfully.打包机成功运行。 it is suggested there may be some constraints in the lab environment of Qwiklab.建议 Qwiklab 的实验室环境可能存在一些限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM