如何使用Terraform脚本在GCP中的LINUX(Ubuntu / Debian)GCE实例中安装堆栈驱动程序?

时间:2019-04-05 11:22:33

标签: google-compute-engine terraform stackdriver google-cloud-stackdriver

我正在使用以下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

3 个答案:

答案 0 :(得分:1)

以下是创建Terraform脚本的链接:

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流利状态

答案 1 :(得分:1)

使用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来删除脚本。

答案 2 :(得分:0)

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

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

metadata_startup_script = "echo hi > /test.txt"