Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Helm Chart on Kubernetes

The Helm Chart template scaffolds a Pulumi project that deploys a Helm chart to an existing Kubernetes cluster using Pulumi’s Kubernetes provider. The template creates a new namespace in the cluster and installs an Nginx ingress controller as the default chart, so the project deploys end to end out of the box.

Architecture diagram of the Helm Chart template Kubernetes Cluster Namespace Service Ingress Controller Deployment

Before you begin#

For this walkthrough, you'll need:

Create a new project#

Create a new project from the template:

Terminal window
mkdir my-k8s-app; cd my-k8s-app
pulumi new helm-kubernetes-typescript

The 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.

Then it asks for the template’s setting:

KeyDescriptionDefault
k8sNamespaceThe Kubernetes namespace to deploy intonginx-ingress
You can change values like these later with pulumi config set :
Terminal window
pulumi config set k8sNamespace <value>

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.
  • index.ts 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 index.ts:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as kubernetes from "@pulumi/kubernetes";
const config = new pulumi.Config();
const k8sNamespace = config.get("k8sNamespace") || "nginx-ingress";
const appLabels = {
app: "nginx-ingress",
};
// Create a namespace (user supplies the name of the namespace)
const ingressNs = new kubernetes.core.v1.Namespace("ingressns", {metadata: {
labels: appLabels,
name: k8sNamespace,
}});
// Use Helm to install the Nginx ingress controller
const ingressController = new kubernetes.helm.v3.Release("ingresscontroller", {
chart: "nginx-ingress",
namespace: ingressNs.metadata.name,
repositoryOpts: {
repo: "https://helm.nginx.com/stable",
},
skipCrds: true,
skipAwait: true,
values: {
controller: {
enableCustomResources: false,
ingressClass: k8sNamespace,
appprotect: {
enable: false,
},
appprotectdos: {
enable: false,
},
service: {
extraLabels: appLabels,
},
},
},
version: "0.14.1",
}, {
deleteBeforeReplace: true,
});
// Export some values for use elsewhere
export const name = ingressController.name;

Deploy the project#

The program installs the chart into the cluster your current kubeconfig context points at. Check which cluster that is before you deploy:

Terminal window
kubectl config current-context

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.

Terminal window
pulumi up

Pulumi adds a random suffix to most resource names, so the Helm release ingresscontroller becomes something like ingresscontroller-4f8a2c1d. This is autonaming. The namespace is the exception: the program names it from k8sNamespace, so it’s called exactly what you entered (nginx-ingress by default). The chart’s cluster-wide IngressClass is named after the namespace too, so to run a second stack of this template in the same cluster, give it a different namespace.

The ingress controller gets a LoadBalancer Service. On kind and minikube, its external IP stays <pending>, and so does a second stack’s on Docker Desktop, which gives only one stack your machine’s ports 80 and 443. The deploy still succeeds, and the controller runs.

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist:

name

The name of your new Helm chart deployment.

Read one any time with pulumi stack output <name>, or pass it to another command. To check the result, list the pods in the namespace (use yours if you didn’t keep nginx-ingress). The ingress controller’s pod shows Running once it’s up:

Terminal window
kubectl get pods -n nginx-ingress
Need help?

If pulumi up reports that the cluster is unreachable, check which cluster kubectl points to and whether it’s running:

Terminal window
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.

Customize the project#

Projects created with this template expose the following configuration settings:

k8sNamespace

The name of the namespace to be created in your existing cluster. Defaults to nginx-ingress.

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, move the chart to a namespace called my-namespace:

Terminal window
pulumi config set k8sNamespace my-namespace
pulumi up

Pulumi removes the release and the old namespace, then installs the chart again in the new namespace. The controller’s pod now runs in my-namespace:

Terminal window
kubectl get pods -n my-namespace

You can customize the Helm chart by passing values to it in your Pulumi code. An example of passing a few values to the chart is included in the template for reference.

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure platform for humans and agents.