---
title: "Kubernetes Go Starter"
description: "A minimal Kubernetes Go Pulumi program"
url: "https://www.pulumi.com/dev/templates/kubernetes-go/"
image: "https://www.pulumi.com/assets/og/dev/templates/kubernetes-go.png"
---

# Kubernetes Go Starter

A minimal Kubernetes Go Pulumi program

## Before you begin

For this walkthrough, you'll need:

- [x] The [Pulumi CLI](https://www.pulumi.com/docs/install/)
- [x] Access to a Kubernetes cluster. You can run one locally with [kind](https://kind.sigs.k8s.io/), [minikube](https://minikube.sigs.k8s.io/docs/start/), or [Docker Desktop](https://docs.docker.com/desktop/use-desktop/kubernetes/)
- [x] The [kubectl](https://kubernetes.io/docs/tasks/tools/) CLI
- [x] Permission to create Deployments in the cluster
- [x] A current version of [Go](https://go.dev/doc/install) installed locally

## Create a new project

This starter creates an NGINX Deployment with one replica in your cluster and exports the Deployment's name.

```bash
mkdir my-project; cd my-project
pulumi new kubernetes-go
```

The `pulumi new` command walks you through creating and configuring the new project and stack. A [stack](https://www.pulumi.com/docs/iac/concepts/stacks/) is an instance of a project, and you can have many of them (`dev`, `staging`, `prod`), each with its own [configuration](https://www.pulumi.com/docs/iac/concepts/config/) 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.yaml` contains the project name and the language it's written in.
- `Pulumi.dev.yaml` contains the `dev` stack's settings, including the values you just entered.
- `main.go` contains 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`:

```go title="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`](https://www.pulumi.com/docs/iac/cli/commands/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](https://www.pulumi.com/docs/iac/concepts/resources/names/#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.

```bash
$ pulumi up
```

When 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`:

```bash
$ 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:

```bash
$ kubectl config current-context
$ kubectl cluster-info
```

Restart your local cluster if necessary, or switch to another cluster with `kubectl config use-context <CONTEXT_NAME>`.

Still stuck? [Let us know](https://github.com/pulumi/docs/issues/new).

## Customize the project

Make the number of copies configurable. In `main.go`, read `replicas` from the stack's [configuration](https://www.pulumi.com/docs/iac/concepts/config/) instead of hard-coding it:

```go
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:

```go
Selector: &metav1.LabelSelectorArgs{
	MatchLabels: appLabels,
},
Replicas: pulumi.Int(replicas),
```

The `config` package is new to the project, so add its dependencies to `go.sum`:

```bash
$ go mod tidy
```

Set `replicas` to `3`. `pulumi config set` saves the value in `Pulumi.dev.yaml`, the `dev` stack's settings file:

```bash
$ pulumi config set replicas 3
```

Deploy again. The preview shows the Deployment as an update:

```bash
$ pulumi up
```

Then check the Deployment again. It shows `3/3` ready:

```bash
$ kubectl get deployment $(pulumi stack output name)
```

To see what else you can add, browse the [Kubernetes provider](https://www.pulumi.com/registry/packages/kubernetes/) in the Pulumi Registry.

## Cleaning up

You can destroy the stack and all of its infrastructure with [`pulumi destroy`](https://www.pulumi.com/docs/iac/cli/commands/pulumi_destroy), then remove the stack with [`pulumi stack rm`](https://www.pulumi.com/docs/iac/cli/commands/pulumi_stack_rm):

```bash
$ pulumi destroy
$ pulumi stack rm
```

## Links

- Deploy with Pulumi: https://app.pulumi.com/new?template=https%3A%2F%2Fgithub.com%2Fpulumi%2Ftemplates%2Ftree%2Fmaster%2Fkubernetes-go
- Source on GitHub: https://github.com/pulumi/templates/tree/master/kubernetes-go
