Deploy the k8s-spot-termination-handler helm chart on Google Kubernetes Engine (GKE)
TypeScriptTo deploy the
k8s-spot-termination-handler
helm chart on Google Kubernetes Engine (GKE), we'll go through several steps with Pulumi. First, we'll create a GKE cluster, and then we'll install the Helm chart to the cluster. We will be using two main resources from Pulumi's providers: the GKE cluster from the@pulumi/gcp
package and the Helm chart from the@pulumi/kubernetes
package.Creating a GKE Cluster
We'll start by creating a GKE cluster. We'll use the
Cluster
resource from the@pulumi/gcp
package. This resource allows us to define our GKE cluster's properties, such as the location, node count, machine type, and others.Deploying the Helm Chart
Once the cluster is up and running, we will use Pulumi's
helm.v3.Chart
resource from the@pulumi/kubernetes
package to deployk8s-spot-termination-handler
. Helm charts help to deploy applications onto a Kubernetes cluster in a repeatable way. TheChart
resource allows you to manage Helm charts within Pulumi programs.Below is the detailed TypeScript program that creates a GKE cluster and deploys the
k8s-spot-termination-handler
Helm chart. Remember to have Pulumi and GCP configured before running this program.import * as gcp from "@pulumi/gcp"; import * as k8s from "@pulumi/kubernetes"; import * as pulumi from "@pulumi/pulumi"; // The name of the GKE cluster, which we'll refer to in the rest of our program. const clusterName = "gke-helm-cluster"; // Create a GKE cluster to which we'll deploy the Helm chart. const cluster = new gcp.container.Cluster(clusterName, { initialNodeCount: 2, minMasterVersion: "latest", // Use the latest version of GKE. nodeVersion: "latest", location: "us-central1", // Specify your desired location. nodeConfig: { machineType: "n1-standard-1", // Choose the machine type based on your requirements. oauthScopes: [ "https://www.googleapis.com/auth/cloud-platform", ], }, }); // Export the Cluster name export const clusterNameExport = cluster.name; // Export the Kubeconfig file to access the Cluster with kubectl export const kubeconfig = pulumi. all([cluster.name, cluster.endpoint, cluster.masterAuth]). apply(([name, endpoint, masterAuth]) => { const context = `${gcp.config.project}_${gcp.config.zone}_${name}`; return `apiVersion: v1 clusters: - cluster: certificate-authority-data: ${masterAuth.clusterCaCertificate} server: https://${endpoint} name: ${context} contexts: - context: cluster: ${context} user: ${context} name: ${context} current-context: ${context} kind: Config preferences: {} users: - name: ${context} user: auth-provider: config: cmd-args: config config-helper --format=json cmd-path: gcloud expiry-key: '{.credential.token_expiry}' token-key: '{.credential.access_token}' name: gcp `; }); // Before deploying the Helm chart, set up the Kubernetes provider. const k8sProvider = new k8s.Provider("k8sProvider", { kubeconfig: kubeconfig, }); // Deploy the k8s-spot-termination-handler Helm chart to the GKE cluster. const chart = new k8s.helm.v3.Chart("k8s-spot-termination-handler", { chart: "k8s-spot-termination-handler", version: "0.1.0", // Specify the version of the Helm chart you wish to deploy. fetchOpts:{ repo: "http://example.com/helm-charts", // Replace with the actual Helm chart repository containing the chart. }, }, { provider: k8sProvider }); // Export the deployed Helm chart app name export const appName = chart.getResourceProperty("v1/Service", "k8s-spot-termination-handler", "metadata").apply(m => m.name);
In the above program:
- We first create and configure a GKE cluster with a specified number of nodes and machine type.
- We export the cluster's name and generate a kubeconfig, which we will need for the Kubernetes provider to interact with the cluster.
- We set up the Kubernetes provider with the kubeconfig just generated.
- We then deploy the
k8s-spot-termination-handler
helm chart to the GKE cluster using the Kubernetes provider.
Make sure to replace the placeholder
http://example.com/helm-charts
with the actual Helm chart repository URL that holds thek8s-spot-termination-handler
chart.After running this program with
pulumi up
, Pulumi will automatically create the cluster for you and deploy the Helm chart. The deployed application's name and the cluster's name are exported, which you can use to interact with your cluster and check on the application. To apply changes, runpulumi up
, which will show you the proposed changes before making them. After reviewing, you can confirm to proceed with the deployment.