Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Example GKE cluster using Terraform

This repository showcases using Terraform to provision a new network and a GKE cluster with nodes within.

By default, this will create a highly available cluster using a NAT gateway for outgoing traffic from private nodes.

See a high level overview of the GKE architecture.

Install and configure

Ensure that kubectl, gcloud, and terraform are installed first.

Ensure the Google Cloud CLI tools are initalised:

gcloud init

Once initialised, ensure your account is added to the Application Default Credentials (ADC) so Terraform can access them:

gcloud auth application-default login

Additionally, ensure that APIs have been enabled:

gcloud services enable storage-api.googleapis.com
gcloud services enable cloudresourcemanager.googleapis.com
gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com
gcloud services enable iam.googleapis.com

Setup variables

In terraform.tfvars set the details according to your Google Cloud account.

project_id, location, and name are required to be set.

location can be set to a region or zone. See regional or zone specific cluster docs for more information.

An example terraform.tfvars file of using a single zone cluster with preemptible nodes:

project_id      = "my-project-123"
location        = "australia-southeast1-a"
name            = "mycluster"
node_pools = [
  {
    preemptible        = true
    min_node_count     = 1
    max_node_count     = 8
    machine_type       = "n2-standard-2"
    disk_type          = "pd-standard"
    disk_size_gb       = 40
  }
]

Check out a list of Google Cloud regions and zones for reference.

Provisioning

terraform init
terraform apply

Configure kubectl

Retrieve the cluster name and location using terraform show, then initialise kubectl configuration:

gcloud container clusters get-credentials mycluster --region australia-southeast1-a

Test it works

kubectl get nodes -o wide

Tearing down

terraform destroy

What now?

Check out google_container_cluster Terraform docs for more details on what GKE parameters can be changed using Terraform.

Some other things: