We are going to deploy a rails app into Google Cloud Run and Cloud SQL. This will make the rails app serverless.
We will follow the following steps to deploy our app.
When running the app, it took over 20 seconds to respond for the first run. Many configurations can be adjected to improve performance. Please feel free to comment with suggestions.
Setup Google Cloud SQL for Mysql.
-
Find the service network API section using the search bar.

-
Enable the service network API.

-
Go to the Cloud SQL section.

-
Create a new database instance.

-
Choose Mysql.

-
Setup the database name and password.

-
Click on "show more configuration options". and enable private IP.

-
Click on create and wait until the process is done.

-
Write down the private IP.
-
Click on Connect with cloud shell.

-
Connect using gcloud by pressing enter in the terminal you should see a similar command shown in your shell.

-
Enter the root password that you choice earlier and press enter.
-
Should now be connected.

-
Create the database using the following command.

-
Check that the database exists.

Setup Cloud Run
-
Clone this repository https://github.com/hadyrashwan/ruby-on-rails-getting-started and enter the folder.
-
Generate the secret key using the following command. For demo purposes, we can use this one
e2348e9aaf6da01aea29f03eef0a09a6d7c330ac2571043a126335ce62bc087f70931a54e6bf3bc8f6edff93e0fc502fb34c8af2cab31581b00507138088f230It is recommended to excuate the following command to get your own secret key.
$ rake secret
-
Update the deploy.sh file with your project ID.

-
Install gcloud from gcloud download page.
-
Run the following command to deploy.
./deploy.sh
When asked to enable the cloud build API to enable it and
then you may be asked where to deploy it choose us central.
- Write down the deployment URL.

Connect Cloud Run with Cloud Sql.
-
Open Serverless VPC page.
. -
Click on
Create Connector.
-
Setup the connector as follows and click
Create.
-
Now go to the cloud run page.

-
Open your app.

-
Edit deploy.

-
Edit the connection section.

-
Update your environment variables. They should be as follow this table:
| Key | Value |
|---|---|
| BLOG_DATABASE_HOST | Private IP |
| BLOG_DATABASE_PASSWORD | Password |
| SECRET_KEY_BASE | Secret key |
- It should look like this.

- Finally, click
Deploy.
Done
-
Your app should be ready now go to the deployment URL on your browser.
-
It may ask you to do a database migration do so.
-
In my test, the response took over 20 seconds coming from a cold start(first time hitting the app) and 254 ms afterwards.
-
I disabled the Webpack caching mechanism and used environment variables instead of the secret manger for the demo purposes.
Google Cloud docs links that helped me.
- Setup Google Cloud SQL for Mysql.
- Configuring private IP for the database .
- Install gcloud.
- Enable Cloud SQL on Cloud Run.
- Update your environment variables.




所有评论(0)