Provision GKE Cluster with Terraform

A Guide to Deploying Kubernetes with Terraform

Prerequisite

Overview of Tasks

1. Set up a project on your Google cloud account

2. Enable the Compute engine API and kubernetes Engine API

3. Create a Service account

4. Installing and initializing gcloud SDK

brew install --cask google-cloud-sdk
==> Caveats
google-cloud-sdk is installed at /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk. Add your profile:

for bash users
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc'
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc'

for zsh users
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc'
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc'
gcloud init...
You must log in to continue. Would you like to log in (Y/n)?
gcloud auth application-default login

5. Install kubectl

brew install kubernetes-cli

6. Install Terraform

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

7. Create Terraform files needed for the cluster creation

export GOOGLE_APPLICATION_CREDENTIALS={{path}}
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.52.0"
}
}
required_version = "~> 0.14"
}
project_id = "gke-terraform-314918"
region = "europe-west1"
terraform init 

8. Provision the GKE cluster

terraform plan
terraform apply 

9. Interacting with the cluster using kubectl

gcloud container clusters get-credentials $(terraform output -raw kubernetes_cluster_name) --region $(terraform output -raw region)
kubectl config get-contexts
kubectl get nodes
kubectl get all --all-namespaces

11 . Destroy the cluster

terraform destroy

I am a DevOps Engineer. Follow me to keep abreast with the latest technology news, industry insights, and DevOps trends.