Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Kubernetes Cluster on Google Cloud

The Google Cloud Kubernetes Cluster template scaffolds a Pulumi project that provisions a managed Google Kubernetes Engine (GKE) cluster and a node pool inside a new VPC network with a subnet. Worker nodes are deployed with private IP addresses for improved security.

Architecture diagram of the Google Cloud Kubernetes Cluster template Google Cloud Compute Network (VPC) GKE Cluster VPC Subnet GKE API Endpoint Node Pool

Before you begin#

Make sure you’ve installed Pulumi and configured your Google Cloud credentials.

Create a new project#

Create a new project using the template in the language of your choice:

Terminal window
mkdir my-k8s-cluster && cd my-k8s-cluster
pulumi new kubernetes-gcp-typescript

pulumi new walks you through creating and configuring a new project and stack. A stack is an instance of a project and you can have many of them — dev, staging, prod — each with its own configuration settings. The template also includes some additional settings:

KeyDescriptionDefault
gcp:projectThe Google Cloud project to deploy into—
gcp:regionThe Google Cloud region to deploy intous-central1
nodesPerZoneThe desired number of nodes PER ZONE in the nodepool1
You can change these settings later with pulumi config set:
Terminal window
pulumi config set gcp:project <value>
pulumi config set gcp:region <value>
pulumi config set nodesPerZone <value>

For gcp:project, enter the ID of an existing Google Cloud project. gcloud config get-value project prints the one gcloud is using.

When setup completes, you’ll see a few key files:

  • Pulumi.yaml names the project and the language it’s written in.
  • Pulumi.dev.yaml holds the dev stack’s settings, including the values you just entered.
  • index.ts is the program: the code that declares your cloud resources.

Deploy the project#

Before the first deploy, turn on the Kubernetes Engine API in your project:

Terminal window
gcloud services enable container.googleapis.com --project $(pulumi config get gcp:project)

Then deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy. A GKE cluster takes 10 to 20 minutes to create, so expect pulumi up to run that long.

Terminal window
pulumi up

Pulumi adds a random suffix to most resource names, so gke-cluster becomes something like gke-cluster-4f8a2c1. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist:

networkName

The name of the VPC network containing the Kubernetes cluster resources.

networkId

The unique ID of the VPC network containing the Kubernetes cluster resources.

clusterName

The name of the GKE cluster.

clusterId

The unique ID of the GKE cluster.

kubeconfig

The cluster’s kubeconfig file, which you can use with kubectl to access and communicate with your cluster.

Read one any time with pulumi stack output <name>, or pass it to another command. The kubeconfig logs in through gcloud’s gke-gcloud-auth-plugin, so install that first if you don’t have it, then save the kubeconfig to a file and list the cluster’s nodes:

Terminal window
gcloud components install gke-gcloud-auth-plugin
pulumi stack output kubeconfig --show-secrets > kubeconfig
KUBECONFIG=./kubeconfig kubectl get nodes

GKE spreads the node pool across three of the region’s zones, so you should see three nodes with the status Ready.

Customize the project#

Projects created with the Kubernetes Cluster template expose the following configuration settings:

gcp:project

The Google Cloud project ID to deploy into.

gcp:region

The Google Cloud region to deploy into. Defaults to us-central1.

nodesPerZone

The desired number of nodes per zone in the node pool. Defaults to 1.

All of these settings are optional and may be adjusted either by editing the stack configuration file directly (by default, Pulumi.dev.yaml) or by changing their values with pulumi config set. For example, run two nodes in each zone:

Terminal window
pulumi config set nodesPerZone 2
pulumi up

When the update finishes, list the nodes again; there are six:

Terminal window
KUBECONFIG=./kubeconfig kubectl get nodes

To see what else you can add, browse the Google Cloud provider in the Pulumi Registry.

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure as code platform for any cloud.