Creating & Deploying own image to Google Kubernetes Engine(GKE) using Terraform(Part 2)

Abhishek Sharma
6 min readJun 1, 2021

Hello Folks,

Let’s continue our learning………

If you don’t read part 1 then please read that first and then come back again, there are some parts that I’m using in this.

For Part 1, Click Here……

Overview

In this part, we’re going to learn some DevOps tools & try to integrate all those tools to understand the process of development and deployment. So let’s see what we already know & what we’re going to learn in this

  • How to create a simple Docker container image using Dockerfile(Completed in Part 1)
  • General use of Docker Container registry DockerHub(Completed in Part 1)
  • Deployment of own docker image to Kubernetes using YAML file(Completed in Part 1)
  • Google Kubernetes Engine Cluster Creation using Terraform Script
  • How to deploy our own image deployment file with the help of Kubernetes configuration file(YAML)
  • Some optional deployment using terraform like Google Virtual Private Cloud(VPC) Network & more.

Let’s start our learning

Step 4: Google Kubernetes Engine Cluster Creation using Terraform Script

Until we know, how to create & run docker images, use of container registry Docker hub & also know, how to deploy that image to google Kubernetes Engine(GKE) using a configuration file(YAML).

In this step, Firstly we’re going to create a cluster using terraform.

In general, Terraform is an Infrastructure as a code tool, what it means “we can create, manage & destroy infrastructure over all the popular cloud providers like Google Cloud Platform, AWS, Microsoft Azure, OCI and many more other providers in a very efficient way and that way is called terraform script file

Note: If you don’t know about terraform, Don’t worry, Just follow this link

No worry If you don’t get how this is working, what is terraform script file & other doubts, as we go one by one, surely you will get it. For now, you just have an idea that terraform does there all tasks using a script file & that script file has an extension of “<file-name>.tf”

Note: You can create a single terraform script file for all resources you want to deploy, but I’m using separate files for better understanding.

  • Firstly create a terraform script file which defines your cloud provider like google cloud, AWS & other cloud providers, which some optional version specifications.
nano provider.tf

Here I defined google as my provider & some specific versions of packages from official Hashicorp Terraform Documentation

  • Next, we create a variable file for our zone & project ID definition.
nano terraform.tfvars
  • Now, we’re going to create a file for your cluster creation.
nano cluster.tf

You can see that this file has the all specification for our cluster like machine type, zone, region, access scope, node pool, etc. I’m creating this cluster for testing purpose, so I defined only the minimum required resources like it is a zonal cluster with minimum resources but in industrial use cases you’ve to define lots of other parameters as well like private network for your cluster, endpoint definition, authentication parameters so for that scenario prefer the official documentation

  • Now create one more file, for displaying the information about the cluster with some specifications.
nano output.tf

Now, we’re ready with our cluster creation scripts, so it's time to see Terraform in action.

Before moving forward, be sure you have to terraform installed in your local & your Google cloud SDK is properly configured. If you don’t then follow this link

Let us initialize the terraform in your working directory.

terraform init

Okay, our terraform is initialized to see what resources (with their details) are going to create using the above-created scripts run the below command.

terraform plan

After reviewing all resources & their details, If you don’t get any error let’s deploy our script defined infrastructure by running this command

terraform apply

If you prompted then type yes to move forward.

It takes some time depending upon which resource you’re going to create, so be patient. After creation finished you can go to the console & validate your step. You can see your Kubernetes cluster has been created with the same details you see after creation in the CLI. Verify that you don’t have any workload & services available in the console, we’ll create those as well in the forward step.

Step 5: How to deploy our own image deployment file with the help of Kubernetes configuration file(YAML)

In Part 1 we have deployed our own image using Kubernetes configuration file “config.yaml”. So here, with the help of that YAML file as the base reference script, we’re going to create our terraform deployment file called kubernetes.tf

Here also, We are separating deployment files for ease of change & better understanding.

mkdir deploymentcd deployment

In this folder, we’re going to create our terraform cluster deployment file

nano cluster-deployment.tf

So this is a cluster deployment file, As you can see, I have defined a provider here(Remember I have written that we can define all in one terraform script file itself) because we’re in the new directory called deployment.

Read Carefully:

  • Here terraform remote state section fetches all details about your existing cluster, we have created previously, so you have to give the path to the previously terraform initialized directory, where you found one folder created by terraform called terraform.tfstate from where it takes all details about your cluster. To edit the path accordingly.
  • You can see that deployment & services section is looking the same as our Part 1 Kubernetes configuration file (YAML). So comparing both changes the file & it is very easy to create this script if you already have a Kubernetes configuration file that’s why I took this in Part 1.

Now, I’m going to follow the same as above to apply this terraform script.

terraform initterraform planterraform apply

You know the meaning of all these 3 commands, so I’m not explaining here. Now go to console Navigation menu -> Kubernetes Engine -> workload. you can see your deployment called my deployment is deployed & running, Also in the left menu click on services & see your my-lb load balancer service is running. So just click on the IP you can see the desired output.

Congratulations!, You’ve created & deployed your own application from scratch to a fully deployed application on the cloud.

Note: This is not ending here, there are lots of parameters, I didn’t mention but when are very important as per industry use cases so go through the official documentation of both google cloud & hashicorp terraform to deep dive.

After experimenting with this, never forget to delete the resources, you’ve created using terraform. For this, we have a command

terraform destroy

This will destroy all the resources you created using terraform.

Optional: Some optional deployment using terraform like Google Virtual Private Cloud(VPC) Network & more.

Some extra resources from my side like in this I have created zonal cluster & used default network but If you want to create a regional cluster with private network follow this link to my GitHub Repo

If you edited any terraform script & want to validate that script, just run this command

terraform validate

Thank you

Hope this will help you

If you have any queries regarding this, or you want to connect with me then ping me on LinkedIn

--

--