Install Google Kubernetes Engine (GKE) Cluster using Terraform

0 / 244
GKE terraform


Introduction and overview


In this article we will learn how to install Google Kubernetes Engine (GKE) Cluster using Devops IaC tool “Terraform”




If you want to learn how to install Terraform, refer this blog -> INSTALL DEVOPS IAC TOOL “TERRAFORM” ON CENTOS 7


Lets get familiar with few terms :


Devops : DevOps is a set of practices that combines software development and information-technology operations which aims to shorten the systems development life cycle and provide continuous delivery with high software quality


Infrastructure as Code (Iac) : Infrastructure as code is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.


Kubernetes : Kubernetes is an open-source container-orchestration system for automating application deployment, scaling, and management. It was originally designed by Google, and is now maintained by the Cloud Native Computing Foundation.


GKE : Kubernetes is an open source solution for managing application containers. With Kubernetes, you can decide when your containers should run, increase, or decrease the size of application containers or check the resource consumption of your application deployments.


Terraform : Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision a datacenter infrastructure using a high-level configuration language known as Hashicorp Configuration Language, or optionally JSON.


Google Cloud Platform (GCP) : Google Cloud Platform, offered by Google, is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products, such as Google Search, Gmail and YouTube.



  1. VM or server installed with terraform
  2. GCP Account with project create


Steps we will follow are:


  1. Login in google cloud console
  2. Create Project & Service Account with Project Editor Role
  3. Download & copy JSON key file credential file
  4. Create, Initialize and apply terraform config


  1. Login in google cloud console
  2. We already have project named “hostbread” created in GCP


Create Service Account:


Select IAM & Admin > Service Account > Click on CREATE SERVICE ACCOUNT

Create Service Account Step 1

Create Service Account Step 1



Create Service Account Step 2

Create Service Account Step 2



Enter service Account details and click on CREATE


Service account name: gke-terraform

Service account iD L gke-terraform


Create Service Account Step 3

Create Service Account Step 3


Next Assign Project Editor Role and click on CONTINUE

Create Service Account Step 4

Create Service Account Step 4


Now click on CREATE KEY

Create Service Account Step 5

Create Service Account Step 5


Tick Create key and click Create. Your browser will download a JSON file containing the details of the service account and a private key that can authenticate as a project editor to your project. Keep this JSON file safe! Anyone with access to this file can create billable resources in your project.


Create Service Account Step 6

Create Service Account Step 6



Create Service Account Step 7

Create Service Account Step 7


We can check Service Account which we created on Service accounts page


Create Service Account Step 8

Create Service Account Step 8



Download & copy JSON key credential file


Create new directory /root/tf/gke-tf-cluster/secret and save the json credentials key


[root@devops tf]# mkdir gke-tf-cluster


[root@devops tf]# cd gke-tf-cluster/


[root@devops gke-tf-cluster]# mkdir secret


[root@devops gke-tf-cluster]# cd secret/


[root@devops secret]# pwd




[root@devops secret]# ls




[root@devops secret]#


We have saved the json credential file at /root/tf/gke-tf-cluster/secret


Create, Initialize and apply terraform config


Create terraform config file


In /root/tf/gke-tf-cluster directory lets create terraform config file


[root@devops secret]# cd /root/tf/gke-tf-cluster


[root@devops secret]# pwd




[root@devops secret]# cd ..


[root@devops gke-tf-cluster]# ls  secret


[root@devops gke-tf-cluster]# cat


provider “google” {

  credentials = file(“/root/tf/gke-tf-cluster/secret/hostbread-5cef426d5a12.json”)

  project = “hostbread”

  region  = “us-central1”



resource “google_container_cluster” “gke-cluster” {

  name               = “gke-tf-cluster”

  network            = “default”

  location               = “us-central1-c”

  initial_node_count = 3



Before initializing & applying we have to initialize & enable GKE API on GCP.

Create main

Create Main


We just need to browse Kubernetes Engine > Cluster

We can see GKE API key being enabled as shown below. This is tricky part of this installation; else we will get an API error.


Initialize GKE API

Initialize GKE API


Initialize terraform config


[root@devops gke-tf-cluster]# terraform init


Terraform Init

Terraform Init


Check our using “terraform plan”


[root@devops gke-tf-cluster]# terraform plan


Let’s apply terraform and when prompted, enter “yes”


[root@devops gke-tf-cluster]# terraform apply


Apply complete! Resources: 1 added, 0 changed, 0 destroyed.


Browse GCP console > Kubernetes Engine > Cluster and you can see we have 3 node GKE cluster is created.


GCP VM provisioned

GCP VM provisioned

GKE connect step 1

GKE connect step 1


Now let’s play with GKE and check if Kubernetes nodes are running fine.


GKE connect step 2

GKE connect step 2



GKE connect step 3

GKE connect step 3


Run below commands in google cloud shell

prayag_rhce@cloudshell:~ (hostbread)$ kubectl get nodes

NAME                                            STATUS   ROLES    AGE     VERSION


gke-gke-tf-cluster-default-pool-a5b5b1c8-2401   Ready    <none>   8m18s   v1.14.10-gke.27


gke-gke-tf-cluster-default-pool-a5b5b1c8-hv9k   Ready    <none>   8m19s   v1.14.10-gke.27


gke-gke-tf-cluster-default-pool-a5b5b1c8-r09r   Ready    <none>   8m19s   v1.14.10-gke.27


prayag_rhce@cloudshell:~ (hostbread)$ kubectl get all


NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE


service/kubernetes   ClusterIP   <none>        443/TCP   9m4s


prayag_rhce@cloudshell:~ (hostbread)$ kubectl cluster-info


Kubernetes master is running at


GLBCDefaultBackend is running at


Heapster is running at


KubeDNS is running at


Metrics-server is running at


To further debug and diagnose cluster problems, use ‘kubectl cluster-info dump’.


GKE connect step 4

GKE connect step 4



GKE connect step 5

GKE connect step 5



prayag_rhce@cloudshell:~ (hostbread)$ kubectl api-versions






















prayag_rhce@cloudshell:~ (hostbread)$


At last destroy  GKE cluster using “terraform destroy” and enter yes when prompted


[root@devops gke-tf-cluster]# terraform destroy


Destroy complete! Resources: 1 destroyed.


GKE connect step 6

GKE connect step 6




Subscribe and follow Golibrary on Facebook and Linkedin to get all the updates.








***Linux, Cloud & Devops Architect & Technical Content Writer*** I am a Linux Enthusiast and Supporter/Promoter of Open Source Technology with over 12+ years of experience in Linux, Cloud and Devops. I am A Technical Content writer for various sites like : Hostbread & Golibrary

Related Posts