Deploy Kubernetes Load Balancer & Gateway Services with Terraform on GCP

By Mesue Collins Asibong • Cloud Engineering & IaC • 8 min read Kubernetes and Terraform Architecture

Managing cloud infrastructure manually is a recipe for configuration drift and human error. In this updated technical deep dive—expanded from my hands-on workshop with Google Developer Groups (GDG)—we explore how to use Terraform to provision a production-hardened Google Kubernetes Engine (GKE) cluster with GKE Dataplane V2 (Cilium eBPF), Workload Identity Federation, and container-native Google Cloud Load Balancing.

Terraform GKE & Container-Native Load Balancing Workflow

graph LR TF["Terraform 1.10 Config"] -->|Plan and Apply| GCPAPI["GCP Compute and Container API"] GCPAPI -->|Provision| GKE["GKE Cluster + Dataplane V2"] GKE -->|RBS Annotation| Service["K8s LoadBalancer or Gateway API"] Service -->|Auto-Create NEG| GCLB["Google Cloud Passthrough or App LB"] GCLB -->|Direct Pod IP Routing| Pods["Application Pods"] classDef default fill:#1e293b,stroke:#334155,color:#fff classDef highlight fill:#4285F4,stroke:#fff,color:#fff class TF,GKE,GCLB highlight

Step 1: Provisioning a Hardened GKE Cluster with Separate Node Pools

In production GCP environments, using the default node pool is an anti-pattern because changes to the cluster resource can trigger disruptive node recreation. Instead, we set remove_default_node_pool = true, enable GKE Dataplane V2 (ADVANCED_DATAPATH) for eBPF-based networking and built-in NetworkPolicy enforcement, and enable Workload Identity:

resource "google_container_cluster" "primary" {
  name                     = "prod-gke-cluster"
  location                 = "us-central1"
  remove_default_node_pool = true
  initial_node_count       = 1
  deletion_protection      = false # Set true in production

  # Enable Cilium eBPF Dataplane V2
  datapath_provider = "ADVANCED_DATAPATH"

  # Enable Workload Identity for keyless Pod-to-GCP IAM authentication
  workload_identity_config {
    workload_pool = "${var.project_id}.svc.id.goog"
  }

  # Enable GKE Gateway API Controller
  gateway_api_config {
    channel = "CHANNEL_STANDARD"
  }

  release_channel {
    channel = "REGULAR"
  }
}

resource "google_container_node_pool" "primary_nodes" {
  name       = "general-workload-pool"
  location   = "us-central1"
  cluster    = google_container_cluster.primary.name
  node_count = 2

  autoscaling {
    min_node_count = 2
    max_node_count = 6
  }

  node_config {
    machine_type = "e2-standard-4"
    disk_type    = "pd-balanced"
    disk_size_gb = 50
    oauth_scopes = ["https://www.googleapis.com/auth/cloud-platform"]
    workload_metadata_config {
      mode = "GKE_METADATA"
    }
  }
}

Step 2: Authenticating the Kubernetes Provider Dynamically

Once the control plane is provisioned, Terraform authenticates to the Kubernetes API using short-lived OAuth2 access tokens fetched from google_client_config:

data "google_client_config" "default" {}

provider "kubernetes" {
  host                   = "https://${google_container_cluster.primary.endpoint}"
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(
    google_container_cluster.primary.master_auth[0].cluster_ca_certificate
  )
}

Step 3: Deploying a Regional BackendService L4 Load Balancer

Modern GKE LoadBalancer Services use Regional Backend Services (RBS) via the annotation cloud.google.com/l4-rbs = "enabled" and external_traffic_policy = "Local". Instead of legacy target pools that double-hop traffic across nodes via `kube-proxy`, RBS uses Network Endpoint Groups (NEGs) to preserve client source IPs and route health-checked traffic directly to nodes running active pods:

resource "kubernetes_service_v1" "nginx_lb" {
  metadata {
    name = "nginx-rbs-loadbalancer"
    annotations = {
      "cloud.google.com/l4-rbs" = "enabled"
    }
  }
  spec {
    selector = {
      app = "nginx"
    }
    port {
      port        = 80
      target_port = 8080
      protocol    = "TCP"
    }
    type                    = "LoadBalancer"
    external_traffic_policy = "Local"
  }
}

output "load_balancer_ip" {
  description = "Public Anycast/Regional IP provisioned by GCLB"
  value       = kubernetes_service_v1.nginx_lb.status[0].load_balancer[0].ingress[0].ip
}

Step 4: Verification and Clean-Up

Run terraform init && terraform apply and wait for the Regional Passthrough Network Load Balancer IP to be assigned. You can inspect the underlying NEG health checks via kubectl describe svc nginx-rbs-loadbalancer. When finished with a workshop environment, terraform destroy cleanly deprovisions the forwarding rules, backend services, node pools, and control plane.

Summary

Combining Terraform with GKE Dataplane V2 and Regional Backend Service Load Balancers provides a deterministic, high-throughput foundation for cloud-native workloads on Google Cloud. You gain reproducible environments, container-native routing without double-hop latency, and seamless scaling under peak traffic.