Skip to content

Commit 04afe35

Browse files
author
Vivek Lakshmanan
authored
Merge pull request #966 from pulumi/vl/GCPNativeRail
Add google-native version of gcp-ts-k8s-ruby-on-rails-postgresql
2 parents 0c530b4 + 7734019 commit 04afe35

File tree

102 files changed

+2014
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2014
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore all logfiles and tempfiles.
11+
/log/*
12+
/tmp/*
13+
!/log/.keep
14+
!/tmp/.keep
15+
16+
# Ignore uploaded files in development
17+
/storage/*
18+
!/storage/.keep
19+
20+
/node_modules
21+
/yarn-error.log
22+
23+
/public/assets
24+
.byebug_history
25+
26+
# Ignore master key for decrypting credentials and more.
27+
/config/master.key
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby-2.5.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM ruby:2.5
2+
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs bundler
3+
RUN mkdir /myapp
4+
WORKDIR /myapp
5+
COPY Gemfile /myapp/Gemfile
6+
COPY Gemfile.lock /myapp/Gemfile.lock
7+
RUN bundle install
8+
COPY . /myapp
9+
RUN rake app:update:bin
10+
CMD ["/myapp/start"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
source 'https://rubygems.org'
2+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3+
4+
ruby '2.5.9'
5+
6+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
7+
gem 'rails', '~> 5.2.5'
8+
# Use postgresql as the database for Active Record
9+
gem 'pg', '>= 0.18', '< 2.0'
10+
# Use Puma as the app server
11+
gem 'puma', '~> 3.11'
12+
# Use SCSS for stylesheets
13+
gem 'sass-rails', '~> 5.0'
14+
# Use Uglifier as compressor for JavaScript assets
15+
gem 'uglifier', '>= 1.3.0'
16+
# See https://github.com/rails/execjs#readme for more supported runtimes
17+
# gem 'mini_racer', platforms: :ruby
18+
19+
# Use CoffeeScript for .coffee assets and views
20+
gem 'coffee-rails', '~> 4.2'
21+
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
22+
gem 'turbolinks', '~> 5'
23+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
24+
gem 'jbuilder', '~> 2.5'
25+
# Use Redis adapter to run Action Cable in production
26+
# gem 'redis', '~> 4.0'
27+
# Use ActiveModel has_secure_password
28+
# gem 'bcrypt', '~> 3.1.7'
29+
30+
# Use ActiveStorage variant
31+
# gem 'mini_magick', '~> 4.8'
32+
33+
# Use Capistrano for deployment
34+
# gem 'capistrano-rails', group: :development
35+
36+
# Reduces boot times through caching; required in config/boot.rb
37+
gem 'bootsnap', '>= 1.1.0', require: false
38+
39+
group :development, :test do
40+
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
41+
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
42+
end
43+
44+
group :development do
45+
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
46+
gem 'web-console', '>= 3.3.0'
47+
gem 'listen', '>= 3.0.5', '< 3.2'
48+
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
49+
gem 'spring'
50+
gem 'spring-watcher-listen', '~> 2.0.0'
51+
end
52+
53+
group :test do
54+
# Adds support for Capybara system testing and selenium driver
55+
gem 'capybara', '>= 2.15'
56+
gem 'selenium-webdriver'
57+
# Easy installation and use of chromedriver to run system tests with Chrome
58+
gem 'chromedriver-helper'
59+
end
60+
61+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
62+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

0 commit comments

Comments
 (0)