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.
Before you begin#
For this walkthrough, you'll need:
- The Pulumi CLI
- The kubectl CLI
- Cluster-admin access to the cluster
For this walkthrough, you'll need:
- The Pulumi CLI
- The kubectl CLI
- Cluster-admin access to the cluster
For this walkthrough, you'll need:
- The Pulumi CLI
- The kubectl CLI
- Cluster-admin access to the cluster
- A current version of Go installed locally
For this walkthrough, you'll need:
- The Pulumi CLI
- The kubectl CLI
- Cluster-admin access to the cluster
- A current version of .NET installed locally
For this walkthrough, you'll need:
- The Pulumi CLI
- The kubectl CLI
- Cluster-admin access to the cluster
For this walkthrough, you'll need:
- A current version of the Pulumi CLI (Pulumi HCL requires no additional runtime)
- The kubectl CLI
- Cluster-admin access to the cluster
Create a new project#
Create a new project from the template:
mkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-typescriptmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-typescriptmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-pythonmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-pythonmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-gomkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-gomkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-csharpmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-csharpmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-yamlmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-yamlmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-hclmkdir my-k8s-app; cd my-k8s-apppulumi new helm-kubernetes-hclThe 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:
| Key | Description | Default |
|---|---|---|
| k8sNamespace | The Kubernetes namespace to deploy into | nginx-ingress |
pulumi config set
:
pulumi config set k8sNamespace <value>pulumi config set k8sNamespace "<value>"| Key | Description | Default |
|---|---|---|
| k8sNamespace | The Kubernetes namespace to deploy into | nginx-ingress |
pulumi config set
:
pulumi config set k8sNamespace <value>pulumi config set k8sNamespace "<value>"| Key | Description | Default |
|---|---|---|
| k8sNamespace | The Kubernetes namespace to deploy into | nginx-ingress |
pulumi config set
:
pulumi config set k8sNamespace <value>pulumi config set k8sNamespace "<value>"| Key | Description | Default |
|---|---|---|
| k8sNamespace | The Kubernetes namespace to deploy into | nginx-ingress |
pulumi config set
:
pulumi config set k8sNamespace <value>pulumi config set k8sNamespace "<value>"| Key | Description | Default |
|---|---|---|
| k8sNamespace | The Kubernetes namespace to deploy into | nginx-ingress |
pulumi config set
:
pulumi config set k8sNamespace <value>pulumi config set k8sNamespace "<value>"| Key | Description | Default |
|---|---|---|
| k8s_namespace | The Kubernetes namespace to deploy into | nginx-ingress |
pulumi config set
:
pulumi config set k8s_namespace <value>pulumi config set k8s_namespace "<value>"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.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfcontains the program code that declares your Kubernetes resources.
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.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfcontains the program code that declares your Kubernetes resources.
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.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfcontains the program code that declares your Kubernetes resources.
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.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfcontains the program code that declares your Kubernetes resources.
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.
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.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfcontains 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.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tf:
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 controllerconst 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 elsewhereexport const name = ingressController.name;import pulumiimport pulumi_kubernetes as kubernetes
config = pulumi.Config()k8s_namespace = config.get("k8sNamespace", "nginx-ingress")app_labels = { "app": "nginx-ingress",}
# Create a namespace (user supplies the name of the namespace)ingress_ns = kubernetes.core.v1.Namespace( "ingressns", metadata=kubernetes.meta.v1.ObjectMetaArgs( labels=app_labels, name=k8s_namespace, ))
# Use Helm to install the Nginx ingress controlleringresscontroller = kubernetes.helm.v3.Release( "ingresscontroller", chart="nginx-ingress", namespace=ingress_ns.metadata.name, repository_opts={ "repo": "https://helm.nginx.com/stable", }, skip_crds=True, skip_await=True, values={ "controller": { "enableCustomResources": False, "ingressClass": k8s_namespace, "appprotect": { "enable": False, }, "appprotectdos": { "enable": False, }, "service": { "extraLabels": app_labels, }, }, }, version="0.14.1", opts=pulumi.ResourceOptions(delete_before_replace=True),)
# Export some values for use elsewherepulumi.export("name", ingresscontroller.name)package main
import ( corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1" helmv3 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3" metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1" "github.com/pulumi/pulumi/sdk/v3/go/pulumi" "github.com/pulumi/pulumi/sdk/v3/go/pulumi/config")
func main() { pulumi.Run(func(ctx *pulumi.Context) error { cfg := config.New(ctx, "") k8sNamespace, err := cfg.Try("k8sNamespace") if err != nil { k8sNamespace = "nginx-ingress" } appLabels := pulumi.StringMap{ "app": pulumi.String("nginx-ingress"), }
// Create a new namespace (user supplies the name of the namespace) ingressNs, err := corev1.NewNamespace(ctx, "ingressns", &corev1.NamespaceArgs{ Metadata: &metav1.ObjectMetaArgs{ Labels: pulumi.StringMap(appLabels), Name: pulumi.String(k8sNamespace), }, }) if err != nil { return err }
// Use Helm to install the Nginx ingress controller ingresscontroller, err := helmv3.NewRelease(ctx, "ingresscontroller", &helmv3.ReleaseArgs{ Chart: pulumi.String("nginx-ingress"), Namespace: ingressNs.Metadata.Name(), RepositoryOpts: &helmv3.RepositoryOptsArgs{ Repo: pulumi.String("https://helm.nginx.com/stable"), }, SkipCrds: pulumi.Bool(true), SkipAwait: pulumi.Bool(true), Values: pulumi.Map{ "controller": pulumi.Map{ "enableCustomResources": pulumi.Bool(false), "ingressClass": pulumi.String(k8sNamespace), "appprotect": pulumi.Map{ "enable": pulumi.Bool(false), }, "appprotectdos": pulumi.Map{ "enable": pulumi.Bool(false), }, "service": pulumi.Map{ "extraLabels": appLabels, }, }, }, Version: pulumi.String("0.14.1"), }, pulumi.DeleteBeforeReplace(true)) if err != nil { return err }
// Export some values for use elsewhere ctx.Export("name", ingresscontroller.Name) return nil })}using System.Collections.Generic;using Pulumi;using Kubernetes = Pulumi.Kubernetes;
return await Deployment.RunAsync(() =>{ var config = new Config(); var k8sNamespace = config.Get("k8sNamespace") ?? "nginx-ingress"; var appLabels = new InputMap<string> { { "app", "nginx-ingress" }, };
var ingressns = new Kubernetes.Core.V1.Namespace("ingressns", new() { Metadata = new Kubernetes.Types.Inputs.Meta.V1.ObjectMetaArgs { Labels = appLabels, Name = k8sNamespace, }, });
var ingresscontroller = new Kubernetes.Helm.V3.Release("ingresscontroller", new() { Chart = "nginx-ingress", Namespace = ingressns.Metadata.Apply(m => m.Name), RepositoryOpts = new Kubernetes.Types.Inputs.Helm.V3.RepositoryOptsArgs { Repo = "https://helm.nginx.com/stable", }, SkipCrds = true, SkipAwait = true, Values = new Dictionary<string, object> { ["controller"] = new Dictionary<string, object> { ["enableCustomResources"] = "false", ["ingressClass"] = k8sNamespace, ["appprotect"] = new Dictionary<string, object> { ["enable"] = "false" }, ["appprotectdos"] = new Dictionary<string, object> { ["enable"] = "false" }, ["service"] = new Dictionary<string, object> { ["extraLabels"] = appLabels }, }, }, Version = "0.14.1", }, new CustomResourceOptions { DeleteBeforeReplace = true, });
return new Dictionary<string, object?> { ["name"] = ingresscontroller.Name, };});name: my-projectdescription: A Pulumi YAML program to deploy a Helm chart onto a Kubernetes clusterruntime: yamlconfig: # Use this user-supplied value to create a Kubernetes namespace later k8sNamespace: default: nginx-ingress type: string
variables: # Define some labels that will be applied to resources appLabels: app: nginx-ingress
resources: # Create a namespace (name of the namespace supplied by the user) ingressns: type: kubernetes:core/v1:Namespace properties: metadata: labels: ${appLabels} name: ${k8sNamespace} # Use Helm to install the Nginx ingress controller ingresscontroller: type: kubernetes:helm.sh/v3:Release properties: 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" options: deleteBeforeReplace: true
# Export some values for use elsewhereoutputs: name: ${ingresscontroller.name}terraform { required_providers { kubernetes = { source = "pulumi/kubernetes" } }}
variable "k8s_namespace" { description = "The Kubernetes namespace to deploy into" type = string default = "nginx-ingress"}
locals { # Define some labels that will be applied to resources app_labels = { app = "nginx-ingress" }}
# Create a namespace (name of the namespace supplied by the user)resource "kubernetes_core_v1_namespace" "ingressns" { metadata = { name = var.k8s_namespace labels = local.app_labels }}
# Use Helm to install the Nginx ingress controllerresource "kubernetes_helm_sh_v3_release" "ingresscontroller" { chart = "nginx-ingress" namespace = kubernetes_core_v1_namespace.ingressns.metadata.name skip_crds = true skip_await = true
repository_opts = { repo = "https://helm.nginx.com/stable" }
values = { controller = { enableCustomResources = false ingressClass = var.k8s_namespace appprotect = { enable = false } appprotectdos = { enable = false } service = { extraLabels = local.app_labels } } }
version = "0.14.1"}
# Export some values for use elsewhereoutput "name" { value = kubernetes_helm_sh_v3_release.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:
kubectl config current-contextkubectl config current-contextDeploy 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 uppulumi upPulumi 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.
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.
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.
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.
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.
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 k8s_namespace, 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:
kubectl get pods -n nginx-ingresskubectl get pods -n nginx-ingressNeed 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#
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.
- k8sNamespace
The name of the namespace to be created in your existing cluster. Defaults to
nginx-ingress.
- k8sNamespace
The name of the namespace to be created in your existing cluster. Defaults to
nginx-ingress.
- k8sNamespace
The name of the namespace to be created in your existing cluster. Defaults to
nginx-ingress.
- k8sNamespace
The name of the namespace to be created in your existing cluster. Defaults to
nginx-ingress.
- k8s_namespace
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:
pulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8sNamespace my-namespacepulumi uppulumi config set k8s_namespace my-namespacepulumi uppulumi config set k8s_namespace my-namespacepulumi upPulumi 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:
kubectl get pods -n my-namespacekubectl get pods -n my-namespaceYou 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.
To see what else you can add, browse the Kubernetes provider in the Pulumi Registry.
Cleaning up#
You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:
pulumi destroypulumi destroyLearn more#
- Browse other architecture templates in the Templates gallery.
- Explore the Kubernetes provider API docs in the Pulumi Registry.
- Walk through Pulumi from the ground up in Pulumi Tutorials.
- Read the latest Kubernetes posts on the Pulumi blog.