This tutorial will show you on how to build a cluster of Kubernetes in GKE (Google Kubernetes Engine) by using the popular Terraform tool.
What is Terraform?
For those who may not be familiar with Terraform, it is a method that modifies the configuration of cloud resources. The tools are stuff that you like, such as providing GKE on Google Cloud.
In order to explain our desired infrastructure, Terraform Scripts themselves use a declarative human reading. The Terraform method is used to incorporate the changes in the script. This whole process is called infrastructure as code (#IaC), with scripts managed by git or by another source code system.
Prerequisites
Before you can proceed with this tutorial, you will need the following prerequisite steps setup and configured:
1) Create Google Cloud Account and login.
2) Create a project in your Google Cloud Account Cloud Console;
3) Once the project is created and you have it selected in the dropdown, on the left hand side find Kubernetes Engine → Configuration
and enable the Kubernetes Engine API and once it enabled it will as shown below.
4) Install the following
- terraform (At least 0.12)
- gcloud cli ( make sure to gcloud login )
- kubectl
Setup GCloud SDK
First before we can start this tutorial, we going to setup the Google Cloud SDK;
After you have configured the GCloud SDK, initialise it by running the following command. This will allow the SDK to access the GCP using your user account data and connect the SDK to your PATH. This process requires you to log in and specify the project in which you want to work.
Finally, add your account to the Application Default Credentials (ADC). This will allow Terraform to access these credentials to provision resources on GCloud.
Initializing Terraform and Setup Workspace
In this tutorial we going to use Terraform repository from HashiCorp for all require file we need for provision GKE, clone the repository by following command;
We will get all these files once we cloned above repository to our local machine;
- The VPC and subnet provisions from
vpc.tf
file. A new VPC is built for this tutorial so that your current cloud environment and resources are not affected. This file outputsregion
. gke.tf
includes a GKE cluster and a separate managed node pool (recommended). Separately controlled node pools let you configure your Kubernetes cluster profile—this is useful if some Pods need more resources than others.terraform.tfvars
is a template for theproject_id
andregion
variables.versions.tf
sets the Terraform version to at least 0.12.
Finally, we have these all file in your workspace;
Next, you have to upate terraform.tfvars
with your project_id
and region
below;
You can also retrieve your Project ID with following command;
After saving your custom variables file, configure your Terraform workspace, which will download the provider and initialise it with the values given in your terraform.tfvars file.
Execute(Apply) Terraform and Provision GKE
In your initialised directory, "terraform apply" and review the expected terraform plan.
If you're comfortable with this, confirm the run with a yes
.Your terminal output should show which plan is running and which resources will be generated.
This process is expected to take approximately 10 minutes. Upon effective run, your terminal will print outputs specified in vpc.tf and gke.tf. You also can check your GKE provision by going to GCP Console-> Kubernetes Dashboard -> Cluster below;
Otherwise if you facing issue while provisioning GKE, go straight troubeshooting section below.
Troubleshooting
If you get error like below while provisioning the GKE;
This happens when the GCP zone is oversubscribed and is usually temporary. However there is something you can do to protect against this happening to you in the future. There are 2 ways to solve this issue which 1st you can change the region in your terraform.tfvars
or certainly not going to fix the error while the zone has insufficient resources, you can avoid this error in the future by Reserving Zonal Resources in advance—by essentially ensuring that the resources will be available to you when you need them.
There's a lot of configurability in what you can reserve, but the minimal gcloud command to reserve a single instance might look something like this:
gcloud compute reservations create my-reservation --machine-type=n1-standard-1 --zone=us-central1-a --vm-count=1
And to delete it when you are done:
gcloud compute reservations delete my-reservation
You can also do this through the console, on the 'Compute Engine > Commited use discounts > Reservations' tab.
Create and Access Kubernetes Dashboard
To verify that your cluster is configured correctly and running, you will navigate to it in your local browser. We need to create a ClusterRoleBinding
to use the Kubernetes dashboard. This gives the cluster-admin
permission to access the kubernetes-dashboard
. you don't need to configure your Terraform Kubernetes Provider since you create this using Terraform, kubectl
.
Finally, to access the Kubernetes dashboard, run the following command, customized with your cluster name instead of sample-aks-
. This will continue running until you stop the process by pressing CTRL + C
.
You should be able to access the Kubernetes dashboard at http://127.0.0.1:8001/.

If want to destroy any resources you create once you are done with this tutorial. Run the destroy
command and confirm with yes
in your terminal.
Recap / Summary
We had learned how to use Terraform to provision Azure Kubernetest Cluster by following steps;
- Set up Google Cloud SDK and Clone HashiCorp repositiry for this tutorial
- Initialize your Terraform workspace and Terrform variables
- Run / Execute Terraform and Provision the GKE cluster
- Configure kubectl locally
- Access Kubernetes Dashboard
- Clean up your tutorial workspace