1. Deploy the preemptible-killer helm chart on Google Kubernetes Engine (GKE)


    To deploy a Helm chart on a Google Kubernetes Engine (GKE) cluster using Pulumi, you must first ensure you have a GKE cluster running. If you don't already have a GKE cluster, you'll need to create one. In this program, we will create a simple cluster, and then deploy the "preemptible-killer" Helm chart. The preemptible-killer is typically used to gracefully handle the deletion of preemptible VM instances within a GKE cluster.

    We'll perform the following steps:

    1. Create a GKE cluster
    2. Deploy the preemptible-killer Helm chart to the GKE cluster

    Here's a TypeScript program that uses @pulumi/gcp and @pulumi/kubernetes packages to accomplish this:

    import * as gcp from "@pulumi/gcp"; import * as k8s from "@pulumi/kubernetes"; import * as pulumi from "@pulumi/pulumi"; // Create a GKE cluster const cluster = new gcp.container.Cluster("gke-cluster", { initialNodeCount: 1, nodeConfig: { preemptible: true, machineType: "n1-standard-1", }, }); // Export the Cluster name export const clusterName = cluster.name; // Manufacture a GKE-style kubeconfig. Note that this is slightly "different" // because of the way GKE requires gcloud to get the credentials. 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 `; }); // Create a Kubernetes provider instance that uses our cluster from above. const clusterProvider = new k8s.Provider("gke-cluster-provider", { kubeconfig: kubeconfig, }); // Deploy the preemptible-killer Helm chart const preemptibleKillerChart = new k8s.helm.v3.Chart("preemptible-killer", { chart: "preemptible-killer", version: "0.1.4", // specify the version of the chart fetchOpts:{ repo: "http://charts.estafette.io", // specify the Helm chart repository }, }, { provider: clusterProvider }); // Export the preemptible-killer deployment name export const preemptibleKillerName = preemptibleKillerChart.getResourceName("v1/Deployment", "preemptible-killer");

    Here's what each part of the code is doing:

    1. We import the necessary Pulumi packages for GCP and Kubernetes.

    2. We define the GKE cluster using @pulumi/gcp, specifying the count and configuration of initial nodes. Note that we set preemptible: true to indicate we're using preemptible VMs.

    3. We construct a kubeconfig that Pulumi can use to communicate with our new GKE cluster. GKE requires a little bit of a different kubeconfig than you might be used to, which includes a command to fetch credentials using gcloud.

    4. We create a Kubernetes provider pointing at the kubeconfig, which configures the Kubernetes API client for Pulumi.

    5. We deploy the preemptible-killer Helm chart to our GKE cluster using the Pulumi Kubernetes provider. We specify the chart name, version, and repository URL according to where the Helm chart is hosted.

    6. We export the cluster name and the deployment name of preemptible killer for easy access.

    After finalizing this program:

    • Run pulumi up to deploy the stack.
    • Run pulumi stack output to see the outputs (the cluster name and the preemptible killer deployment name in this case).
    • You may want to inspect or manage the resources using the kubectl command, the Kubernetes Dashboard, or any other preferred tool for Kubernetes management.

    Remember to replace the chart version with the version you intend to deploy if it's different than the one specified and adjust the repository URL if the chart has been moved or if you're deploying a different chart that might be hosted elsewhere.