How to Launch a Google Kubernetes Engine (GKE) Autopilot Cluster Using Terraform
Google Kubernetes Engine (GKE) Autopilot is a fully managed, optimized Kubernetes experience that allows you to focus more on your applications and less on managing the underlying infrastructure. Autopilot automates cluster provisioning, scaling, and management while enforcing best practices for Kubernetes, making it an excellent choice for developers and DevOps teams looking for a simplified Kubernetes environment. In this article, we’ll walk you through the steps to launch a GKE Autopilot cluster using Terraform.
Prerequisites
Before you begin, ensure that you have the following:
- Google Cloud Account: An active Google Cloud account with a project set up. If you don’t have one, sign up at Google Cloud.
- Terraform Installed: Terraform should be installed on your local machine. You can download it from the Terraform website.
- GCP Service Account Key: You’ll need a service account key with appropriate permissions (e.g., Kubernetes Engine Admin, Compute Admin). Download the JSON key file for this service account.
Step 1: Set Up Your Terraform Directory
Create a new directory for your Terraform configuration files.
mkdir gcp-terraform-autopilot
cd gcp-terraform-autopilot
Step 2: Create the Terraform Configuration File
In your directory, create a file named main.tf
. This file will contain the configuration for your GKE Autopilot cluster.
touch main.tf
Open main.tf
in your preferred text editor and add the following configuration:
# main.tf
provider "google" {
project = "<YOUR_GCP_PROJECT_ID>"
region = "us-central1"
credentials = file("<PATH_TO_YOUR_SERVICE_ACCOUNT_KEY>.json")
}
resource "google_container_cluster" "autopilot_cluster" {
name = "terraform-autopilot-cluster"
location = "us-central1"
# Enabling Autopilot mode
autopilot {
enabled = true
}
networking {
network = "default"
subnetwork = "default"
}
initial_node_count = 0
ip_allocation_policy {}
}
Explanation of the Configuration
- Provider Block: Specifies the GCP provider, including the project ID, region, and credentials.
- google_container_cluster Resource: Defines the GKE cluster in Autopilot mode, specifying the name and location. The
autopilot
block enables Autopilot mode. Thenetworking
block specifies the network and subnetwork configurations. Theinitial_node_count
is set to 0 because node management is handled automatically in Autopilot. - ip_allocation_policy: This block ensures IP addresses are automatically allocated for the cluster’s Pods and services.
Step 3: Initialize Terraform
Initialize Terraform in your directory to download the necessary provider plugins.
terraform init
Step 4: Plan Your Infrastructure
Run the terraform plan
command to preview the changes Terraform will make. This step helps you validate your configuration before applying it.
terraform plan
If everything is configured correctly, Terraform will generate a plan to create the GKE Autopilot cluster.
Step 5: Apply the Configuration
Once you’re satisfied with the plan, apply the configuration to create the GKE Autopilot cluster on GCP.
terraform apply
Terraform will prompt you to confirm the action. Type yes
to proceed.
Terraform will now create the GKE Autopilot cluster. This process may take a few minutes.
Step 6: Verify the GKE Autopilot Cluster
After Terraform has finished applying the configuration, you can verify the GKE Autopilot cluster by logging into the GCP Console:
- Navigate to the Kubernetes Engine section.
- You should see the
terraform-autopilot-cluster
running in the list of clusters.
You can also use the gcloud
command-line tool to check the status of your cluster:
gcloud container clusters list --project <YOUR_GCP_PROJECT_ID>
Step 7: Configure kubectl
To interact with your GKE Autopilot cluster, you’ll need to configure kubectl
, the Kubernetes command-line tool.
gcloud container clusters get-credentials terraform-autopilot-cluster --region us-central1 --project <YOUR_GCP_PROJECT_ID>
Now you can run Kubernetes commands to manage your applications and resources on the GKE Autopilot cluster.
Step 8: Clean Up Resources
If you no longer need the GKE Autopilot cluster, you can delete all resources managed by Terraform using the following command:
terraform destroy
This command will remove the GKE Autopilot cluster and any associated resources defined in your Terraform configuration.
Conclusion
Using Terraform to launch a GKE Autopilot cluster provides a streamlined, automated way to manage Kubernetes clusters on Google Cloud. With Terraform’s Infrastructure as Code approach, you can easily version control, automate, and replicate your infrastructure, ensuring consistency and reducing manual errors. GKE Autopilot further simplifies the process by managing the underlying infrastructure, allowing you to focus on developing and deploying applications.