You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform部署GKE集群节点数异常及默认节点池删除失效问题

问题分析与修复方案

1. remove_default_node_pool = true未生效的原因及修复

启用remove_default_node_pool = true时,必须将initial_node_count设为0——否则GCP会先基于你设置的initial_node_count创建默认节点池,导致删除默认池的操作无法执行。

修改集群配置,将initial_node_count改为0:

resource "google_container_cluster" "chess-cluster-london" {
  name                     = "chess-cluster-london"
  location                 = "europe-west1"
  initial_node_count       = 0  # 必须设为0才能触发默认节点池删除
  resource_labels          = {}
  remove_default_node_pool = true

  # 注:默认池会被删除,这段node_config可直接移除,无需保留
  node_config {
    resource_labels = {}
    tags            = []
    machine_type    = "e2-standard-4"
    disk_size_gb    = 100

    oauth_scopes = [
      "https://www.googleapis.com/auth/cloud-platform"
    ]
  }
}

2. 节点池自动创建3个节点的原因及修复

你指定的location是区域(europe-west1包含3个可用区:b、c、d),如果未明确设置node_locations,GCP会在该区域的所有可用区各部署1个节点,总节点数等于可用区数量(这里是3个)。

要固定节点数为1,需在节点池配置中添加node_locations指定单个可用区:

resource "google_container_node_pool" "chess-node-pool" {
  name           = "chess-node-pool"
  location       = "europe-west1"
  cluster        = google_container_cluster.chess-cluster-london.name
  node_count     = 1
  node_locations = ["europe-west1-b"]  # 指定单个可用区,确保只创建1个节点

  node_config {
    machine_type = "e2-standard-4"
    disk_size_gb = 100
    oauth_scopes = [
      "https://www.googleapis.com/auth/cloud-platform"
    ]
  }
}

完整修复后的脚本

resource "google_container_cluster" "chess-cluster-london" {
  name                     = "chess-cluster-london"
  location                 = "europe-west1"
  initial_node_count       = 0
  resource_labels          = {}
  remove_default_node_pool = true
}

resource "google_container_node_pool" "chess-node-pool" {
  name           = "chess-node-pool"
  location       = "europe-west1"
  cluster        = google_container_cluster.chess-cluster-london.name
  node_count     = 1
  node_locations = ["europe-west1-b"]

  node_config {
    machine_type = "e2-standard-4"
    disk_size_gb = 100
    oauth_scopes = [
      "https://www.googleapis.com/auth/cloud-platform"
    ]
  }
}

额外说明

  • 如果需要在多可用区部署但控制总节点数,可启用节点池自动扩缩容,设置autoscaling块并指定min_node_count和max_node_count为1。
  • 执行terraform plan确认变更符合预期(默认节点池销毁、自定义池仅创建1个节点)后,再执行terraform apply。

内容的提问来源于stack exchange,提问作者Robin Mollah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 18:03:16