Terraform — Provision Google Kubernetes Engine(GKE) Cluster

Prashant Bhatasana
4 min readOct 13, 2023

In this article, we are talking about How we can provision Google Kubernetes Engine(GKE) Cluster with terraform script.

Terraform — Provision Google Kubernetes Engine(GKE) Cluster

Google Kubernetes Engine (GKE) is a powerful managed Kubernetes service provided by Google Cloud Platform (GCP). It allows you to deploy, manage, and scale containerized applications using Kubernetes, an open-source container orchestration platform. GKE simplifies the process of creating Kubernetes clusters and handling the underlying infrastructure, making it an ideal choice for developers and DevOps teams.

Terraform, on the other hand, is an open-source infrastructure-as-code (IaC) tool that enables you to define and manage your infrastructure declaratively. With Terraform, you can create, modify, and destroy resources across various cloud providers, including GCP, using simple configuration files.

Prerequisites

Before you start setting up GKE with Terraform, make sure you have the following prerequisites:

  • Google Cloud Platform Account: You need an active GCP account. You can sign up for a free trial if you don't have one.
  • Google Cloud SDK: Install the Google Cloud SDK on your local machine. This SDK provides command-line tools for interacting with GCP services.
  • Terraform: Install Terraform on your machine. You can download the appropriate binary for your operating system from the official website.
  • Google Cloud IAM Credentials: Create a Service Account on GCP with appropriate permissions to manage GKE clusters. Download the JSON key file for the service account and keep it secure.

So, let’s start!

.
└── tf-gke-project/
├── modules/
│ └── gke_cluster/
│ ├── main.tf
│ ├── variables.tf
│ └── output.tf
├── provider.tf
├── production.tf
├── variables.tf
└── terraform.tfvars

Project Initialization

The first step is to initialize a new Terraform project. Please create a new directory for your project and navigate into it.

mkdir tf-gke-project
cd tf-gke-project

Configure Terraform Provider

In the provider.tf file, we need to define the Terraform provider for GCP. This tells Terraform which cloud platform to interact with and sets up the required credentials.

provider "google" {
credentials = file("<path/to/your/google-credentials.json>")
project = "<gcp-project-id>"
region = "us-east4"
}

Ensure you replace "<path/to/your/google-credentials.json>" with the path to your GCP service account key, and "<gcp-project-id>" with your GCP project ID.

Create module directory “modules/gke_cluster”

A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.

Goto modules > gke_cluster Directory

Define GKE Cluster Configuration > main.tf

Next, we’ll define the configuration for the GKE cluster.

Define variables > variables.tf

All variables will be in variables.tf file. This way we can pass dynamic values in our module.

variable "region" {
description = "Deployment region"
default = "us-east4"
}
variable "clusterName" {
description = "Name of our Cluster"
}
variable "diskSize" {
description = "Node disk size in GB"
}
variable "minNode" {
description = "Minimum Node Count"
}
variable "maxNode" {
description = "maximum Node Count"
}
variable "machineType" {
description = "Node Instance machine type"
}

Export resource attributes > output.tf

We can export any details from created resources and give that as input for another module.

output "cluster_endpoint" {
value = google_container_cluster.gke_cluster.endpoint
}

Now we are ready to use our gke_cluster module.

Now come back to our project’s root directory.

Define variables

This is the same as the above variable.tf file just declares all variables that we are using in our created module’smain.tf a file so we can get all variable's values from production.tf file that we will create in the below steps.

If you are using terraform.tfvars you just need to add a description only.

Configure values of our defined variables

To persist variable values, create a file, and assign variables within this file. Create a file named terraform.tfvars with the following contents:

region="us-east4"
clusterName="tf-cluster"
diskSize=50
minNode=1
maxNode=3
machineType="e2-medium"

In the above configuration, we’re setting up a GKE cluster with three nodes of e2-medium machine type and 50GB disk size. You can modify these parameters terraform.tfvars as per your needs.

The main file that calls module > production.tf

production.tf files in your project directory when you run terraform plan or terraform apply together from the root module. That module may call other modules and connect them by passing output values from one to the input values of another. To learn how to use modules, see the Modules Configuration section.

module "gke_cluster" {
source = "./modules/gke_cluster"

region = var.region
clusterName = var.clusterName
diskSize = var.diskSize
minNode = var.minNode
maxNode = var.maxNode
machineType = var.machineType
}

Now, We are ready to init!

Run terraform init that downloads all module information and downloads terraform in your project file.

Initialize and Apply the Configuration

Once you have defined the Terraform configuration, navigate to the project directory and run the following commands:

terraform init
terraform plan
terraform apply -auto-approve

Terraform will initialize itself, download the necessary providers, and validate the configuration. After the validation, it will start resource provisioning.

Access the GKE Cluster

Once Terraform has completed the setup, it will provide you with the output of the GKE cluster.

You can access the GKE cluster using kubectl the command-line tool by configuring it with the provided credentials.

gcloud container clusters get-credentials <CLUSTER-NAME> --region us-east4 --project <PROJECT-ID>

Now you can interact with your GKE cluster using kubectl.

kubectl cluster-info

Congratulations 🙌🥂🎉 ! We have successfully set up a GKE cluster using Terraform.

Thank you for reading, if you have anything to add please send a response or add a note!

Happy deploying!

--

--

Prashant Bhatasana

AWS Community Builder | AWS Certified | Terraform Associate | DevOps Engineer, Love to work with #AWS #Terraform #Jenkins #Kubernetes #Docker #Ansible #Selenium