Skip to content

Instantly share code, notes, and snippets.

@gengwg
Created May 12, 2025 18:45
Show Gist options
  • Save gengwg/628613ec3f951d3d32bb4b1d3ccca141 to your computer and use it in GitHub Desktop.
Save gengwg/628613ec3f951d3d32bb4b1d3ccca141 to your computer and use it in GitHub Desktop.
Provision GKE Cluster on Google Cloud with Terraform.

Steps

1. GCP Account Setup

2. Enable Billing

3. Enable Required APIs

4. Install Tools

  • Install Terraform.
  • Install Google Cloud SDK and authenticate:
    gcloud init          # Log in to GCP
    gcloud auth application-default login

5. Terraform Configuration

# main.tf
provider "google" {
  project = "YOUR_PROJECT_ID"  # Replace with your GCP Project ID
  region  = "us-central1"
}

module "gke" {
  source             = "terraform-google-modules/kubernetes-engine/google"
  project_id         = "YOUR_PROJECT_ID"
  name               = "my-cluster"
  region             = "us-central1"
  network            = "default"
  subnetwork         = "default"
  ip_range_pods      = ""
  ip_range_services  = ""
  node_pools = [{
    name         = "default-node-pool"
    machine_type = "e2-small"
    node_count   = 1
  }]
}

6. Verify APIs Are Enabled

gcloud services list --enabled | grep -E 'container.googleapis.com|compute.googleapis.com'
  • Ensure both APIs appear in the output.

7. Apply Terraform

terraform init && terraform plan
terraform apply

8: Clean Up (Avoid Costs!)

terraform destroy  # Delete the cluster and all associated resources

Notes

Is GKE Free?

  • No, but GCP offers:
    1. Free Tier: $300 credits for new users (valid for 90 days).
    2. Always Free: Limited resources (e.g., 1 non-preemptible f1-micro VM instance/month), but GKE nodes are not included in "Always Free."
    • Cost Note: A small GKE cluster (e.g., 1 node) costs ~$25–$50/month if left running. Always clean up resources after testing!

Cost-Saving Tips

  1. Use Preemptible Nodes: Add preemptible = true to the node pool (nodes last max 24h, cost 80% less).
  2. Small Machine Types: Use e2-small or e2-micro.
  3. Minimize Node Count: Start with node_count = 1.
  4. Delete Clusters: Always run terraform destroy after testing.

Need Help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment