Skip to content

Commit

Permalink
Add GKE cluster example
Browse files Browse the repository at this point in the history
  • Loading branch information
polkx authored and pawelprazak committed Apr 19, 2024
1 parent 7baa3a1 commit c22acfd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/gcp-gke/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Scala an JVM
*.class
*.log
.bsp
.scala-build

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
67 changes: 67 additions & 0 deletions examples/gcp-gke/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import besom.*
import besom.api.gcp

@main def main = Pulumi.run {
val kubernetesEngine = gcp.projects.Service(
name = "enable-kubernetes-engine",
gcp.projects.ServiceArgs(
service = "container.googleapis.com",
/* if true - at every destroy this will disable the dependent services for the whole project */
disableDependentServices = true,
/* if true - at every destroy this will disable the service for the whole project */
disableOnDestroy = true
)
)

val k8sCluster = gcp.container.Cluster(
name = "cluster",
gcp.container.ClusterArgs(
deletionProtection = false,
initialNodeCount = 1,
minMasterVersion = "1.29.1-gke.1589018",
nodeVersion = "1.29.1-gke.1589018",
nodeConfig = gcp.container.inputs.ClusterNodeConfigArgs(
machineType = "n1-standard-1",
oauthScopes = List(
"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"
)
)
),
opts = opts(dependsOn = kubernetesEngine)
)

val context = p"${k8sCluster.project}_${k8sCluster.location}_${k8sCluster.name}"
val kubeconfig =
p"""apiVersion: v1
|clusters:
|- cluster:
| certificate-authority-data: ${k8sCluster.masterAuth.clusterCaCertificate.map(_.get).asPlaintext}
| server: https://${k8sCluster.endpoint}
| name: $context
|contexts:
|- context:
| cluster: $context
| user: $context
| name: $context
|current-context: $context
|kind: Config
|preferences: {}
|users:
|- name: $context
| user:
| exec:
| apiVersion: client.authentication.k8s.io/v1beta1
| command: gke-gcloud-auth-plugin
| installHint: Install gke-gcloud-auth-plugin for use with kubectl by following
| https://cloud.google.com/blog/products/containers-kubernetes/kubectl-auth-changes-in-gke
| provideClusterInfo: true
|""".stripMargin

Stack.exports(
clusterName = k8sCluster.name,
kubeconfig = kubeconfig
)
}
3 changes: 3 additions & 0 deletions examples/gcp-gke/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: gcp-gke
runtime: scala
description: A Google Kubernetes Engine (GKE) example
74 changes: 74 additions & 0 deletions examples/gcp-gke/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Google Kubernetes Engine (GKE) Cluster

This example deploys an Google Cloud Platform (
GCP) [Google Kubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/) cluster.

## Deploying the App

To deploy your infrastructure, follow the below steps.

### Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
2. [Install Google Cloud SDK (`gcloud`)](https://cloud.google.com/sdk/docs/downloads-interactive)
3. Configure GCP Auth

* Login using `gcloud`

```bash
$ gcloud auth login
$ gcloud config set project <YOUR_GCP_PROJECT_HERE>
$ gcloud auth application-default login
```
> Note: This auth mechanism is meant for inner loop developer
> workflows. If you want to run this example in an unattended service
> account setting, such as in CI/CD, please [follow instructions to
> configure your service account](https://www.pulumi.com/docs/intro/cloud-providers/gcp/setup/). The
> service account must have the role `Kubernetes Engine Admin` / `container.admin`.

## Running the App

1. Create a new stack:

```bash
$ pulumi stack init dev
```

2. Set the required GCP configuration variables:

```bash
$ pulumi config set gcp:project <YOUR_GCP_PROJECT_HERE>
$ pulumi config set gcp:zone us-west1-a // any valid GCP Zone here
```

3. Stand up the GKE cluster by invoking pulumi.

```bash
$ pulumi up
```

4. After 3-5 minutes, your cluster will be ready, and the `kubeconfig` JSON you'll use to connect to the cluster will
be available as an output.
5. Access the Kubernetes Cluster using `kubectl`
To access your new Kubernetes cluster using `kubectl`, we need to setup the
`kubeconfig` file and download `kubectl`. We can leverage the Pulumi
stack output in the CLI, as Pulumi facilitates exporting these objects for us.
```bash
$ pulumi stack output kubeconfig > kubeconfig
$ kubectl --kubeconfig=./kubeconfig.json get all --all-namespaces
```
6. From there, feel free to experiment. Simply making edits and running pulumi up will incrementally update your
infrastructure.
7. To clean up resources, destroy your stack and remove it:
```bash
$ pulumi destroy
```
```bash
$ pulumi stack rm gcp-static-page-dev
```
4 changes: 4 additions & 0 deletions examples/gcp-gke/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using scala "3.3.1"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
//> using dep "org.virtuslab::besom-core:0.3.0"
//> using dep "org.virtuslab::besom-gcp:7.18.0-core.0.3"

0 comments on commit c22acfd

Please sign in to comment.