Skip to content

Instantly share code, notes, and snippets.

@leandronsp
Last active October 23, 2021 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandronsp/7149480799e842266265de5e02f6846e to your computer and use it in GitHub Desktop.
Save leandronsp/7149480799e842266265de5e02f6846e to your computer and use it in GitHub Desktop.
Provisioning a Kubernetes cluster on GKE using Pulumi

Install the node packages

npm install

Provision cloud resources using Pulumi

pulumi up
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
import * as k8s from "@pulumi/kubernetes";
const clusterLocation = "us-central1-a"
export const k8sCluster = new gcp.container.Cluster("my-cluster-yay", {
initialNodeCount: 1,
removeDefaultNodePool: true,
location: clusterLocation,
});
const nodePool = new gcp.container.NodePool("primary-node-pool", {
cluster: k8sCluster.name,
initialNodeCount: 1,
location: k8sCluster.location,
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"
],
},
management: {
autoRepair: true,
autoUpgrade: true,
},
}, {
dependsOn: [k8sCluster],
});
export const k8sConfig = pulumi.
all([ k8sCluster.name, k8sCluster.endpoint, k8sCluster.masterAuth ]).
apply(([ name, endpoint, auth ]) => {
const context = `${gcp.config.project}_${gcp.config.zone}_${name}`;
return `apiVersion: v1
clusters:
- cluster:
certificate-authority-data: ${auth.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
`;
});
export const k8sProvider = new k8s.Provider("gkeK8s", {
kubeconfig: k8sConfig,
}, {
dependsOn: [nodePool],
});
{
"name": "my-cluster",
"devDependencies": {
"@types/node": "^14"
},
"dependencies": {
"@pulumi/pulumi": "^3.0.0",
"@pulumi/gcp": "^5.0.0",
"@pulumi/kubernetes": "^3.0.0"
}
}
config:
gcp:project: "my-project-created-on-gcp"
name: my-cluster
runtime: nodejs
description: My Kubernetes cluster using Pulumi
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment