|
| 1 | +[](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/tree/master/gcp-ts-k8s-ruby-on-rails-postgresql/infra) |
| 2 | + |
| 3 | +# Containerized Ruby on Rails App Delivery using the Google Native Pulumi Provider |
| 4 | + |
| 5 | +This example is a full end to end example of delivering a containerized Ruby on Rails application. It |
| 6 | + |
| 7 | +- Provisions a [Google Kubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/) cluster |
| 8 | +- Provisions a fully managed Google Cloud SQL PostgreSQL database |
| 9 | +- Builds a containerized Ruby on Rails container image, and publishes it to the Google Container Registry |
| 10 | +- Deploys that container image as a Kubernetes Service inside of the provisioned GKE cluster |
| 11 | + |
| 12 | +All of these happen behind a single `pulumi up` command, and are expressed in just a handful of TypeScript. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +Before trying to deploy this example, please make sure you have performed all of the following tasks: |
| 17 | +- [downloaded and installed the Pulumi CLI](https://www.pulumi.com/docs/get-started/install/). |
| 18 | +- [downloaded and installed Docker](https://docs.docker.com/install/) |
| 19 | +- [signed up for Google Cloud](https://cloud.google.com/free/) |
| 20 | +- [followed the instructions here](https://www.pulumi.com/docs/intro/cloud-providers/gcp/setup/) to connect Pulumi to your Google Cloud account. |
| 21 | + |
| 22 | +This example assumes that you have Google Cloud's `gcloud` CLI on your path. This is installed as part of the |
| 23 | +[Google Cloud SDK](https://cloud.google.com/sdk/). |
| 24 | + |
| 25 | +As part of this example, we will setup and deploy a Kubernetes cluster on GKE. You may also want to install [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) if you would like to directly interact with the underlying Kubernetes cluster. |
| 26 | + |
| 27 | +## Running the Example |
| 28 | + |
| 29 | +After cloning this repo, `cd infra/` and run these commands. After 8 minutes, you'll have a fully functioning GKE |
| 30 | +cluster and containerized Ruby on Rails application deployed into it, using a hosted PostgreSQL instance! |
| 31 | + |
| 32 | +1. Create a new stack, which is an isolated deployment target for this example: |
| 33 | + |
| 34 | + ```bash |
| 35 | + $ pulumi stack init dev |
| 36 | + ``` |
| 37 | + |
| 38 | +2. Set the required configuration variables for this program: |
| 39 | + |
| 40 | + ```bash |
| 41 | + $ pulumi config set google-native:project [your-gcp-project-here] |
| 42 | + $ pulumi config set google-native:region us-west1 # any valid region |
| 43 | + ``` |
| 44 | + |
| 45 | + Since we will use Google's Container Registry for hosting the Docker image, we need to configure your machine's Docker to be able to authenticate with GCR: |
| 46 | + |
| 47 | + ```bash |
| 48 | + $ gcloud auth configure-docker # Configure docker to be able to push to your Google project's container registry |
| 49 | + ``` |
| 50 | + |
| 51 | + The following configuration variables have defaults or are automatically generated when not specified but can be explicitly specified as follows: |
| 52 | + |
| 53 | + ```bash |
| 54 | + $ pulumi config set clusterPassword --secret [your-new-cluster-password-here] # must be at least 16 characters |
| 55 | + $ pulumi config set dbUsername [your-new-db-username-here] |
| 56 | + $ pulumi config set dbPassword --secret [your-new-db-password-here] |
| 57 | + $ pulumi config set masterVersion # any valid master version, or latest |
| 58 | + ``` |
| 59 | + |
| 60 | + Config variables that use the `--secret` flag are [encrypted and not stored as plaintext](https://www.pulumi.com/docs/intro/concepts/config/#secrets). |
| 61 | + |
| 62 | + By default, your cluster will use the default nodepool. `index.ts` includes a reference incantation to enable an additional nodepool |
| 63 | + which can be configured as follows: |
| 64 | + ```bash |
| 65 | + $ pulumi config set clusterNodeCount 5 |
| 66 | + $ pulumi config set clusterNodeMachineType n1-standard-2 |
| 67 | + ``` |
| 68 | + |
| 69 | + You can even change these after provisioning. |
| 70 | + |
| 71 | +3. Deploy everything with the `pulumi up` command. This provisions all the GCP resources necessary, including |
| 72 | + your GKE cluster and database, as well as building and publishing your container image, all in a single gesture: |
| 73 | + |
| 74 | + ```bash |
| 75 | + $ pulumi up |
| 76 | + ``` |
| 77 | + |
| 78 | + This will show you a preview, ask for confirmation, and then chug away at provisioning your cluster: |
| 79 | + |
| 80 | + ``` |
| 81 | + Updating stack 'google-rails-dev' |
| 82 | + Performing changes: |
| 83 | +
|
| 84 | + Type Name Plan Info |
| 85 | + + pulumi:pulumi:Stack google-rails-dev create |
| 86 | + + ├─ docker:image:Image rails-app create |
| 87 | + + ├─ google-native:container/v1:Cluster cluster create |
| 88 | + + ├─ google-native:sqladmin/v1beta4:Instance web-db create |
| 89 | + + ├─ google-native:container/v1:ClusterNodePool primary-node-pool create |
| 90 | + + ├─ pulumi:providers:kubernetes gke-k8s create |
| 91 | + + ├─ gcp:sql:User web-db-user create |
| 92 | + + ├─ kubernetes:apps/v1:Deployment rails-deployment create |
| 93 | + + └─ kubernetes:core/v1:Service rails-service create |
| 94 | + Diagnostics: |
| 95 | + docker:image:Image (rails-app): |
| 96 | + Building container image: context=../app |
| 97 | + logging in to registry... |
| 98 | +
|
| 99 | + Sending build context to Docker daemon 22.79MB |
| 100 | + Step 1/9 : FROM ruby:2.5 |
| 101 | + ---> 8e2b5b80415f |
| 102 | + ... |
| 103 | +
|
| 104 | + ---outputs:--- |
| 105 | + appAddress: "http://32.233.14.89:3000" |
| 106 | + appName : "rails-deployment-vt7uyigk" |
| 107 | + dbAddress : "36.223.156.57" |
| 108 | + kubeConfig: "apiVersion: v1\n..." |
| 109 | +
|
| 110 | + info: 8 changes |
| 111 | + + 8 created |
| 112 | + Update duration: 7m20.867501974as |
| 113 | + ``` |
| 114 | + |
| 115 | + After this completes, numerous outputs will show up. `appAddress` is the URL that your Rails app will be available |
| 116 | + at, `appName` is the resulting Kubernetes Deployment, `dbAddress` is your PostgreSQL hostname in case you want to |
| 117 | + connect to it with `psql`, and `kubeConfig` is the full Kubernetes configuration that you can use with `kubectl`. |
| 118 | + |
| 119 | +4. Open a browser to visit the site, `open $(pulumi stack output appAddress)/todo_lists`. Make some todo lists! |
| 120 | + |
| 121 | +5. At this point, you have a running cluster. Feel free to modify your program, and run `pulumi up` to redeploy changes. |
| 122 | + The Pulumi CLI automatically detects what has changed and makes the minimal edits necessary to accomplish these |
| 123 | + changes. This could be altering the app code, adding new Google Cloud or Kubernetes resources, or anything, really. |
| 124 | + |
| 125 | +6. Once you are done, you can destroy all of the resources, and the stack: |
| 126 | + |
| 127 | + ```bash |
| 128 | + $ pulumi destroy |
| 129 | + $ pulumi stack rm |
| 130 | + ``` |
0 commit comments