|
| 1 | +[](https://app.pulumi.com/new) |
| 2 | + |
| 3 | +# Google Kubernetes Engine (GKE) with a Canary Deployment |
| 4 | + |
| 5 | +This example provisions a [Google Kubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/) cluster, using |
| 6 | +infrastructure-as-code, and then deploys a Kubernetes Deployment into it, to test that the cluster is working. This |
| 7 | +demonstrates that you can manage both the Kubernetes objects themselves, in addition to underlying cloud infrastructure, |
| 8 | +using a single configuration language (in this case, Python), tool, and workflow. |
| 9 | + |
| 10 | +# Prerequisites |
| 11 | + |
| 12 | +Ensure you have [Python 3](https://www.python.org/downloads/) and [the Pulumi CLI](https://pulumi.io/install). |
| 13 | + |
| 14 | +We will be deploying to Google Cloud Platform (GCP), so you will need an account. If you don't have an account, |
| 15 | +[sign up for free here](https://cloud.google.com/free/). In either case, |
| 16 | +[follow the instructions here](https://pulumi.io/quickstart/gcp/setup.html) to connect Pulumi to your GCP account. |
| 17 | + |
| 18 | +This example assumes that you have GCP's `gcloud` CLI on your path. This is installed as part of the |
| 19 | +[GCP SDK](https://cloud.google.com/sdk/). |
| 20 | + |
| 21 | +# Running the Example |
| 22 | + |
| 23 | +After cloning this repo, `cd` into it and run these commands. A GKE Kubernetes cluster will appear! |
| 24 | + |
| 25 | +1. Create a new stack, which is an isolated deployment target for this example: |
| 26 | + |
| 27 | + ```bash |
| 28 | + $ pulumi stack init dev |
| 29 | + ``` |
| 30 | + |
| 31 | +2. Set the required configuration variables for this program: |
| 32 | + |
| 33 | + ```bash |
| 34 | + $ pulumi config set gcp:project [your-gcp-project-here] |
| 35 | + $ pulumi config set gcp:zone us-west1-a # any valid GCP zone here |
| 36 | + $ pulumi config set password --secret [your-cluster-password-here] |
| 37 | + ``` |
| 38 | + |
| 39 | + By default, your cluster will have 3 nodes of type `n1-standard-1`. This is configurable, however; for instance |
| 40 | + if we'd like to choose 5 nodes of type `n1-standard-2` instead, we can run these commands: |
| 41 | +
|
| 42 | + ```bash |
| 43 | + $ pulumi config set node_count 5 |
| 44 | + $ pulumi config set node_machine_type n1-standard-2 |
| 45 | + ``` |
| 46 | +
|
| 47 | + This shows how stacks can be configurable in useful ways. You can even change these after provisioning. |
| 48 | +
|
| 49 | +3. Deploy everything with the `pulumi up` command. This provisions all the GCP resources necessary, including |
| 50 | + your GKE cluster itself, and then deploys a Kubernetes Deployment running nginx, all in a single gesture: |
| 51 | +
|
| 52 | + ```bash |
| 53 | + $ pulumi up |
| 54 | + ``` |
| 55 | +
|
| 56 | + This will show you a preview, ask for confirmation, and then chug away at provisioning your cluster: |
| 57 | +
|
| 58 | + ``` |
| 59 | + Updating stack 'gcp-ts-gke-dev' |
| 60 | + Performing changes: |
| 61 | +
|
| 62 | + Type Name Plan |
| 63 | + + pulumi:pulumi:Stack gcp-py-dev create |
| 64 | + + ├─ gcp:container:Cluster gke-cluster create |
| 65 | + + ├─ pulumi:providers:kubernetes gkeK8s create |
| 66 | + + └─ kubernetes:apps:Deployment canary create |
| 67 | +
|
| 68 | + ---outputs:--- |
| 69 | + kubeConfig: "apiVersion: v1\n..." |
| 70 | +
|
| 71 | + info: 4 changes updated: |
| 72 | + + 4 resources created |
| 73 | + Update duration: 2m07.424737735s |
| 74 | + ``` |
| 75 | +
|
| 76 | + After about two minutes, your cluster will be ready, and its config will be printed. |
| 77 | +
|
| 78 | +4. From here, you may take this config and use it either in your `~/.kube/config` file, or just by saving it |
| 79 | + locally and plugging it into the `KUBECONFIG` envvar. All of your usual `gcloud` commands will work too, of course. |
| 80 | +
|
| 81 | + For instance: |
| 82 | +
|
| 83 | + ```bash |
| 84 | + $ pulumi stack output kubeconfig > kubeconfig.yaml |
| 85 | + $ KUBECONFIG=./kubeconfig.yaml kubectl get po |
| 86 | + NAME READY STATUS RESTARTS AGE |
| 87 | + canary-n7wfhtrp-fdbfd897b-lrm58 1/1 Running 0 58s |
| 88 | + ``` |
| 89 | +
|
| 90 | +5. At this point, you have a running cluster. Feel free to modify your program, and run `pulumi up` to redeploy changes. |
| 91 | + The Pulumi CLI automatically detects what has changed and makes the minimal edits necessary to accomplish these |
| 92 | + changes. This could be altering the existing chart, adding new GCP or Kubernetes resources, or anything, really. |
| 93 | +
|
| 94 | +6. Once you are done, you can destroy all of the resources, and the stack: |
| 95 | +
|
| 96 | + ```bash |
| 97 | + $ pulumi destroy |
| 98 | + $ pulumi stack rm |
| 99 | + ``` |
0 commit comments