Deploy the cadvisor helm chart on Google Kubernetes Engine (GKE)
TypeScriptTo deploy the
cadvisor
helm chart on Google Kubernetes Engine (GKE), we will perform the following steps:- Set up a new GKE cluster.
- Create a Kubernetes
Provider
which uses the credentials of the newly created GKE cluster. - Use the
helm.v3.Chart
resource to deploy thecadvisor
chart using the above provider.
Step 1: Set up a new GKE cluster
First, we define a GKE cluster by using the
gcp.container.Cluster
class. This creates a new cluster in GKE that our application will run on.Step 2: Set up a Kubernetes Provider
Once we have a GKE cluster, we need to create a
kubernetes.Provider
resource that knows how to communicate with the GKE cluster. This provider will be used to deploy the Helm chart in the subsequent step.Step 3: Deploy
cadvisor
using HelmFinally, we define a
helm.v3.Chart
resource forcadvisor
, using thekubernetes.Provider
we set up in the previous step.Let's go through the Pulumi program in TypeScript:
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("my-gke-cluster", { initialNodeCount: 2, minMasterVersion: "latest", nodeVersion: "latest", nodeConfig: { 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; // Export the Kubeconfig to connect to the cluster 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 `; }); // Create a Kubernetes provider instance that uses our cluster from above. const k8sProvider = new k8s.Provider("my-k8s-provider", { kubeconfig: kubeconfig, }); // Deploy the cadvisor helm chart. const cadvisorChart = new k8s.helm.v3.Chart("cadvisor", { chart: "cadvisor", version: "2.5.0", // specify the exact chart version fetchOpts: { repo: "https://kubernetes.github.io/ingress-nginx" // helm repository hosting the cadvisor chart }, }, { provider: k8sProvider }); // Export the cadvisor deployment status export const cadvisorStatus = cadvisorChart.status;
Explanation:
- We first import the required Pulumi packages,
@pulumi/pulumi
for core Pulumi functionalities,@pulumi/gcp
for interacting with Google Cloud resources, and@pulumi/kubernetes
for Kubernetes resources. - We then create a GKE cluster with an initial node count of 2 and specify the machine type and OAuth scopes. The cluster details can be customized per your needs.
- The
kubeconfig
is a string that will allow us to communicate with our Kubernetes cluster. We export it so that we can use this outside of Pulumi if necessary. - We create a Kubernetes Provider that knows how to communicate with our GKE cluster using the
kubeconfig
that we generated. - We deploy the
cadvisor
Helm chart using thek8s.helm.v3.Chart
resource. We specify the chart name, version, and repository. - Finally, we export the cluster name and the Helm chart's status which can be useful for debugging and monitoring purposes.
After saving the code above into a
.ts
file, runpulumi up
in the same directory to create the resources. This will provision the GKE cluster and deploy thecadvisor
helm chart on it. Make sure you have Pulumi and GCP CLI configured in your machine to carry out these actions.