Deploy the static-site helm chart on Google Kubernetes Engine (GKE)
TypeScriptDeploying a static site on Google Kubernetes Engine (GKE) using a Helm chart involves several steps. Pulumi allows us to define our infrastructure as code using programming languages like TypeScript. Below, I will take you through creating a GKE cluster and then deploying a static-site Helm chart to it.
Firstly, a GKE cluster is created. Pulumi supports creating GKE clusters with its
gcp.container.Cluster
resource. After the cluster's creation, we configurekubectl
to interact with the cluster by setting up the required context. Then we move on to deploying the Helm chart. Thekubernetes.helm.v3.Chart
resource from the Pulumi Kubernetes provider is used for this purpose.The following is a detailed TypeScript program that uses Pulumi to create a GKE cluster and then deploy a static-site Helm chart:
import * as pulumi from "@pulumi/pulumi"; import * as gcp from "@pulumi/gcp"; import * as k8s from "@pulumi/kubernetes"; // Create a GCP project and region resource (if not already present). const project = gcp.config.project; const region = gcp.config.region; // Configure the GKE cluster. const cluster = new gcp.container.Cluster("gke-cluster", { initialNodeCount: 1, 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 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 client credentials for // the cluster. This is why we create a cluster-specific kubeconfig. const kubeconfig = pulumi. all([ cluster.name, cluster.endpoint, cluster.masterAuth ]). apply(([ name, endpoint, masterAuth ]) => { const context = `${project}_${region}_${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 `; }); // Provide kubectl with access to the cluster const provider = new k8s.Provider("gke-k8s", { kubeconfig: kubeconfig, }); // Deploy the static-site Helm chart to the GKE cluster const staticSiteChart = new k8s.helm.v3.Chart("static-site", { chart: "nginx", // Assuming 'nginx' is the Helm chart for your static site. version: "1.12.0", // Specify the version of the Helm chart. fetchOpts: { repo: "https://charts.bitnami.com/bitnami", // The Helm chart repository. }, }, { provider }); // Export the static-site Helm chart deployment's public endpoint, if applicable. export const staticSiteEndpoint = staticSiteChart.getResourceProperty("v1/Service", "static-site-nginx", "status").apply(status => status.loadBalancer.ingress[0].ip);
In this program, we:
- Import the necessary Pulumi libraries.
- Create a new GKE cluster with a single node pool, specifying the machine type and the OAuth scopes necessary for the nodes.
- Generate a kubeconfig file so that
kubectl
can communicate with our GKE cluster. - Set up a Pulumi Kubernetes provider that uses our generated kubeconfig.
- Deploy the static site using the Helm chart named
nginx
, which is fetched from the Bitnami Helm chart repository. You might replacenginx
with the specific chart that you're using for your static site. - Export the GKE cluster name and, if your service type is LoadBalancer, the public endpoint for accessing the static site.
Make sure you replace the placeholder for the Helm chart (
nginx
andhttps://charts.bitnami.com/bitnami
) with the actual details of your static-site Helm chart. The version (1.12.0
) also needs to be the chart version you wish to deploy.To run this program:
- Ensure Pulumi and GCP CLI (
gcloud
) are installed and configured with necessary access. - Save the code to a file named
index.ts
in a new Pulumi project directory. - Run
pulumi up
to provision the resources.
The code above is a blueprint, and you may need to change properties of the resources or add configuration based on the specifics of your deployment.