Deploy the sneakers helm chart on Google Kubernetes Engine (GKE)
TypeScriptTo deploy the sneakers Helm chart on a Google Kubernetes Engine (GKE) cluster using Pulumi, we will perform the following steps:
- Create a GKE cluster.
- Deploy the Helm chart to the GKE cluster.
Firstly, we will create a GKE cluster by using the
gcp.container.Cluster
class from the Pulumi GCP provider. A GKE cluster is a managed Kubernetes cluster hosted on Google Cloud with multiple worker nodes for deploying applications.Secondly, once the GKE cluster is running, we will deploy the sneakers Helm chart using the
kubernetes.helm.v3.Chart
class from the Pulumi Kubernetes provider. Helm is a package manager for Kubernetes that allows you to define, install, and upgrade complex Kubernetes applications. Helm charts are packages of pre-configured Kubernetes resources.Below is a TypeScript program that uses Pulumi to:
- Set up a new GKE cluster.
- Deploy a Helm chart named "sneakers" to this cluster.
Make sure you have Pulumi CLI installed and configured with the necessary credentials before running this program.
import * as pulumi from "@pulumi/pulumi"; import * as gcp from "@pulumi/gcp"; import * as k8s from "@pulumi/kubernetes"; // Create a GKE cluster const cluster = new gcp.container.Cluster("sneakers-cluster", { initialNodeCount: 2, nodeVersion: "latest", minMasterVersion: "latest", nodeConfig: { preemptible: true, machineType: "n1-standard-1", oauthScopes: [ "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.read_only", "https://www.googleapis.com/auth/logging.write", "https://www.googleapis.com/auth/monitoring", ], }, }); // Export the Cluster name and Kubeconfig export const clusterName = cluster.name; 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 `; }); // Set up a provider for the created cluster const clusterProvider = new k8s.Provider("gke-k8s", { kubeconfig: kubeconfig, }); // Deploy the sneakers Helm chart const sneakersChart = new k8s.helm.v3.Chart("sneakers-chart", { chart: "sneakers", version: "1.0.0", // Specify the chart version you want to deploy fetchOpts:{ repo: "http://example.com/helm-charts", // Specify the Helm repository URL }, }, { provider: clusterProvider }); // Export the Helm chart deployment status export const sneakersChartStatus = sneakersChart.status;
Explanation:
- We instantiate a
Cluster
object, specifying the initial node count, the node version, and the machine type, as well as the OAuth scopes which define the permissions for the GKE nodes. - The
kubeconfig
output is a string that provides the necessary information to configure kubectl to connect to the new GKE cluster. - We use the
Provider
class to tell Pulumi to use thekubeconfig
for deploying resources to this cluster. This works similarly to how you would configure kubectl to use a specific context. - The
Chart
object represents the deployment of the Helm chart. You must replace therepo
property infetchOpts
with the actual URL of the Helm repository that hosts the sneakers chart, and specify the correct chart version. - Finally, we export the status of the Helm chart so we can access details about the deployment using the Pulumi CLI.
You can run this program using
pulumi up
, which will perform the deployment. If you need to make changes to the Pulumi program, you can adjust the code and runpulumi up
again to update the deployment.After the successful execution of the program, you would have a running GKE cluster and the sneakers Helm chart deployed to it. You can manage the lifecycle of both the cluster and the Helm chart through Pulumi, including updates and deletions.