Files
gcp_solution_architecture_a…/skills/google-cloud-iac-patterns/references/gke.md
Jonathan Boniface 71da0ed504
Some checks failed
validation / verify (push) Failing after 12s
chore: housekeeping from the runs of 2026-10-09
- An Execution's files are written as each Deliverable is submitted, marked
  in_progress, so a killed process or one waiting for its user leaves them on
  disk. A write removes files an earlier write left that it no longer has.
- A question about delivered work is answered from that Execution's solution
  guide by the reply writer; a question about the service is answered as
  before. Nothing is started for either.
- Lessons: prove writes only for lessons the Skill holds; the Skill records
  the vendored Skill versions the lessons were read against, status names any
  that changed, and a test fails until they are reviewed.
- Four more code patterns: Firestore, BigQuery, Eventarc and GKE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 11:44:38 +01:00

3.5 KiB

Google Kubernetes Engine

A private Autopilot cluster on an existing subnet, with its node service account and Workload Identity for one workload.

  • enable_autopilot = true makes Google manage the nodes. Node pools, machine types and node counts are then not yours to set: leave out node_pool, node_config and initial_node_count.
  • A regional location gives a control plane in three zones. A zone gives one.
  • The subnet needs two secondary ranges, for Pods and Services, named in ip_allocation_policy.
  • enable_private_nodes = true gives nodes no public address; they then need Cloud NAT to pull images from outside Google.
  • master_authorized_networks_config limits who can reach the control plane. With enable_private_endpoint = true it has no public address at all.
  • Give nodes a dedicated service account with roles/container.defaultNodeServiceAccount, not the Compute Engine default account.
  • Autopilot has Workload Identity on. A Kubernetes service account acts as a Google service account through roles/iam.workloadIdentityUser, with the member serviceAccount:PROJECT.svc.id.goog[NAMESPACE/KSA].
  • deletion_protection is true unless set; take it from a variable.
variable "project_id" {
  type        = string
  description = "Project to deploy into."
}

variable "region" {
  type        = string
  description = "Region for regional resources."
}

variable "network_id" {
  type        = string
  description = "Id of the VPC the cluster is in."
}

variable "subnetwork_id" {
  type        = string
  description = "Id of the subnet for nodes. It has secondary ranges named pods and services."
}

variable "admin_cidr" {
  type        = string
  description = "Range allowed to reach the control plane."
}

variable "deletion_protection" {
  type        = bool
  description = "Whether the cluster is protected from deletion."
  default     = true
}

resource "google_service_account" "nodes" {
  project      = var.project_id
  account_id   = "gke-nodes"
  display_name = "GKE nodes"
}

resource "google_project_iam_member" "nodes_default_role" {
  project = var.project_id
  role    = "roles/container.defaultNodeServiceAccount"
  member  = "serviceAccount:${google_service_account.nodes.email}"
}

resource "google_container_cluster" "workload" {
  project             = var.project_id
  name                = "workload"
  location            = var.region
  enable_autopilot    = true
  deletion_protection = var.deletion_protection

  network    = var.network_id
  subnetwork = var.subnetwork_id

  ip_allocation_policy {
    cluster_secondary_range_name  = "pods"
    services_secondary_range_name = "services"
  }

  private_cluster_config {
    enable_private_nodes    = true
    enable_private_endpoint = false
  }

  master_authorized_networks_config {
    cidr_blocks {
      cidr_block   = var.admin_cidr
      display_name = "administrators"
    }
  }

  cluster_autoscaling {
    auto_provisioning_defaults {
      service_account = google_service_account.nodes.email
    }
  }

  release_channel {
    channel = "REGULAR"
  }

  depends_on = [google_project_iam_member.nodes_default_role]
}

resource "google_service_account" "app" {
  project      = var.project_id
  account_id   = "orders-app"
  display_name = "Orders application"
}

resource "google_service_account_iam_member" "app_workload_identity" {
  service_account_id = google_service_account.app.name
  role               = "roles/iam.workloadIdentityUser"
  member             = "serviceAccount:${var.project_id}.svc.id.goog[orders/orders-app]"
}