Deploy the sample-chart helm chart on Digital Ocean Kubernetes Service
TypeScriptTo deploy a Helm chart on the Digital Ocean Kubernetes Service using Pulumi, you will need to create a Kubernetes cluster on Digital Ocean first and then deploy the Helm chart to that cluster. Below is a step-by-step guide and a corresponding TypeScript program to accomplish this task.
The process involves two main steps:
-
Creating a Digital Ocean Kubernetes Cluster: You will use the
digitalocean.KubernetesCluster
resource to create a new Kubernetes cluster. You specify properties such as the region, version, node pool specifications, etc. -
Deploying a Helm Chart: After the cluster is available, you utilize the
kubernetes.helm.v3.Chart
resource to deploy your Helm chart onto the Kubernetes cluster. The chart can be from any Helm chart repository or a local path.
Here is a detailed explanation of the Pulumi TypeScript program:
import * as pulumi from "@pulumi/pulumi"; import * as digitalocean from "@pulumi/digitalocean"; import * as k8s from "@pulumi/kubernetes"; import * as random from "@pulumi/random"; // Step 1: Create a new Digital Ocean Kubernetes cluster const cluster = new digitalocean.KubernetesCluster("do-cluster", { region: "nyc1", // Choose the region that is closest to you or your users version: "1.21.5-do.0", // Specify the version of Kubernetes nodePool: { name: "default-pool", // Define the node pool name size: "s-1vcpu-2gb", // Choose the size of the nodes (VMs) nodeCount: 2, // Specify the number of nodes }, }); // Step 2: Use the Digital Ocean Kubernetes cluster's kubeconfig to interact with the cluster const kubeconfig = pulumi.all([cluster.status, cluster.kubeConfigs]).apply(([status, kubeConfigs]) => { if (status === "running") { return kubeConfigs[0].rawConfig; } else { throw new Error("The Digital Ocean Kubernetes cluster is not running."); } }); // Step 3: Create a Kubernetes provider instance using the kubeconfig from the Digital Ocean cluster const k8sProvider = new k8s.Provider("do-k8s", { kubeconfig: kubeconfig, }); // Step 4: Deploy the Helm chart onto the Kubernetes cluster using the provider const chart = new k8s.helm.v3.Chart("sample-chart", { chart: "nginx", // Replace 'nginx' with 'sample-chart' // You can specify a 'repo' property if your Helm chart is not in the default Helm repository. version: "1.16.1", // Replace with the version of your Helm chart // If you have custom values, you can specify them using the 'values' property: // values: { key: "value" }, }, { provider: k8sProvider }); // Step 5: Export the public endpoint of the Helm chart (if applicable) export const endpoint = chart.getResourceProperty("v1/Service", "sample-chart-nginx-svc", "status") .apply(status => { if (status.loadBalancer.ingress && status.loadBalancer.ingress.length > 0) { return status.loadBalancer.ingress[0].ip; } return "LoadBalancer is not assigned an external IP yet."; });
Now, let's explain each step:
-
First, you create a Digital Ocean Kubernetes cluster with the desired specifications (region, version, node pool size and count).
-
After the cluster is created, you retrieve and use its kubeconfig to set up a Kubernetes provider that allows Pulumi to interact with the cluster.
-
With the Kubernetes provider configured, you deploy the Helm chart by specifying its name and version. The
chart
property is where you'd put the name of your Helm chart (in this example,nginx
is used as a placeholder). Theversion
property is the version of the Helm chart you want to deploy. Thevalues
property would hold any customization you wish to apply to the default chart settings. -
An export is provided at the end of the program, which will output the endpoint IP address of the service created by the Helm chart, if available.
Once the program is set up, you can deploy your infrastructure with Pulumi's CLI commands:
- Initialize a new Pulumi project and select Digital Ocean as the cloud provider.
- Place the above TypeScript code in an
index.ts
file in your Pulumi project directory. - Run
pulumi up
to create the Digital Ocean Kubernetes cluster and deploy the Helm chart.
Remember to replace
"nginx"
with the actual name of thesample-chart
you wish to deploy, and if your chart is in a Helm repository other than the default, add the'repo'
property to specify its location. If your chart requires custom values, pass them through thevalues
object.This program will require the appropriate Pulumi providers to be installed in your project, which you can do by running:
pulumi plugin install resource digitalocean v4.22.0 pulumi plugin install resource kubernetes v4.4.0
And you will also require the corresponding packages in your
package.json
file:{ "dependencies": { "@pulumi/digitalocean": "^4.22.0", "@pulumi/kubernetes": "^4.4.0", "@pulumi/pulumi": "^3.0.0", "@pulumi/random": "^4.0.0" } }
You can add these dependencies with npm or Yarn.
-