Install Google Kubernetes Engine (GKE) Cluster using Terraform

0 / 400
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 what is Terraform, refer this blog -> INFRASTRUCTURE AS CODE – ORCHESTRATION, PROVISIONING & CONFIGURATION MANAGEMENT (ANSIBLE & 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.   Pre-requisites:
  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 https://console.cloud.google.com
  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/tf/gke-tf-cluster/secret   [root@devops secret]# ls   hostbread-5cef426d5a12.json   [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 main.tf file   In /root/tf/gke-tf-cluster directory lets create terraform config file main.tf   [root@devops secret]# cd /root/tf/gke-tf-cluster   [root@devops secret]# pwd   /root/tf/gke-tf-cluster/secret/   [root@devops secret]# cd ..   [root@devops gke-tf-cluster]# ls   main.tf  secret   [root@devops gke-tf-cluster]# cat main.tf   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 main.tf 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   10.63.240.1   <none>        443/TCP   9m4s   prayag_rhce@cloudshell:~ (hostbread)$ kubectl cluster-info   Kubernetes master is running at https://35.222.237.141   GLBCDefaultBackend is running at https://35.222.237.141/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy   Heapster is running at https://35.222.237.141/api/v1/namespaces/kube-system/services/heapster/proxy   KubeDNS is running at https://35.222.237.141/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy   Metrics-server is running at https://35.222.237.141/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy   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   admissionregistration.k8s.io/v1beta1   apiextensions.k8s.io/v1beta1   apiregistration.k8s.io/v1   apiregistration.k8s.io/v1beta1   apps/v1   apps/v1beta1   apps/v1beta2   authentication.k8s.io/v1   authentication.k8s.io/v1beta1   authorization.k8s.io/v1   authorization.k8s.io/v1beta1   autoscaling/v1   autoscaling/v2beta1   batch/v1   batch/v1beta1   certificates.k8s.io/v1beta1   cloud.google.com/v1beta1   coordination.k8s.io/v1   coordination.k8s.io/v1beta1   extensions/v1beta1   metrics.k8s.io/v1beta1   networking.gke.io/v1beta1   networking.k8s.io/v1   networking.k8s.io/v1beta1   node.k8s.io/v1beta1   nodemanagement.gke.io/v1alpha1   policy/v1beta1   rbac.authorization.k8s.io/v1   rbac.authorization.k8s.io/v1beta1   scalingpolicy.kope.io/v1alpha1   scheduling.k8s.io/v1   scheduling.k8s.io/v1beta1   storage.k8s.io/v1   storage.k8s.io/v1beta1   v1   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.          

Comments

comments


***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