Kubernetes Go Starter
A minimal Kubernetes Go Pulumi program
Before you begin#
For this walkthrough, you'll need:
- The Pulumi CLI
- The kubectl CLI
- Permission to create Deployments in the cluster
- A current version of Go installed locally
Create a new project#
This starter creates an NGINX Deployment with one replica in your cluster and exports the Deployment’s name.
mkdir my-project; cd my-projectpulumi new kubernetes-gomkdir my-project; cd my-projectpulumi new kubernetes-goThe pulumi new command walks you through creating and configuring the 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. Depending on the language, you may also be asked for your preferred package manager or toolchain.
The template has no settings of its own.
Review the code#
When setup completes, you’ll see a few files:
Pulumi.yamlcontains the project name and the language it’s written in.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.main.gocontains the program code that declares your Kubernetes resources.
Take a moment to open them up and have a look. Here’s what’s in main.go:
package main
import ( appsv1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/apps/v1" corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1" metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1" "github.com/pulumi/pulumi/sdk/v3/go/pulumi")
func main() { pulumi.Run(func(ctx *pulumi.Context) error {
appLabels := pulumi.StringMap{ "app": pulumi.String("nginx"), } deployment, err := appsv1.NewDeployment(ctx, "app-dep", &appsv1.DeploymentArgs{ Spec: appsv1.DeploymentSpecArgs{ Selector: &metav1.LabelSelectorArgs{ MatchLabels: appLabels, }, Replicas: pulumi.Int(1), Template: &corev1.PodTemplateSpecArgs{ Metadata: &metav1.ObjectMetaArgs{ Labels: appLabels, }, Spec: &corev1.PodSpecArgs{ Containers: corev1.ContainerArray{ corev1.ContainerArgs{ Name: pulumi.String("nginx"), Image: pulumi.String("nginx"), }}, }, }, }, }) if err != nil { return err }
ctx.Export("name", deployment.Metadata.Name())
return nil })}Deploy the project#
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.
pulumi up deploys to the cluster your current kubeconfig context points at, the same one kubectl uses.
Pulumi adds a random suffix to most resource names, so the Deployment the program calls app-dep gets a name like app-dep-3o5i0y8b.
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.
pulumi uppulumi upWhen it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist. This program exports the Deployment’s name as name. Read it any time with pulumi stack output name, or pass it to another command. This one shows the Deployment with its ready replica count, 1/1:
kubectl get deployment $(pulumi stack output name)kubectl get deployment $(pulumi stack output name)Need help?
If pulumi up reports that the cluster is unreachable, check which cluster kubectl points to and whether it’s running:
kubectl config current-contextkubectl cluster-infokubectl config current-contextkubectl cluster-infoRestart your local cluster if necessary, or switch to another cluster with kubectl config use-context <CONTEXT_NAME>.
Still stuck? Let us know.
Customize the project#
Make the number of copies configurable. In main.go, read replicas from the stack’s configuration instead of hard-coding it:
import ( // ... "github.com/pulumi/pulumi/sdk/v3/go/pulumi/config")
func main() { pulumi.Run(func(ctx *pulumi.Context) error { replicas, err := config.New(ctx, "").TryInt("replicas") if err != nil { replicas = 1 }Then use it for the Deployment’s replica count:
Selector: &metav1.LabelSelectorArgs{ MatchLabels: appLabels,},Replicas: pulumi.Int(replicas),The config package is new to the project, so add its dependencies to go.sum:
go mod tidygo mod tidySet replicas to 3. pulumi config set saves the value in Pulumi.dev.yaml, the dev stack’s settings file:
pulumi config set replicas 3pulumi config set replicas 3Deploy again. The preview shows the Deployment as an update:
pulumi uppulumi upThen check the Deployment again. It shows 3/3 ready:
kubectl get deployment $(pulumi stack output name)kubectl get deployment $(pulumi stack output name)To see what else you can add, browse the Kubernetes provider in the Pulumi Registry.
Cleaning up#
You can destroy the stack and all of its infrastructure with pulumi destroy, then remove the stack with pulumi stack rm:
pulumi destroypulumi stack rmpulumi destroypulumi stack rm