/ TERRAFORM, GCP

Create a GKE Cluster on the Google Cloud Platform using Terraform

Create a GKE Cluster on the Google Cloud Platform using Terraform

Google Kubernetes Engine (GKE) is a managed Kubernetes service offered by Google Cloud Platform (GCP), simplifying containerized applications’ management and deployment. This tutorial will guide you through creating a GKE cluster using Terraform, a widespread Infrastructure as Code (IaC) tool.

Prerequisites

  1. A Google Cloud Platform account
  2. The Google Cloud SDK installed and configured
  3. Terraform installed

Table of Contents:


Set up the Terraform Configuration

First, create a new directory for your Terraform configuration:

$ mkdir gke-terraform
$ cd gke-terraform

Create a main.tf file in the gke-terraform directory and add the following code:

provider "google" {
  credentials = file("<PATH_TO_YOUR_SERVICE_ACCOUNT_KEY_JSON>")
  project     = "<YOUR_PROJECT_ID>"
  region      = "<YOUR_REGION>"
}

resource "google_container_cluster" "gke_cluster" {
  name               = "gke-cluster"
  location           = "<YOUR_REGION>"
  initial_node_count = 1

  node_config {
    machine_type = "n1-standard-1"
  }
}

output "cluster_endpoint" {
  value = google_container_cluster.gke_cluster.endpoint
}

output "cluster_ca_certificate" {
  value     = google_container_cluster.gke_cluster.master_auth.0.cluster_ca_certificate
  sensitive = true
}

Replace <PATH_TO_YOUR_SERVICE_ACCOUNT_KEY_JSON> with the path to your GCP service account key JSON file, <YOUR_PROJECT_ID> with your GCP project ID, and <YOUR_REGION> with your desired GCP region.

Initialize Terraform

In the gke-terraform directory, run the following command to initialize Terraform:

$ terraform init

This command downloads the required provider plugins and sets up the backend for storing your Terraform state.

Create the GKE Cluster

Run the following command to create the GKE cluster:

$ terraform apply

Type “yes” when prompted to confirm that you want to create the resources. The resource creation process may take several minutes to complete. Once done, Terraform will output the cluster endpoint and cluster CA certificate.

Connect to the GKE Cluster

Save the kubeconfig output from the previous step to a file, and set the KUBECONFIG environment variable to use it:

$ gcloud container clusters get-credentials gke-cluster --region <YOUR_REGION>

Now, you can use kubectl to interact with your GKE cluster:

$ kubectl get nodes

This command will show the nodes in your GKE cluster.

Clean Up Resources

When you no longer need the GKE cluster, you can destroy the resources using Terraform:

$ terraform destroy

Type “yes” when prompted to confirm that you want to destroy the resources.

Conclusion

This tutorial taught you how to create a Google Kubernetes Engine (GKE) cluster using Terraform on the Google Cloud Platform. By leveraging Infrastructure as Code, you can maintain consistent environments, collaborate with your team more effectively, and automate the provisioning and management of your Kubernetes clusters.

With your GKE cluster up and running, you can now deploy containerized applications, scale your infrastructure, and take advantage of the many features offered by GCP and Kubernetes. To dive deeper into GKE and Terraform, explore the official GKE documentation and the Terraform Google provider documentation.

Dive Deeper: Recommended Reads

Expand your knowledge of Infrastructure as Code and Terraform with our insightful collection of articles! Dive into a range of topics that will help you master the art of managing infrastructure:

Embrace the power of Terraform and Infrastructure as Code with this comprehensive collection of articles, and enhance your skills in deploying, managing, and maintaining your infrastructure.

faizan

Faizan Bashir

Principal Engineer | Architecting and building distributed applications in the Cloud | Adventurer

Read More