8

使用 Terraform,我需要将文件复制到 Google Compute Engine 实例模板。为此,我通常使用文件配置器,但它不起作用,因为它依赖于SSH 连接,由于需要外部可访问的主机地址而失败。由于实例模板的动态特性,我不知道如何将外部可访问的主机地址分配给实例。

如何实现将文件复制到通过实例模板(通过 Terraform)创建的 GCE 实例?

示例 Terraform google_compute_instance_template 定义

resource "google_compute_instance_template" "node" {
  name = "kubernetes-node-template"
  machine_type = "g1-small"
  can_ip_forward = true
  tags = ["staging", "node"]

  network_interface {
    network = "default"
  }

  provisioner "file" {
    source = "worker/assets/kubelet.service"
    destination = "/etc/systemd/system/kubelet.service"
  }

  connection {
    user = "core"
    type = "ssh"
    private_key = "${file("~/.ssh/id_rsa")}"
  }
}
4

4 回答 4

6

我能够通过以下配置解决此问题。

resource "google_compute_instance" "hubmud" {
    name = "hubmud"
    machine_type = "f1-micro"

    tags = ["buildserver", "jenkins", "central", "terraformer"]
    tags = [ "http-server" ]

    zone = "us-central1-b"

    disk {
        image = "ubuntu-1404-trusty-v20160406"
    }

    network_interface {
        network = "default"
        access_config {}
    }

    provisioner "file" {
        source = "installations.sh"
        destination = "installations.sh"
        connection {
            type = "ssh"
            user = "ubuntu"
            private_key = "${file("~/.ssh/google_compute_engine")}"
        }
    }

    provisioner "remote-exec" {
        inline = [
          "chmod +x ~/installations.sh",
          "cd ~",
          "./installations.sh"
        ]
        connection {
            type = "ssh"
            user = "ubuntu"
            private_key = "${file("~/.ssh/google_compute_engine")}"
        }

    }

    service_account {
        scopes = ["userinfo-email", "compute-ro", "storage-ro"]
    }
}

我使用的 SSH 密钥是由 gcloud 实例生成的,要复制的文件必须采用如图所示的格式。我确实遇到了使用~/或只是./指定文件位置的问题。同样重要的是要注意在“ubuntu”帐户下复制的文件,该帐户似乎是 GCE 在图像上默认设置的 ubuntu 上的默认帐户。

请注意,我还添加了type = "ssh"即使这是连接类型的默认值,所以不需要它。不过,我喜欢对配置文件中的某些内容进行详细说明,所以我添加了它。

于 2016-04-16T18:59:17.983 回答
2

您可以通过 SSH 连接到google_compute_instance(毕竟这是一个具体的、有形的虚拟机),但既不能连接到google_compute_instance_group_manager也不能连接到google_compute_instance_template,因为这些都是抽象。

如这里https://github.com/terraform-providers/terraform-provider-google/issues/52所述,目前这是不可能的。

于 2018-08-15T15:20:02.520 回答
1

也许这个功能是后来添加的,但从 2021 年初开始,您可以使用google_compute_instance_template的可选metadata_startup_script参数来指定一个脚本,该脚本将在每次从模板创建的任何实例启动时运行:

resource "google_compute_instance" "hubmud" {
    name = "hubmud"
    machine_type = "f1-micro"

    tags = ["buildserver", "jenkins", "central", "terraformer"]
    tags = [ "http-server" ]

    zone = "us-central1-b"

    disk {
        image = "ubuntu-1404-trusty-v20160406"
    }

    network_interface {
        network = "default"
        access_config {}
    }

    # As per the documentation for google_compute_instance_template (https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_instance_template#metadata_startup_script)
    # you can set either metadata.startup-script or metadata_startup_script
    # to the contents of a file that will be added to the metadata of
    # any instance created from the template. 
    # In both cases the script will run each time the instance starts. 
    # The difference is that if metadata_startup_script is used and the metadata 
    # value is changed the instance will be destroyed and recreated before 
    # running the new script.
    metadata_startup_script = file("${path.module}/installations.sh")

    service_account {
        scopes = ["userinfo-email", "compute-ro", "storage-ro"]
    }
}

请注意,此机制需要操作系统支持,但正如google_compute_instance 上基础参数的文档所指出的那样:

“大多数基于 linux 的映像将在每次启动时在 shell 中运行 metadata.startup-script 的内容。至少,Debian、CentOS、RHEL、SLES、Container-Optimized OS 和 Ubuntu 映像支持此密钥。Windows 实例需要其他键取决于脚本的格式和您希望它运行的时间。”

早在 Ubuntu 16.04 就进行了测试,该版本在本月底达到了 LTS 的 EOL。所以这个功能已经存在了 5 年多。不清楚它是否在很久以前就被 GCP Terraform 提供者公开了,但它现在可以完美运行。

于 2021-04-16T03:40:35.973 回答
0

当您第一次启动您的实例时,您可以将元数据用于运行脚本和任何操作,但它只能工作一次。对于重播,元数据需要重新创建实例。

metadata = {
    startup-script = "echo LOL SSH CONNECTION > /test.info"
}

OR

metadata = {
    startup-script = "${file("${path.module}/shared/runner/bootstrap.sh")}"
}
于 2019-10-16T06:15:32.467 回答