Created
October 26, 2020 22:33
-
-
Save pulumipus/0c6e4ede557600e114885869ea0aeb96 to your computer and use it in GitHub Desktop.
Kubernetes deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as gcp from "@pulumi/gcp"; | |
import * as k8s from "@pulumi/kubernetes"; | |
import * as pulumi from "@pulumi/pulumi"; | |
const name = "helloworld"; | |
const config = new pulumi.Config(); | |
export const masterVersion = config.get("masterVersion") || | |
gcp.container.getEngineVersions().then(it => it.latestMasterVersion); | |
// Create a GKE cluster | |
const cluster = new gcp.container.Cluster(name, { | |
// We can't create a cluster with no node pool defined, but we want to only use | |
// separately managed node pools. So we create the smallest possible default | |
// node pool and immediately delete it. | |
initialNodeCount: 1, | |
removeDefaultNodePool: true, | |
minMasterVersion: masterVersion, | |
resourceLabels: { | |
"stack": pulumi.getStack() | |
}, | |
}); | |
const nodePool = new gcp.container.NodePool(`primary-node-pool`, { | |
cluster: cluster.name, | |
initialNodeCount: 2, | |
location: cluster.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", | |
], | |
}, | |
version: masterVersion, | |
management: { | |
autoRepair: true, | |
}, | |
}, { | |
dependsOn: [cluster], | |
}); | |
const clusterName = cluster.name; | |
// Manufacture a GKE-style kubeconfig. Note that this is slightly "different" | |
// because of the way GKE requires gcloud to be in the picture for cluster | |
// authentication (rather than using the client cert/key directly). | |
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 clusterProvider = new k8s.Provider(name, { | |
kubeconfig: kubeconfig, | |
}, { | |
dependsOn: [nodePool], | |
}); | |
// Create a Kubernetes Namespace | |
const ns = new k8s.core.v1.Namespace(name, {}, { provider: clusterProvider }); | |
const namespaceName = ns.metadata.name; | |
// Create a NGINX Deployment | |
const appLabels = { appClass: name }; | |
const deployment = new k8s.apps.v1.Deployment(name, | |
{ | |
metadata: { | |
namespace: namespaceName, | |
labels: appLabels, | |
}, | |
spec: { | |
replicas: 1, | |
selector: { matchLabels: appLabels }, | |
template: { | |
metadata: { | |
labels: appLabels, | |
}, | |
spec: { | |
containers: [ | |
{ | |
name: name, | |
image: "nginx:latest", | |
ports: [{ name: "http", containerPort: 80 }], | |
}, | |
], | |
}, | |
}, | |
}, | |
}, | |
{ | |
provider: clusterProvider, | |
}, | |
); | |
// Create a LoadBalancer Service for the NGINX Deployment | |
const service = new k8s.core.v1.Service(name, | |
{ | |
metadata: { | |
labels: appLabels, | |
namespace: namespaceName, | |
}, | |
spec: { | |
type: "LoadBalancer", | |
ports: [{ port: 80, targetPort: "http" }], | |
selector: appLabels, | |
}, | |
}, | |
{ | |
provider: clusterProvider, | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment