Deploy Kubernetes on Google Cloud with Terraform

☸️ Kubernetes 🌐 Google Cloud Platform πŸ—οΈ Terraform

Configuration Files

Production-ready configuration files with detailed comments and best practices. Each file works together as a complete deployment solution.
# main.tf - GKE Cluster with Terraform

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = var.region
}

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

  # We can't create a cluster with no node pool
  remove_default_node_pool = true
  initial_node_count       = 1

  network    = "default"
  subnetwork = "default"

  # Enable Autopilot for managed Kubernetes
  enable_autopilot = true

  # Workload Identity for secure pod authentication
  workload_identity_config {
    workload_pool = "${var.project_id}.svc.id.goog"
  }
}

# variables.tf
variable "project_id" {
  description = "GCP Project ID"
  type        = string
}

variable "region" {
  description = "GCP region"
  type        = string
  default     = "us-central1"
}

variable "cluster_name" {
  description = "GKE cluster name"
  type        = string
  default     = "my-gke-cluster"
}

Prerequisites

  • GCP account with billing enabled
  • Terraform 1.0+ installed
  • gcloud CLI installed
  • kubectl installed
  • GKE API enabled in project

Deployment Steps

  • 1. Install Terraform and gcloud CLI
  • 2. Create GCP project
  • 3. Enable GKE API: gcloud services enable container.googleapis.com
  • 4. Create service account with GKE permissions
  • 5. Download service account key JSON
  • 6. Set GOOGLE_APPLICATION_CREDENTIALS env var
  • 7. Create main.tf and variables.tf
  • 8. Run: terraform init
  • 9. Run: terraform plan
  • 10. Run: terraform apply
  • 11. Connect: gcloud container clusters get-credentials my-gke-cluster
  • 12. Deploy apps: kubectl apply -f deployment.yaml

Additional Notes

  • ☸️ GKE Autopilot: fully managed Kubernetes
  • πŸ’° ~$75/month for Autopilot cluster
  • πŸ”’ Workload Identity for secure authentication
  • ⚑ Auto-scaling and auto-repair
  • 🎯 Perfect for production workloads
  • ⚠️ Autopilot has some limitations vs Standard mode