如何使用 Terraform 脚本在 GCP 中的 LINUX(Ubuntu/Debian) GCE 实例中安装 stackdriver?

How to install stackdriver in LINUX(Ubuntu/Debian) GCE instances in GCP using Terraform script?

我正在使用以下 terraform 脚本创建 LINUX Debian 实例。

resource "template_dir" "config" {
  source_dir      = "${path.module}/config.d/"
  destination_dir = "/tmp/fluent-templates"

  vars = {
    instance-name = "${var.instance_name}"
  }
}

resource "google_compute_instance" "default" {
  name         = "${var.instance_name}"
  project      = "${var.project}"
  machine_type = "${var.machine_type}"
  zone         = "${var.zone}"

  boot_disk {
    initialize_params {
      image = "${var.boot_disk_image}"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }


  #StackDriver must be installed before this command runs,
  #as it will create the "/etc/google-fluentd/config.d" directory,
  #which is supposed to be replaced by the below provisioner

  provisioner "file" {
    source      = "${template_dir.config.destination_dir}"
    destination = "/etc/google-fluentd/config.d"
  }
}

我想使用 Terraform 在这些 Debian/Ubuntu 上安装 StackDriver Logging Agent 以避免手动使用 SSH,并在每次启动实例时安装它。

我尝试使用 remote-exec,但它对我不起作用。下面是 remote-exec:

的代码
provisioner "remote-exec" {
    inline = [
      "curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh",
      "bash install-logging-agent.sh",
    ]
  }

将上述代码放入我的 terraform 脚本的 google_compute_instance 资源中不起作用,并在大约 5 分钟后连接失败并出现以下错误:

* google_compute_instance.default:
 timeout - last error: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

我不确定如何连接到服务器以使用 remote-exec

看来您应该能够使用启动脚本字段来指定代理安装:

https://www.terraform.io/docs/providers/google/r/compute_instance.html

metadata_startup_script = "echo hi > /test.txt"

这里是创建 Terraform 脚本的link:

https://cloud.google.com/community/tutorials/getting-started-on-gcp-with-terraform

添加以下元数据以安装 Stackdriver Logging 代理:

metadata_startup_script = "curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh; sudo bash install-logging-agent.sh"

最后通过 SSH 进入实例并检查服务状态:

$ sudo 服务 google-流利状态

使用 remote-exec 最适合我。

provisioner "remote-exec" {
    inline = [
      "curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh",
      "sudo bash install-logging-agent.sh",
      "rm install-logging-agent.sh",
    ]
    connection {
            type  = "ssh"
            user  = "${var.gce_ssh_user}"
            private_key = "${file(var.gce_ssh_private_key_file)}"
            timeout = "60s"
        }
  }

在启动 LINUX 实例时,使用 remote-execssh 进入实例,运行 这两个命令提到一个 Installing the agent on Linux and Windows 页。

我添加了 rm install-logging-agent.sh 以在安装完成后删除脚本。