Terraform & GCP:Google kubernetes 集群问题:在工作负载(部署、状态集)中看不到监控部分(内存和 cpu)

Terraform & GCP: Google kubernetes cluster problem: Can't see monitoring section (memory and cpu) inside workloads (deployments, statefulsets)

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

这是我的代码:

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
}

这里有一些关于集群的信息(当我与启用指标的标准集群进行比较时,我看不出有什么不同:

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

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

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

Terraform 代码段

### 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 中显示。

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

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