Deploy the webapp-chart helm chart on Google Kubernetes Engine (GKE)
TypeScriptTo deploy a Helm chart on Google Kubernetes Engine (GKE), we need to perform several tasks using Pulumi:
- Create a GKE cluster: This involves setting up a Kubernetes cluster on GKE where our Helm chart will be deployed.
- Deploy the Helm chart: Using the Pulumi Kubernetes provider, we can deploy Helm charts onto our GKE cluster.
Here's a breakdown of the Pulumi process:
-
Google-native.container/v1beta1.Cluster: This is a Pulumi resource to create a GKE cluster. We will define the necessary configuration to initialize the cluster, such as the machine type for the nodes, the number of nodes, and the zone where the cluster will be deployed.
-
Kubernetes provider: After creating the GKE cluster, we will configure Pulumi to use the Kubernetes provider which enables us to work with Kubernetes resources, including deploying Helm charts.
-
Kubernetes.helm.v3.Chart: This is a Pulumi resource that represents a Helm chart. We will use this to specify the chart we want to deploy, which in this case is the 'webapp-chart'.
Prerequisites
- Pulumi CLI installed and authenticated to use GCP and GKE.
- A GCP project set up with billing enabled.
- Google Kubernetes Engine API and associated APIs enabled for your project.
Now, let's proceed with the Pulumi TypeScript code to create a GKE cluster and deploy a Helm chart on it:
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("webapp-cluster", { initialNodeCount: 2, minMasterVersion: "latest", nodeVersion: "latest", nodeConfig: { machineType: "n1-standard-1", // Specifies the machine type for the cluster nodes. 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", ], }, // The zone where the cluster will be created location: "us-central1-a", }); // Export the Cluster name export const clusterName = cluster.name; // Configure the Kubernetes provider to use the created GKE cluster credentials const k8sProvider = new k8s.Provider("k8s-provider", { kubeconfig: cluster.kubeConfigRaw, }); // Deploy a Helm chart to the cluster const webappChart = new k8s.helm.v3.Chart("webapp-chart", { chart: "webapp-chart", // Set the version of the Helm chart you want to deploy. version: "1.2.3", // Add the namespace if required or remove this line to use the default namespace. namespace: "default", }, { provider: k8sProvider }); // Export the Helm release's name export const helmReleaseName = webappChart.releaseName;
Understanding the Code
The first block imports the necessary Pulumi Packages:
pulumi
: The core Pulumi package for orchestrating the deployment.gcp
: The Pulumi package for interacting with Google Cloud resources.k8s
: The Pulumi package for managing Kubernetes resources.
The second block creates a GKE cluster with a given configuration. We define the initial number of nodes, machine type, OAuth scopes for the cluster, and the location of the cluster.
The third block configures the Kubernetes provider with the credentials from the newly created cluster. It uses the
kubeconfig
property from the cluster to authenticate with the Kubernetes cluster.The fourth block deploys the Helm chart to the GKE cluster using the Kubernetes provider configured in the previous step. We specify the name of the Helm chart and its version.
Finally, we export two identifiers
clusterName
andhelmReleaseName
. TheclusterName
can be used for referencing the cluster in further commands or scripts;helmReleaseName
represents the instance of the deployed Helm chart.To run this Pulumi program, simply save the code to a file (for example
index.ts
), ensure all dependencies inpackage.json
are installed by runningnpm install
, and then execute it using the Pulumi CLI with the commandpulumi up
. Pulumi will take care of the rest, creating the resources and deploying your Helm chart to GKE.