Deploying-own-image-to-GKE-using-terraform

This repo will help you to deploy your own container image to google kubernetes engine(GKE) using terraform. I used my own docker hub public image for this repo.

This repo contains several terraform configuration files.

This terraform file is used to configure google kubernetes engine cluster, node pools in that cluster, their machine type, scopes etc. you can change it according to your requirement.

This is the file, where we configure our docker container image, firstly it takes information about your existing deployed cluster & create a deployment to deploy your container image, for kubernetes service I used NodePort to expose image. you can change it according to your kubernetes configuration file(yaml).

This terraform file is used to create a new virtual private cloud network so you don't worry about your existing personal network, also you can change it to use you existing network.

Basically, this is a file which defines provider and terraform version you used to deploy your infrastructure. I suggest go with latest one but here in this repo I used older one which is terraform 0.12. If you're using cloud shell than this version is already installed in your machine.

This file is used to show your deployment details after deployment.

This file is used to setup your project ID & region where you want to deploy.

This is kubernetes dashboard configuration file for api calls & management of cluster.

Requirements

  • a GCP account
  • a configured gcloud SDK
  • kubectl
  • terraform

Set up

clone the repo into your machine

Step 1. Creating a cluster & other resources

  1. Change the directory where all files are located
  • $cd Deploying-own-image-to-GKE-using-terraform
  1. see the all configuration files & if required change it accordingly. Note: You have to put your project ID to terraform.tfvars file.

  2. Now, initiaze the terraform in this folder & check all things are working well. Note : I'm using terraform version 0.12 but if you're using latest version 0.15 then change required version to your version.

  • $terraform init

  • $terraform plan

  1. If you don't get any error then let's deploy our configuration
  • $terraform apply
  1. To use kubectl command line tools for this cluster in cloud shell,
  • $gcloud container clusters get-credentials $(terraform output -raw kubernetes_cluster_name) --region $(terraform output -raw region)

Step 2. Deploying own image to the cluster

  1. get the deployment file
  • $ cd deployment
  1. Again apply terraform
  • $terraform init

  • $terraform plan

Note: If you got error regarding remote cluster so you can put your cluster execution file path here.

  • $terraform apply

Optional Step

If you're using cloud shell then you can preview you application through cloud shell web preview

  • $gcloud container clusters get-credentials replace_cluster_name --region replace_your_deployed_zone --project replace_project_id
    && kubectl port-forward $(kubectl get pod --selector="App=myapp" --output jsonpath='{.items[0].metadata.name}') 8080:80

    Open your web preview.

    After experimenting with this demo, let's clean up & destroy what we deployed.

  • $terraform destroy

This repo is a created with reference to the Provision a GKE Cluster learn guide, containing Terraform configuration files to provision an GKE cluster on GCP.