How to Launch a Google Kubernetes Engine (GKE) Cluster Using Terraform


How to Launch a Google Kubernetes Engine (GKE) Cluster Using Terraform

Google Kubernetes Engine (GKE) is a managed Kubernetes service provided by Google Cloud Platform (GCP). It allows you to run containerized applications in a scalable and automated environment. Terraform, a popular Infrastructure as Code (IaC) tool, makes it easy to deploy and manage GKE clusters using simple configuration files. In this article, we’ll walk you through the steps to launch a GKE cluster using Terraform.

Prerequisites

Before starting, ensure you have the following:

  1. Google Cloud Account: You need an active Google Cloud account with a project set up. If you don’t have one, you can sign up at Google Cloud.
  2. Terraform Installed: Ensure Terraform is installed on your local machine. Download it from the Terraform website.
  3. GCP Service Account Key: You’ll need a service account key with appropriate permissions (e.g., Kubernetes Engine Admin, Compute Admin). Download the JSON key file for this service account.

Step 1: Set Up Your Terraform Directory

Create a new directory to store your Terraform configuration files.

mkdir gcp-terraform-gke
cd gcp-terraform-gke

Step 2: Create the Terraform Configuration File

In your directory, create a file named main.tf where you will define the configuration for your GKE cluster.

touch main.tf

Open main.tf in your preferred text editor and add the following configuration:

# main.tf

provider "google" {
  project     = "<YOUR_GCP_PROJECT_ID>"
  region      = "us-central1"
  credentials = file("<PATH_TO_YOUR_SERVICE_ACCOUNT_KEY>.json")
}

resource "google_container_cluster" "primary" {
  name     = "terraform-gke-cluster"
  location = "us-central1"

  initial_node_count = 3

  node_config {
    machine_type = "e2-medium"

    oauth_scopes = [
      "https://www.googleapis.com/auth/cloud-platform",
    ]
  }
}

resource "google_container_node_pool" "primary_nodes" {
  name       = "primary-node-pool"
  location   = google_container_cluster.primary.location
  cluster    = google_container_cluster.primary.name

  node_config {
    preemptible  = false
    machine_type = "e2-medium"

    oauth_scopes = [
      "https://www.googleapis.com/auth/cloud-platform",
    ]
  }

  initial_node_count = 3
}

Explanation of the Configuration

  • Provider Block: Specifies the GCP provider details, including the project ID, region, and credentials.
  • google_container_cluster Resource: Defines the GKE cluster, specifying the name, location, and initial node count. The node_config block sets the machine type and OAuth scopes.
  • google_container_node_pool Resource: Defines a node pool within the GKE cluster, allowing for more granular control over the nodes.

Step 3: Initialize Terraform

Initialize Terraform in your directory to download the necessary provider plugins.

terraform init

Step 4: Plan Your Infrastructure

Run the terraform plan command to preview the changes Terraform will make. This step helps you validate your configuration before applying it.

terraform plan

If everything is configured correctly, Terraform will generate a plan to create the GKE cluster and node pool.

Step 5: Apply the Configuration

Once you’re satisfied with the plan, apply the configuration to create the GKE cluster on GCP.

terraform apply

Terraform will prompt you to confirm the action. Type yes to proceed.

Terraform will now create the GKE cluster and associated resources on GCP. This process may take a few minutes.

Step 6: Verify the GKE Cluster

After Terraform has finished applying the configuration, you can verify the GKE cluster by logging into the GCP Console:

  1. Navigate to the Kubernetes Engine section.
  2. You should see the terraform-gke-cluster running in the list of clusters.

Additionally, you can use the gcloud command-line tool to check the status of your cluster:

gcloud container clusters list --project <YOUR_GCP_PROJECT_ID>

Step 7: Configure kubectl

To interact with your GKE cluster, you’ll need to configure kubectl, the Kubernetes command-line tool.

gcloud container clusters get-credentials terraform-gke-cluster --region us-central1 --project <YOUR_GCP_PROJECT_ID>

Now you can run Kubernetes commands to manage your applications and resources on the GKE cluster.

Step 8: Clean Up Resources

If you no longer need the GKE cluster, you can delete all resources managed by Terraform using the following command:

terraform destroy

This command will remove the GKE cluster and any associated resources defined in your Terraform configuration.

Conclusion

Launching a GKE cluster using Terraform simplifies the process of managing Kubernetes clusters on Google Cloud. By defining your infrastructure as code, you can easily version control your environment, automate deployments, and ensure consistency across different stages of your project. Whether you’re setting up a development, testing, or production environment, Terraform provides a powerful and flexible way to manage your GKE clusters.