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.
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:
mkdir my-k8s-cluster && cd my-k8s-clusterpulumi new kubernetes-gcp-typescriptmkdir my-k8s-cluster && cd my-k8s-clusterpulumi new kubernetes-gcp-pythonmkdir my-k8s-cluster && cd my-k8s-clusterpulumi new kubernetes-gcp-gomkdir my-k8s-cluster && cd my-k8s-clusterpulumi new kubernetes-gcp-csharpmkdir my-k8s-cluster && cd my-k8s-clusterpulumi new kubernetes-gcp-yamlmkdir my-k8s-cluster && cd my-k8s-clusterpulumi new kubernetes-gcp-hclpulumi 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:
| Key | Description | Default |
|---|---|---|
| gcp:project | The Google Cloud project to deploy into | — |
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| nodesPerZone | The desired number of nodes PER ZONE in the nodepool | 1 |
pulumi config set:
pulumi config set gcp:project <value>pulumi config set gcp:region <value>pulumi config set nodesPerZone <value>| Key | Description | Default |
|---|---|---|
| gcp:project | The Google Cloud project to deploy into | — |
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| nodesPerZone | The desired number of nodes PER ZONE in the nodepool | 1 |
pulumi config set:
pulumi config set gcp:project <value>pulumi config set gcp:region <value>pulumi config set nodesPerZone <value>| Key | Description | Default |
|---|---|---|
| gcp:project | The Google Cloud project to deploy into | — |
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| nodesPerZone | The desired number of nodes PER ZONE in the nodepool | 1 |
pulumi config set:
pulumi config set gcp:project <value>pulumi config set gcp:region <value>pulumi config set nodesPerZone <value>| Key | Description | Default |
|---|---|---|
| gcp:project | The Google Cloud project to deploy into | — |
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| nodesPerZone | The desired number of nodes PER ZONE in the nodepool | 1 |
pulumi config set:
pulumi config set gcp:project <value>pulumi config set gcp:region <value>pulumi config set nodesPerZone <value>| Key | Description | Default |
|---|---|---|
| gcp:project | The Google Cloud project to deploy into | — |
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| nodesPerZone | The desired number of nodes PER ZONE in the nodepool | 1 |
pulumi config set:
pulumi config set gcp:project <value>pulumi config set gcp:region <value>pulumi config set nodesPerZone <value>| Key | Description | Default |
|---|---|---|
| gcp:project | The Google Cloud project to deploy into | — |
| region | The Google Cloud region to deploy into | us-central1 |
| nodes_per_zone | The number of nodes per zone in the node pool | 1 |
pulumi config set:
pulumi config set gcp:project <value>pulumi config set region <value>pulumi config set nodes_per_zone <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.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and holds the program: the resources it declares.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis 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:
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.
pulumi upPulumi 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
kubectlto access and communicate with your cluster.
- 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
kubectlto access and communicate with your cluster.
- 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
kubectlto access and communicate with your cluster.
- 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
kubectlto access and communicate with your cluster.
- 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
kubectlto access and communicate with your cluster.
- network_name
The name of the VPC network containing the Kubernetes cluster resources.
- cluster_name
The name of the GKE cluster.
- kubeconfig
The cluster’s kubeconfig file, which you can use with
kubectlto 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:
gcloud components install gke-gcloud-auth-pluginpulumi stack output kubeconfig --show-secrets > kubeconfigKUBECONFIG=./kubeconfig kubectl get nodesGKE 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.
- 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.
- 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.
- 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.
- 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.
- region
The Google Cloud region to deploy into. Defaults to
us-central1.- nodes_per_zone
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:
pulumi config set nodesPerZone 2pulumi uppulumi config set nodesPerZone 2pulumi uppulumi config set nodesPerZone 2pulumi uppulumi config set nodesPerZone 2pulumi uppulumi config set nodesPerZone 2pulumi uppulumi config set nodes_per_zone 2pulumi upWhen the update finishes, list the nodes again; there are six:
KUBECONFIG=./kubeconfig kubectl get nodesTo 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:
pulumi destroyLearn more#
- Browse other architecture templates in the Templates gallery.
- Explore the Google Cloud provider API docs in the Pulumi Registry.
- Walk through Pulumi from the ground up in Pulumi Tutorials.
- Read the latest Google Cloud posts on the Pulumi blog.