Kubernetes YAML Starter
A minimal Kubernetes Pulumi YAML program
Before you begin#
For this walkthrough, you'll need:
- The Pulumi CLI
- The kubectl CLI
- Permission to create Deployments in the cluster
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-yamlmkdir my-project; cd my-projectpulumi new kubernetes-yamlThe 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 resources it declares.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.
Take a moment to open them up and have a look. Here’s what’s in Pulumi.yaml:
name: my-projectdescription: A minimal Kubernetes Pulumi YAML programruntime: yamlvariables: appLabels: app: nginx
resources: deployment: type: kubernetes:apps/v1:Deployment properties: spec: selector: matchLabels: ${appLabels} replicas: 1 template: metadata: labels: ${appLabels} spec: containers: - name: nginx image: nginx
outputs: name: ${deployment.metadata.name}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 deployment gets a name like deployment-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 Pulumi.yaml, read replicas from the stack’s configuration instead of hard-coding it:
pulumi new already added a config block to the end of the file. Add replicas to it:
config: # ... replicas: type: integer default: 1Then use it for the Deployment’s replica count:
selector: matchLabels: ${appLabels}replicas: ${replicas}Set 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