Search code examples
terraformgoogle-kubernetes-engineterraform-provider-gcp

creation of a gke cluster using terraform


Hi I am using the below terraform block to create a gke cluster

resource "google_container_cluster" "my-cluster" {
  name     = "my-cluster"
  location = var.zone

  remove_default_node_pool = true
}

resource "google_container_node_pool" "default" {
  name       = "default"
  cluster    = google_container_cluster.my-cluster.name
  location   = google_container_cluster.my-cluster.location
  node_count = 1
}

the gcloud command for this

gcloud container clusters create my-cluster --num-nodes=1 --zone=europe-west2-b

when i execute the terraform plan it is not showing any error

+ location                    = "europe-west2-b"
      + managed_instance_group_urls = (known after apply)
      + max_pods_per_node           = (known after apply)
      + name                        = "default"
      + name_prefix                 = (known after apply)
      + node_count                  = 1

However during terraform apply it fails with the below error complaining on the node count. However in the plan output the node_count is shown as 1.

│ Error: googleapi: Error 400: Cluster.initial_node_count must be greater than zero.
│ Details:
│ [
│   ***
│     "@type": "type.googleapis.com/google.rpc.RequestInfo",
│     "requestId": "0xc636f1e1fe8d9937"
│   ***
│ ]
│ , badRequest

Solution

  • Even though you are deleting the default node pool, it still needs to be created with at least 1 node (and then of course is it deleted).

    resource "google_container_cluster" "my-cluster" {
      name     = "my-cluster"
      location = var.zone
    
      remove_default_node_pool = true
      initial_node_count       = 1
      
    }
    
    resource "google_container_node_pool" "default" {
      name       = "default"
      cluster    = google_container_cluster.my-cluster.name
      location   = google_container_cluster.my-cluster.location
      node_count = 1
    }