【问题标题】:Terraform & GCP: Google kubernetes cluster problem: Can't see monitoring section (memory and cpu) inside workloads (deployments, statefulsets)Terraform 和 GCP:Google kubernetes 集群问题:看不到工作负载(部署、状态集)内部的监控部分(内存和 cpu)
【发布时间】:2022-01-18 16:59:54
【问题描述】:

我已经花了 4 天时间测试了 kubernetes terraform gcp 模块的所有配置,但我看不到我的工作负载指标,它从来没有显示 CPU 和内存(甚至在 GUI 中创建的标准默认 kubernetes 也已激活此功能。

这是我的代码:

resource "google_container_cluster" "default" {
  provider = google-beta
  name        = var.name
  project     = var.project_id
  description = "Vectux GKE Cluster"
  location    = var.zonal_region
  remove_default_node_pool = true
  initial_node_count       = var.gke_num_nodes
  master_auth {
    #username = ""
    #password = ""
    client_certificate_config {
      issue_client_certificate = false
    }
  }
  timeouts {
    create = "30m"
    update = "40m"
  }
  logging_config {
    enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"]
  }
  monitoring_config {
    enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"]
  }
}

resource "google_container_node_pool" "default" {
  name       = "${var.name}-node-pool"
  project    = var.project_id
  location   = var.zonal_region
  node_locations = [var.zonal_region]
  cluster    = google_container_cluster.default.name
  node_count = var.gke_num_nodes
 
  node_config {
    preemptible  = true
    machine_type = var.machine_type
    disk_size_gb = var.disk_size_gb
    service_account = google_service_account.default3.email
    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
      "https://www.googleapis.com/auth/cloud-platform",
      "compute-ro",
      "storage-ro",
      "service-management",
      "service-control",
    ]
    metadata = {
      disable-legacy-endpoints = "true"
    }
  }

  management {
    auto_repair  = true
    auto_upgrade = true
  }
}


resource "google_service_account" "default3" {
  project      = var.project_id
  account_id   = "terraform-vectux-33"
  display_name = "tfvectux2"
  provider     = google-beta
}

以下是有关集群的一些信息(当我与启用了指标的标准集群进行比较时,我没有发现任何差异:

这是工作负载视图,没有我希望看到的指标:

【问题讨论】:

  • 您使用的是什么 Terraform 版本以及 google-beta 的提供者注册表版本?这是你的完整代码吗?我已经复制了这个(通常创建了 GKE 集群和您的 terraform 脚本)并使用 UI 显示的工作负载创建并形成您的代码没有。但是,我使用您的Service Account 使用 UI 创建了另一个集群,但我无法获得任何工作负载指标。您是否尝试添加 IAM policy for service account 以提供例如默认的 Compute Engine 权限?
  • 我在没有使用 SA 的情况下尝试了您的代码,所以我想这就是根本原因。您需要这个特定的服务帐户吗?您不能在这种情况下使用默认值吗?
  • 非常适合检查并让我知道它对您的工作方式@PjoterS。那我相信你是对的。我可以使用其他服务帐户,是的。我只是想在同一个 terraform 项目中部署一个新的服务帐户。我认为 oauth 会对此有所帮助,但也许我缺少一些范围或其他东西。最后我将使用具有所有访问权限的服务帐户进行一些测试,希望我能够看到指标。再次感谢您的检查,我会尽快回来说明它是否有效。
  • 要使其工作,您需要将google_project_iam_bindingrole/owner 或2 个角色一起使用:"roles/compute.admin"roles/logging.logWriter"。我会做一些额外的测试,明天会发布详细的答案。

标签: google-cloud-platform terraform terraform-provider-gcp


【解决方案1】:

正如我在评论中提到的要解决您的问题,您必须添加 google_service_account_iam_binding 模块并授予您的 Service Account 特定角色 - roles/monitoring.metricWriter。在 cmets 中,我提到您也可以授予 role/compute.admin,但经过另一次测试,我已经运行它没有必要。

下面是一个 terraform sn-p,我用 Service Account 创建了一个名为 sa 的测试集群。我更改了node config 中的一些字段。在您的情况下,您需要添加整个 google_project_iam_binding 模块。

地形片段

### Creating Service Account
resource "google_service_account" "sa" {
  project      = "my-project-name"
  account_id   = "terraform-vectux-2"
  display_name = "tfvectux2"
  provider     = google-beta
}
### Binding Service Account with IAM
resource "google_project_iam_binding" "sa_binding_writer" {
  project = "my-project-name"
  role    = "roles/monitoring.metricWriter"
  members = [
    "serviceAccount:${google_service_account.sa.email}" 
    ### in your case it will be "serviceAccount:${google_service_account.your-serviceaccount-name.email}"
  ]
}

resource "google_container_cluster" "default" {
  provider = google-beta
  name        = "cluster-test-custom-sa"
  project     = "my-project-name"
  description = "Vectux GKE Cluster"
  location    = "europe-west2"
  remove_default_node_pool = true
  initial_node_count       = "1"
  master_auth {
    #username = ""
    #password = ""
    client_certificate_config {
      issue_client_certificate = false
    }
  }
  timeouts {
    create = "30m"
    update = "40m"
  }
  logging_config {
    enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"]
  }
  monitoring_config {
    enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"]
  }
}

resource "google_container_node_pool" "default" {
  name       = "test-node-pool"
  project    = "my-project-name"
  location   = "europe-west2"
  node_locations = ["europe-west2-a"]
  cluster    = google_container_cluster.default.name
  node_count = "1"

  node_config {
    preemptible  = "true"
    machine_type = "e2-medium"
    disk_size_gb = 50
    service_account = google_service_account.sa.email
    ###service_account = google_service_account.your-serviceaccount-name.email
    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
      "https://www.googleapis.com/auth/cloud-platform",
      "compute-ro",
      "storage-ro",
      "service-management",
      "service-control",
    ]
    metadata = {
      disable-legacy-endpoints = "true"
    }
  }

  management {
    auto_repair  = true
    auto_upgrade = true
  }
}

我的屏幕:

整个工作负载

节点工作负载

其他信息

如果您只添加roles/compute.admin,您可能会看到整个应用程序的工作负载,但您无法看到每个节点的工作负载。使用"roles/monitoring.metricWriter",您可以查看整个应用程序工作负载和每个节点的工作负载。要实现您想要的 - 查看节点中的工作负载,您只需要 "roles/monitoring.metricWriter"

您需要使用"google_project_iam_binding",因为如果在 IAM 角色中没有此功能,您将不会拥有新创建的Service Account,并且它会缺少权限。简而言之,您的新 SA 将在 IAM & Admin > Service Accounts 中可见,但在 IAM & Admin > IAM 中将没有条目。

如果您想了解更多关于 IAM 和 terraform 中的绑定的信息,请查看this Terraform Documentation

最后,请记住 Oauth Scope"https://www.googleapis.com/auth/cloud-platform" 可以访问所有 GCP 资源。

【讨论】:

  • 是的@WytrzymałyWiktor,已被接受为答案,非常感谢答案,再次感谢 PjoterS
猜你喜欢
  • 1970-01-01
  • 2019-09-04
  • 2019-03-29
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
相关资源
最近更新 更多