How to Deploy a GKE Cluster from CLI with Terraform

How to Deploy a GKE Cluster from CLI with Terraform

Last week I started to try out Google Cloud Platform (GCP) with a free trial account. I am going to post my findings here as I discover usage in GCP.

My first goal here is to deploy a Google Kubernetes Engine (GKE) cluster using Terraform in GCP.

I am using a Vagrant environment with a bento/centos8 image. So some commands here can change according to your local environment.

The first thing to do is to be able to access gcloud environment from my local environment. To do this I installed Google Cloud SDK by following the official page.

Added Cloud SDK repo information so I can pull needed packages.

sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-sdk]
name=Google Cloud SDK
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM

Installed Cloud SDK

sudo dnf install google-cloud-sdk

Installed kubectl which will let me manage my kubernetes cluster locally.

sudo dnf install  kubectl

Now I need to initialize gcloud environment with the following command:

gcloud init

At this step, it will give me a link that will return a code upon entering it to the browser. Where I will enter it on our CLI to give our environment access to the Google Cloud account.

Now I have access, I can continue with Terraform setup.

To install Terraform I used brew package manager.

brew install terraform

Now I will need to define Google credentials for Terraform to use. There are two different ways to do this.

Auth with gcloud

gcloud auth application-default login

or create a separate service account from Google Console, create a key, and download in .json format. Put it to the home directory, then set it as an environment variable. Example command will be like this:

export GOOGLE_APPLICATION_CREDENTIALS=~/terraformtest-307604-3bd499771da4.json

Also putting this cmd to the .bashrc file will ensure that it is set on every login.

Now we need to download Hashicorp's learn-terraform-provision-gke-cluster repository

git clone https://github.com/hashicorp/learn-terraform-provision-gke-cluster

log in to the repo directory

cd learn-terraform-provision-gke-cluster

Update terraform.tfvars file.

# terraform.tfvars
project_id = "REPLACE_ME"
region     = "us-central1-c"

Here project_id is not the same thing as your project name so you need to check the id with this command.

gcloud config get-value project

Also for the region parameter, I will be going to use zone instead of region. So it will be us-central1-c which is currently the suggested zone for free-tier accounts.

I put zone variable as region here because I am going to do zonal deployment instead of regional deployment. Because as stated on free trial program page :

No cluster management fee for one Autopilot or Zonal cluster per billing account.

As I go with zonal deployment and I want my deployment as simple as possible, I am also going to remove vpc configs.

To do this comment out following lines in gke.tf file like this:

#  network    = google_compute_network.vpc.name
#  subnetwork = google_compute_subnetwork.subnet.name

and also comment out following lines on vpc.tf file like this:

# VPC
#resource "google_compute_network" "vpc" {
#  name                    = "${var.project_id}-vpc"
#  auto_create_subnetworks = "false"
#}

# Subnet
#resource "google_compute_subnetwork" "subnet" {
#  name          = "${var.project_id}-subnet"
#  region        = var.region
#  network       = google_compute_network.vpc.name
#  ip_cidr_range = "10.10.0.0/24"
#}

Now I am ready to deploy our GKE cluster. First, run init to setup terraform configs:

terraform init

Then deploy GKE cluster with apply:

terraform apply

When this command finishes executing, it will show me all information regarding my newly created cluster.

To manage the GKE cluster locally I need to get credentials with the following command:

gcloud container clusters get-credentials <cluster name>

After this, I can use kubectl to manage the GKE cluster.

When I want to destroy our GKE cluster I can just run:

terraform destroy

There you go. We checked how to deploy a GKE cluster from CLI. Make sure to check files inside the repository and change some parameters to see how you can manage your cluster further from a simple Terraform deployment.

Be sure to check the following links to learn more: