How to create GKE cluster on GCP using Terraform

gcp gke terraform
Reading Time: 4 minutes

GCP is a cloud platform provided by Google. In this blog, we will see how we can provision GKE Google Kubernetes Engine on GCP by using Terraform. Kubernetes is a container orchestration system for managing container-based applications and automated software deployments & management.

We will deploy a GKE cluster on GCP by using some Terraform script and modules.

Prerequisites – GKE on GCP with Terraform

  1. gcloud util – download from here
  2. kubectl util – download from here
  3. GCP account
  4. GCP project with Enabled billing account
  5. Service account & CRM API
  6. Terraform – download from here

Initial Setup – GKE on GCP with Terraform

1. Initialize gcloud CLI

gcloud init

2. Create a service account & assign the policy

gcloud iam service-accounts create <SERVICE_ACCOUNT_NAME>
  • <SERVICE_ACCOUNT_NAME> is name for your service account.
gcloud projects add-iam-policy-binding <PROJECT_ID> --member="serviceAccount:NAME@PROJECT_ID.iam.gserviceaccount.com" --role="roles/owner"
  • <PROJECT_ID> is name of your project ID
  • NAME@PROJECT_ID.iam.gserviceaccount.com is email ID of service account

3. Create a credential key for the service account

gcloud iam service-accounts keys create cred.json --iam-account=NAME@PROJECT_ID.iam.gserviceaccount.com

A credential key named cred.json is downloaded at your present working directory.

4. Set environment variable for credential key

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/cred.json"

5. Enable GKE API & CRM API

gcloud services enable container.googleapis.com
gcloud services enable cloudresourcemanager.googleapis.com

Terraform scripts

1. Create main.tf file and copy the below code in this file.

main.tf

provider "google" {
  credentials = file("/path/to/cred.json")
}

module "gke_auth" {
  source       = "terraform-google-modules/kubernetes-engine/google//modules/auth"
  depends_on   = [module.gke]
  project_id   = var.project_id
  location     = module.gke.location
  cluster_name = module.gke.name
}

resource "local_file" "kubeconfig" {
  content  = module.gke_auth.kubeconfig_raw
  filename = "kubeconfig-${var.env_name}"
}

module "gcp-network" {
  source       = "terraform-google-modules/network/google"
  project_id   = var.project_id
  network_name = "${var.network}-${var.env_name}"
  subnets = [
    {
      subnet_name   = "${var.subnetwork}-${var.env_name}"
      subnet_ip     = "10.10.0.0/16"
      subnet_region = var.region
    },
  ]
  secondary_ranges = {
    "${var.subnetwork}-${var.env_name}" = [
      {
        range_name    = var.ip_range_pods_name
        ip_cidr_range = "10.20.0.0/16"
      },
      {
        range_name    = var.ip_range_services_name
        ip_cidr_range = "10.30.0.0/16"
      },
    ]
  }
}

module "gke" {
  source            = "terraform-google-modules/kubernetes-engine/google//modules/private-cluster"
  project_id        = var.project_id
  name              = "${var.cluster_name}-${var.env_name}"
  regional          = false
  region            = var.region
  zones             = ["us-central1-c"]
  network           = module.gcp-network.network_name
  subnetwork        = module.gcp-network.subnets_names[0]
  ip_range_pods     = var.ip_range_pods_name
  ip_range_services = var.ip_range_services_name
  node_pools = [
    {
      name           = "node-pool"
      machine_type   = "n2-standard-2"
      node_locations = "us-central1-c"
      min_count      = var.minnode
      max_count      = var.maxnode
      disk_size_gb   = var.disksize
      preemptible    = false
      auto_repair    = false
      auto_upgrade   = true
    },
  ]
}
output "cluster_name" {
  description = "Cluster name"
  value       = module.gke.name
}

2. Create a variables.tf for variables definition used in this script

variables.tf

variable "project_id" {
  description = "The project ID of your project"
}
variable "cluster_name" {
  description = "The name for the GKE cluster"
  default     = "gke-terraform"
}
variable "env_name" {
  description = "The environment for the GKE cluster"
  default     = "learn"
}
variable "region" {
  description = "The region to host the cluster in"
  default     = "us-central1"
}
variable "zones" {
  description = "Cluster zone"
  default     = "us-central1-c"
}
variable "network" {
  description = "The VPC network created to host the cluster in"
  default     = "gke-network"
}
variable "subnetwork" {
  description = "The subnetwork created to host the cluster in"
  default     = "gke-subnet"
}
variable "ip_range_pods_name" {
  description = "The secondary ip range to use for pods"
  default     = "ip-range-pods"
}
variable "ip_range_services_name" {
  description = "The secondary ip range to use for services"
  default     = "ip-range-services"
}

variable "service-account-id" {
  description = "The ID of service account of GCP"
  default     = "serviceaccount-id"
}
variable "cpus" {
  description = "Number of cpus"
  default     = "2"
}

variable "minnode" {
  description = "Minimum number of node pool"
  default     = 1
}
variable "maxnode" {
  description = "Maximum number of node pool"
  default     = 2
}

variable "disksize" {
  description = "Disk Size in GB"
  default     = 10
}

Build Cluster on GCP

First, Export env var for credential key in current working shell

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/cred.json"

1. Initialize Terraform & download required plugins

terraform init
terraform init kubernetes GCP

2. Check the Terraform plan

terraform plan
Kubernetes Terraform plan

3. Apply the terraform plan to create the cluster

terraform apply

4. Get the credentials for GKE clusters

gcloud container clusters get-credentials <CLUSTER_NAME> --zone us-central1-c --project <PROJECT_ID>
  • <CLUSTER_NAME> is name of your GKE cluster just created.
  • <PROJECT_ID> is project id in which custer is created.

OR

Go to GCP console >> GKE clusters >> click on menu dots & click on connect >> copy the command

Kubernetes cluster on GCP

5. Check cluster-info & health with kubectl utility

kubectl get cs

and

kubectl cluster-info

Conclusion

This is how we can create out GKE cluster on GCP using Terraform. Terraform is IAC infrastructure as code can be used to provision any kind of infrastructure on the cloud.

References

Terraform gke module

knoldus

Written by 

Rahul Soni is a Software Intern at Knoldus Software. He is always charged up for new things & learnings. He is dedicated to his work and believes in quality output. He loves to take deep dives into cloud technologies & different tools.