Terraforming GKE

Terraform is your ally

Featured image

Taming Kubernetes: How I Conquered Multi-Environment GKE Deployment with Terraform

Let me tell you a story about how I went from Kubernetes cluster chaos to deployment zen - all thanks to Terraform and Google Kubernetes Engine (GKE). You gotta love Infrastructure as code and most importantly, managed services. I have talk to much about the benefits of managed services in other blog post, this will just showcase the transformation from madness to sanity thanks to the managed services.

The Messy Reality Before Terraform

Picture this: Five different environments (dev, staging, pre-prod, prod, and a random experimental cluster), each manually configured. Each time I needed a new cluster, it was like playing a complex game of “whack-a-config” - tweaking settings, remembering specific configurations, and praying nothing breaks.

Spoiler alert: Something always breaks. 🔥

Enter Terraform: My Deployment Superhero

Terraform became my secret weapon for creating consistent, reproducible GKE clusters in everything I do. Here’s how I transformed my messy multi-environment nightmare into a smooth, repeatable process.

The Magic Terraform Module

module "gke_clusters" {
  source  = "terraform-google-modules/kubernetes-engine/google"
  version = "~> 24.0.0"

  # Create 5 clusters with a single module
  count = 5

  project_id = var.project_id
  name       = "cluster-${["dev", "staging", "preprod", "prod", "experimental"][count.index]}"

  regional   = true
  region     = var.region

  # Standardized node pool configuration
  node_pools = [
    {
      name           = "application1-node-pool"
      machine_type   = "e2-standard-4"
      min_count      = 1
      max_count      = 5
      disk_size_gb   = 100
      auto_upgrade   = true
    },
    {
      name           = "application2-node-pool"
      machine_type   = "e2-standard-4"
      min_count      = 5
      max_count      = 10
      disk_size_gb   = 100
      auto_upgrade   = true
    },
    {
      name           = "services-node-pool"
      machine_type   = "e2-standard-4"
      min_count      = 2
      max_count      = 10
      disk_size_gb   = 100
      auto_upgrade   = true
    }
    
  ]

  # Network and security configs
  network           = google_compute_network.vpc.name
  subnetwork        = google_compute_subnetwork.subnet.name
  ip_range_pods     = "pods-ip-range"
  ip_range_services = "services-ip-range"
}

Why This Approach is a Game-Changer

  1. Consistency is King 👑
    • Identical base configuration across all environments
    • No more “it works on my cluster” syndrome, or even, it works on my computer.
    • Reduced configuration drift
    • Easy and repeatable way to create stuff.
    • Multiple node pools automagically created within the needs of the environemnt.
  2. Environment-Specific Tweaks Made Easy

     # Simple variable to customize per environment
     variable "cluster_configs" {
       type = map(object({
         node_count = number
         machine_type = string
       }))
       default = {
         dev = { node_count = 2, machine_type = "e2-standard-2" }
         prod = { node_count = 5, machine_type = "e2-standard-4" }
       }
     }
        
    
  3. Security and Compliance Baked In
    • Consistent security groups
    • Predefined network policies
    • Automated updates

Real-World Pro Tips

The Hidden Benefits

Beyond technical magic, this approach gave me something priceless: peace of mind 🧘‍♂️

No more late-night config debugging. No more “who set this weird parameter?” moments. Just clean, consistent, reproducible Kubernetes infrastructure.

Gotchas and Lessons Learned

The Final Terraform Incantation

# Initialize
terraform init

# Plan (always plan before apply!)
terraform plan

# Apply with confidence
terraform apply

Wrap-Up: From Chaos to Kubernetes Zen

Terraform isn’t just a tool - it’s a philosophy. It transforms infrastructure from a complex art into a predictable science. Read that again.

So next time you’re drowning in cluster configs, remember: Terraform is your life raft in the Kubernetes ocean. 🌊☁️

Pro Tip: Your mileage may vary. Always test in a safe environment first!

Build On!