首页 > 解决方案 > Terraform:具有不同区域节点的 GKE 集群

问题描述

我有这个带有 3 个节点的 Terraform GKE 集群。当我部署此集群时,所有节点都位于相同的区域中,即europe-west1-b.

gke-cluster.yml

resource "google_container_cluster" "primary" {
  name = var.cluster_name

  initial_node_count       = var.initial_node_count

  master_auth {
    username = ""
    password = ""

    client_certificate_config {
      issue_client_certificate = false
    }
  }

  node_config {
    //machine_type = "e2-medium"
    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
    ]

    metadata = {
      disable-legacy-endpoints = "true"
    }

    labels = {
      app = var.app_name
    }

    tags = ["app", var.app_name]
  }

  timeouts {
    create = "30m"
    update = "40m"
  }
}

variables.tf

variable "cluster_name" {
  default = "cluster"
}

variable "app_name" {
  default = "my-app"
}

variable "initial_node_count" {
  default = 3
}

variable "kubernetes_min_ver" {
  default = "latest"
}

variable "kubernetes_max_ver" {
  default = "latest"
}

variable "remove_default_node_pool" {
  default = false
}

variable "project" {
  default = "your-project-name"
}

variable "credentials" {
  default = "terraform-key.json"
}

variable "region" {
  default = "europe-west1"
}

variable "zone" {
  type        = list(string)
  description = "The zones to host the cluster in."
  default     = ["europe-west1-b", "europe-west1-c", "europe-west1-d"]
}

并且想知道是否可以将每个节点部署在不同的区域中。如果是,我该如何使用 Terraform?

标签: kubernetesgoogle-cloud-platformterraformgoogle-kubernetes-engineterraform-provider-gcp

解决方案


只需添加以下行

resource "google_container_cluster" "primary" {
  name = "cluster"
  location = "us-central1"
  initial_node_count       = "3"

以创建区域集群。以上将启动 9 个节点,每个区域 (fab) 包含 3 个节点。如果您只希望每个区域有 1 个节点,则只需将 initial_node_count 更改为 1。

更多信息请参见参数参考。


推荐阅读