Deploy a GKE cluster and then deploy a Helm chart to that cluster
JavaYour Pulumi Java program will include two main steps:
- Provision a GKE Cluster using the gcp.container.Cluster resource
- Deploy a Helm Chart to the newly created cluster using the
kubernetes.helm.v3.Chart
resource
Here's a simple example of how to do this:
import com.pulumi.*; import com.pulumi.gcp.container.Cluster; import com.pulumi.kubernetes.helm.v3.*; import java.util.*; class DeployGkeClusterAndHelmChart extends Stack { public DeployGkeClusterAndHelmChart() { // Provision a GKE Cluster Cluster cluster = new Cluster("gke-cluster", new ClusterArgs.Builder() .setName("gke-cluster") .setLocation("us-central1-a") .setInitialNodeCount(2) .build()); // Export the Cluster's name this.export("kubeConfig", cluster.getMasterAuth().apply(masterAuth -> Output.all(masterAuth.getClusterCaCertificate(), masterAuth.getClientCertificate(), masterAuth.getClientKey()).apply(args -> { String clusterCert = args.get(0); String masterCert = args.get(1); String masterKey = args.get(2); Map<String, Object> user = new HashMap<>(); user.put("client-certificate-data", masterCert); user.put("client-key-data", masterKey); Map<String, Object> clusterData = new HashMap<>(); clusterData.put("certificate-authority-data", clusterCert); clusterData.put("server", String.format("https://%s", cluster.getEndpoint())); Map<String, Object> context = new HashMap<>(); context.put("cluster", "my-cluster"); context.put("user", "my-user"); Map<String, Object> kubeConfig = new HashMap<>(); kubeConfig.put("apiVersion", "v1"); kubeConfig.put("kind", "Config"); kubeConfig.put("clusters", Collections.singletonList(Collections.singletonMap("name", "my-cluster", clusterData))); kubeConfig.put("contexts", Collections.singletonList(Collections.singletonMap("name", "my-context", context))); kubeConfig.put("users", Collections.singletonList(Collections.singletonMap("name", "my-user", user))); kubeConfig.put("current-context", "my-context"); return kubeConfig; }))); // Export the Kubeconfig this.export("kubeconfig", kubeconfig); // Deploy a Helm Chart to the newly created cluster Provider k8sProvider = new Provider("k8sProvider", new ProviderArgs.Builder() .setKubeconfig(kubeconfig) .build()); new Chart("nginx", new ChartArgs.Builder() .setChart("nginx") .setVersion("1.14.2") .setFetchOpts(new FetchOptsArgs.Builder() .setRepo("https://charts.bitnami.com/bitnami") .build()) .setNamespace("default") .setProvider(k8sProvider) .build()); } } class Program { public static void main(String[] args) { Pulumi.run(DeployGkeClusterAndHelmChart::new); } }
This program first creates a Google Kubernetes Engine (GKE) cluster with 2 nodes (see gcp.container.Cluster) and exports the kubeconfig for the cluster. Please replace
"us-central1-a"
with the appropriate zone for your use case.To connect to the GKE cluster, the program creates a Kubernetes Provider with the kubeconfig of the GKE cluster.
The Helm chart
"nginx"
from Bitnami's Helm chart repository is then deployed to the GKE cluster (see kubernetes.helm.v3.Chart). Thenamespace
value of"default"
specifies the namespace that the chart should be installed into. If needed, you can adjust this value, the chart name/version and repository to match the Helm chart you wish to deploy.When running the program, it will both deploy the GKE cluster and the Helm chart in a single
pulumi up
operation.