Archive: a Terraform outline for a GCP Kubernetes cluster
This is a 2022 outline for creating a Kubernetes cluster on Google Cloud Platform with Terraform. It records the shape of an early configuration, not a current production template. Provider schemas, GKE defaults, identity controls, and Kubernetes release support change. Use the current provider and GKE documentation when building new infrastructure.
The original outline followed these steps:
- Install Terraform on your machine. (Read More: How to Download & Install Terraform on Windows, Linux, Mac)
- Set up a GCP account and create a project.
- Create a service account with the necessary permissions to create and manage resources in your GCP project.
- Create credentials with only the permissions the deployment requires and protect them appropriately.
- Define the provider and credentials for your GCP project in a Terraform configuration file.
provider "google" {
credentials = file("path/to/service_account.json")
project = "my-gcp-project"
region = "us-central1"
}
The following google_container_cluster example is preserved as historical code. It contains static credentials and older defaults, so it should not be copied into a current deployment.
resource "google_container_cluster" "my-cluster" {
name = "my-cluster"
location = "us-central1-a"
initial_node_count = 3
master_auth {
username = "admin"
password = "password"
}
node_config {
machine_type = "n1-standard-1"
oauth_scopes = [
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
]
}
}
After reviewing a current configuration and plan, run terraform init, inspect terraform plan, and apply through the change controls appropriate to the environment.
Once the cluster is available, kubectl can manage workloads through the cluster's configured access controls. The important decision is not the command alone. It is how identity, state, network boundaries, upgrades, and recovery are defined for the environment.