【问题标题】:How to install stackdriver in LINUX(Ubuntu/Debian) GCE instances in GCP using Terraform script?如何使用 Terraform 脚本在 GCP 中的 LINUX(Ubuntu/Debian)GCE 实例中安装 stackdriver?
【发布时间】:2019-04-05 11:22:32
【问题描述】:

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

【问题讨论】:

  • 如果您想使用它进行远程配置,您需要为实例设置ssh auth。

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


【解决方案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 service google-fluentd 状态

【讨论】:

    【解决方案2】:

    使用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 以在安装完成后删除脚本。

    【讨论】:

      【解决方案3】:

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

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

      metadata_startup_script = "echo hi > /test.txt"

      【讨论】:

        猜你喜欢
        • 2018-04-17
        • 1970-01-01
        • 2020-09-30
        • 1970-01-01
        • 1970-01-01
        • 2020-03-17
        • 2021-07-15
        • 2021-05-29
        相关资源
        最近更新 更多