Building GKE with Terraform

Provision Google Kubernetes Engine with Terraform

About Terraform

Previous Article

Requirements

  1. Google Cloud SDK: these tools are needed to manage Google Cloud
  2. Terraform: provision a new cluster with human readable language HCL.
  3. Kubectl (pronounced koob-cuttle) is the Kubernetes client cli tool to interact with your newly created cluster.

Provisioning GKE with Terraform

Create Project File Structure

mkdir $HOME/gke_project
cd $HOME/gke_project
touch main.tf provider.tf
provider.tf

Initializing Terraform

terraform init

Configuring Variables

export TF_VAR_project=$(gcloud config get-value project)
export TF_VAR_region="us-central1"
export TF_VAR_cluster_name="my-terraform-gke-cluster"

Provisioning a Cluster

terraform apply

Test the Cluster

watch gcloud container clusters list \
--filter name=my-terraform-gke-cluster
gcloud container clusters get-credentials my-terraform-gke-cluster \
--region us-central1
kubectl config get-contexts
kubectl get all --all-namespaces

Cleaning Up

terraform destroy

Deploying an Application

Deploy the Deployment

kubectl
kubectl apply --filename hello-k8s-deploy.yaml
kubectl get deployment

Deploy the Service

kubectl apply --filename hello-k8s-svc.yaml
kubectl get service

Testing the Deployment

kubectl port-forward service/hello-kubernetes 8080:8080
http://localhost:8080

Resources

Blog Source Code

Google Terraform Modules

GruntWork’s GKE Module

JetStack’s GKE Module

Learn Terraform: Provision a GKE Cluster

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joaquín Menchaca (智裕)

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, o11y, k8s, progressive deployment (ci/cd), cloud native infra, infra as code