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

问题描述 投票:0回答:3

我正在使用下面的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。

google-compute-engine terraform stackdriver google-cloud-stackdriver
3个回答
2
投票

这是创建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 service google-fluentd status


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


0
投票

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

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

metadata_startup_script = "echo hi > /test.txt"

© www.soinside.com 2019 - 2024. All rights reserved.