Skip to main content

Google Kubernetes Engine (GKE) cluster

A GCP Go Pulumi program with Kubernetes

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set gcp-go-gke
cd pulumi-examples/gcp-go-gke

This example deploys a Google Cloud Platform (GCP) Google Kubernetes Engine (GKE) cluster and an application to it.

Prerequisites#

  1. Install Pulumi

  2. Configure GCP credentials

  3. Install Go

  4. Install the Google Cloud SDK (gcloud)

  5. Configure GCP auth by logging in with gcloud:

    Terminal window
    gcloud auth login
    gcloud config set project <YOUR_GCP_PROJECT_HERE>
    gcloud auth application-default login

    Note: This auth mechanism is meant for inner loop developer workflows. If you want to run this example in an unattended service account setting, such as in CI/CD, please follow instructions to configure your service account. The service account must have the role Kubernetes Engine Admin / container.admin.

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Set the required GCP configuration variables:

    Terminal window
    pulumi config set gcp:project <YOUR_GCP_PROJECT_HERE>
    pulumi config set gcp:zone us-west1-a
  3. Install dependencies:

    Terminal window
    go mod download
  4. Stand up the GKE cluster by running pulumi up. Note that provisioning a new GKE cluster takes between 3-5 minutes:

    Terminal window
    pulumi up
    Updating (dev):
    Type Name Plan
    + pulumi:pulumi:Stack gcp-go-gke-dev created
    + └─ gcp:container:Cluster helloworld created
    Outputs:
    ClusterName: "helloworld-9b9530f"
    KubeConfig : "<KUBECONFIG_CONTENTS>"
    Resources:
    + 2 created
    Duration: 3m3s
  5. After 3-5 minutes, your cluster will be ready, and the kubeconfig JSON you’ll use to connect to the cluster will be available as an output.

  6. Access the Kubernetes cluster using kubectl. Set up the kubeconfig file from the Pulumi stack output, then run your usual kubectl commands:

    Terminal window
    pulumi stack output kubeconfig --show-secrets > kubeconfig
    export KUBECONFIG=$PWD/kubeconfig
    kubectl version
    kubectl cluster-info
    kubectl get nodes

Cleaning up#

Once you’re finished experimenting, tear down your stack’s resources by destroying and removing it:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.