Terraform for GCP How to create Kubernetes Cluster

Paul Ravvich
Terraform for the Google Cloud Platform
3 min readMay 9, 2024

--

Terraform for GCP How to create Kubernetes Cluster

Hi, this is Paul, and welcome to the #35 part of my Terraform guide. Today we will discuss, how to create a Kubernetes Cluster using the Terraform script.

Introduction

This article will introduce you to the process of deploying a Kubernetes cluster in Google Cloud Platform (GCP) using Terraform. By using Terraform, we can automate the creation, modification, and management of cloud infrastructure, providing a more reliable and repeatable management of resources.

Terraform Configuration

Below is the complete example of the Terraform code for creating a Kubernetes cluster in GCP:

module "gke" {
source = "terraform-google-modules/kubernetes-engine/google"
name = "gke-test-1"
region = "us-central1"
zones = ["us-central1-a", "us-central1-b", "us-central1-f"]
network = "default"
subnetwork = "default"
ip_range_pods = ""
ip_range_services = ""
http_load_balancing = false
horizontal_pod_autoscaling = true
kubernetes_dashboard = true
network_policy = false

node_pools = [
{
name = "default-node-pool"
machine_type = "n1-standard-2"
min_count = 1
max_count = 2
disk_size_gb = 10
disk_type = "pd-standard"
image_type = "COS"
auto_repair = true
auto_upgrade = true
preemptible = false
initial_node_count = 1
},
]

node_pools_oauth_scopes = {
all = []

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

node_pools_labels = {
all = {}

default-node-pool = {
default-node-pool = "true"
}
}

node_pools_metadata = {
all = {}

default-node-pool = {
node-pool-metadata-custom-value = "my-node-pool"
}
}

node_pools_tags = {
all = []

default-node-pool = [
"default-node-pool",
]
}
}

Configuration Parameter Explanation

Core Module Parameters

  • source: Specifies the use of a Terraform module for Google Kubernetes Engine.
  • name: The name of the Kubernetes cluster being created.
  • region and zones: The region and zones in GCP where the cluster will be located.
  • network and subnetwork: Network settings for the cluster.
  • ip_range_pods and ip_range_services: IP ranges for Kubernetes pods and services.
  • http_load_balancing: Configuration for the HTTP load balancer.
  • horizontal_pod_autoscaling: Auto-scaling of pods based on load.
  • kubernetes_dashboard: Dashboard for managing the cluster.
  • network_policy: Network policies for controlling traffic between pods.

Node Configuration (node_pools)

  • machine_type: Type of virtual machine.
  • min_count and max_count: Minimum and maximum number of nodes.
  • disk_size_gb and disk_type: Size and type of the nodes’ disks.
  • image_type: Operating system of the nodes.
  • auto_repair and auto_upgrade: Automatic repair and upgrade of nodes.
  • preemptible: Use of preemptible VMs to reduce costs.
  • initial_node_count: Initial number of nodes in the pool.

Additional Parameters

  • node_pools_oauth_scopes: OAuth scopes for the nodes.
  • node_pools_labels: Labels for the nodes.
  • node_pools_metadata: Custom metadata for the nodes.
  • node_pools_tags: Tags for network resources associated with the nodes.

Conclusion

Using this Terraform example, you can easily deploy and configure a Kubernetes cluster in Google Cloud, managing the entire infrastructure as code. This not only simplifies the deployment process but also enhances its reproducibility and scalability.

Thank you for reading until the end. Before you go:

Paul Ravvich

--

--

Paul Ravvich
Terraform for the Google Cloud Platform

Software Engineer with over 10 years of XP. Join me for tips on Programming, System Design, and productivity in tech! New articles every Tuesday and Thursday!