gpt4 book ai didi

google-cloud-platform - Terraform 和 GCP : Google kubernetes cluster problem: Can't see monitoring section (memory and cpu) inside workloads (deployments, 状态集)

转载 作者:行者123 更新时间:2023-12-05 04:39:59 25 4
gpt4 key购买 nike

我花了 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
}

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

下面是没有我希望看到的指标的工作负载 View : enter image description here

最佳答案

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

下面是一个 Terraform 片段,我用它创建了一个名为 saService Account 测试集群。我更改了 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 和绑定(bind)的更多信息,请查看 this Terraform Documentation

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

关于google-cloud-platform - Terraform 和 GCP : Google kubernetes cluster problem: Can't see monitoring section (memory and cpu) inside workloads (deployments, 状态集),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70364860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com