LAB17_GCP_Kubernetes: Deploying a GKE Autopilot Cluster

Kubernetes: Deploying a GKE Autopilot Cluster

☁ GCP 🟢 Beginner GKE Autopilot Kubernetes Containers

Scenario

Your team wants to deploy workloads to Kubernetes, but they don't want the operational burden of managing Node Groups, VM scaling, or Kubernetes version upgrades. GKE Autopilot is an operation-mode in Google Kubernetes Engine where Google manages the cluster infrastructure, nodes, and scaling entirely for you. You only pay for the Pods that are running.

Your Objectives
  • Create a google_container_cluster resource.
  • Set enable_autopilot = true (this fundamentally changes how the cluster provisions).
  • Set the cluster to run in a region rather than a single zone (Autopilot requires this).
  • Output the cluster's endpoint and client certificate to prove cluster creation.

Additional Context

Autopilot Restrictions: Because Google manages the nodes, you do not create a `google_container_node_pool`. If you try to declare a node pool in Terraform next to an autopilot cluster, it will fail. Furthermore, you cannot SSH into nodes or install host-level agents (DaemonSets have restrictions) because the nodes are abstracted away.

Network Configuration: In production, you would attach this cluster to a custom VPC network and specify secondary IP ranges for Pods and Services (VPC-native routing). For this simplified lab, it uses the `default` VPC network.

variables.tf

variable "project_id" { type = string }
variable "gcp_region" { type = string; default = "us-central1" }

main.tf

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

# GKE Autopilot Cluster
resource "google_container_cluster" "autopilot_cluster" {
  name     = "app-autopilot-cluster"
  
  # Autopilot clusters must be regional, so use `location = region` rather than a zone
  location = var.gcp_region

  # This boolean entirely shifts the cluster type from Standard to Autopilot
  enable_autopilot = true

  # Though not strictly required, it's best practice to specify the network explicitly
  network    = "default"
  subnetwork = "default"

  # We can set a release channel to let Google slowly roll out k8s upgrades
  release_channel {
    channel = "REGULAR" 
  }

  # Deletion protection is true by default in Terraform. False allows destroy.
  deletion_protection = false
}

outputs.tf

output "cluster_name" {
  value = google_container_cluster.autopilot_cluster.name
}

output "cluster_endpoint" {
  value       = google_container_cluster.autopilot_cluster.endpoint
  description = "The IP address of the Kubernetes API Server"
}

Workflow Commands

terraform init
terraform apply -var="project_id=my-gcp-project-123"

# Generate the kubeconfig file so kubectl can connect to the new cluster
gcloud container clusters get-credentials $(terraform output -raw cluster_name) \
    --region us-central1

# View the nodes (Google provisions them implicitly)
kubectl get nodes

terraform destroy -var="project_id=my-gcp-project-123"
✓ Expected Output After Apply
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

cluster_endpoint = "34.120.67.89"
cluster_name     = "app-autopilot-cluster"