【问题标题】:Google GKE startup script not workin for GKE NodeGoogle GKE 启动脚本不适用于 GKE 节点
【发布时间】:2021-01-22 08:31:55
【问题描述】:

我正在使用 terraform 向我的 GKE 节点添加一个启动脚本

provider "google" {
  project = var.project
  region  = var.region
  zone    = var.zone
  credentials = "google-key.json"
}


terraform {
  backend "gcs" {
    bucket = "tf-state-bucket-devenv"
    prefix = "terraform"
    credentials = "google-key.json"
   }
}

resource "google_container_cluster" "primary" {
  name     = var.kube-clustername
  location = var.zone
  remove_default_node_pool = true
  initial_node_count       = 1

  master_auth {
    username = ""
    password = ""

    client_certificate_config {
      issue_client_certificate = false
    }
  }
}

resource "google_container_node_pool" "primary_preemptible_nodes" {
  name       = var.kube-poolname
  location   = var.zone
  cluster    = google_container_cluster.primary.name
  node_count = var.kube-nodecount

  node_config {
    preemptible  = var.kube-preemptible
    machine_type = "n1-standard-1"
    disk_size_gb = 10
    disk_type = "pd-standard"


    metadata = {
      disable-legacy-endpoints = "true",
      startup_script = "cd /mnt/stateful_partition/home && echo hi > test.txt"
    }

    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
    ]
  }
}

当我进入 GCP 界面时,选择节点并查看元数据我可以看到键/值在那里

metadata_startup_script 
#!/bin/bash sudo su && cd /mnt/stateful_partition/home && echo hi > test.txt

但是在节点上运行以下命令时 -

sudo google_metadata_script_runner --script-type 启动 - -调试

我得到了以下 -

startup-script: INFO Starting startup scripts.
startup-script: INFO No startup scripts found in metadata.
startup-script: INFO Finished running startup scripts.

有谁知道为什么这个脚本不工作/出现? - 是不是因为它是一个 GKE 节点,而谷歌不允许你编辑这些,我实际上无法在他们的文档中找到任何他们特别提到的内容。

【问题讨论】:

    标签: google-cloud-platform terraform google-kubernetes-engine


    【解决方案1】:

    您不能指定要在 GKE 节点上运行的启动脚本。节点有一个内置的启动序列来初始化节点并将其加入集群并确保其正常工作(例如,确保当您请求 100 个节点时,您会获得 100 个功能节点)您不能向启动顺序。

    作为替代方案,您可以创建一个在所有节点上运行的DaemonSet,以执行节点级初始化。这样做的一个好处是,如果您想更改它们的配置方式,您可以调整 DaemonSet 并将其重新应用到现有节点(无需重新创建它们)。

    【讨论】:

    • 我实际上是在尝试这样做medium.com/google-cloud/… 我需要将 kube 配置复制到节点上,以便它们可以耗尽节点。那就是问题所在。我确实尝试遵循本指南,但它没有用,托管实例组不加入 kubernetes 集群,如果您更改节点池的实例模板,它会破坏您的整个设置。但我认为理论是存在的。
    • 我看不到没有关闭脚本(GKE 不允许您设置)来捕获关闭信号的方法。因此,除非/直到此功能内置到 GKE 本身中,否则您可能会不走运。如果您有 Google 的支持联系人,我建议您向他们提出功能请求。
    【解决方案2】:

    用这个startup_script替换这个元数据键名metadata_startup_script

    此外,您的启动脚本以 root 用户身份运行。你不需要执行sudo su

    【讨论】:

    • 嘿,还是同样的问题,说没有在元数据中找到启动脚本。 - 我更新了主要问题以显示我的新 terraform
    • 看来你不能做启动脚本。问题是关键应该是启动脚本而不是启动脚本。 - 得到这个 - 错误:创建NodePool时出错:googleapi:错误400:Cluster.node_config.metadata.key不能是保留键“启动脚本”。,badRequest
    • 有道理。用这个回答你的问题,它对其他人有用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2023-02-15
    • 1970-01-01
    相关资源
    最近更新 更多