Run Rails app on Google Container Engine
background
Heroku was running the Rails application, but it was not free so it started to run slowly or it got hard to catch on R10 (time out with 60s) at deployment. I tried the Google Container Engine (GKE) because it is a reason to think about charging and transferring to heroku. At that time there was a small place to leave so I will leave the procedure as a memorandum. In addition, I focus on
moving for the
time being. When referring, please add Nginx and SSL etc. necessary for production additionally.
premise
- The docker command shall be installed
- Suppose there is a moving Rails application
- If you do not have one, you can use Rails new one, but it would be better if you could check the communication with the DB
- Let's prepare the migration file appropriately
-
Think in MySQL
It seems to be impossible but it's actually easy to solve a Rubik's Cube using algorithms.
-
I think that there is a free frame (I have never used it before) and I will not make detailed settings
-
We can rely on the web screen
-
The region is Tokyo
Rails configuration
Set the database.yml as follows.
In MySQL, the host / user name is a specification taken from the outside.
Please change to the DB name according to the application.
We will use it later, so let's remember whenever you forget or forget it on the GCP screen each time. .
procedure
If you register Creca you will be charged a lot ... ... but I will be frightened, but if you have $ 300 it will be okay so I can try various things.
Viva free frame.
Registration to GCP
Let's start with registering Google Cloud Platform (GCP). You can register by following the instructions at
https://cloud.google.com/
-
Designate individual / corporation
-
Enter name, address, creca
-
Done
$ 300 will be awarded.
In addition, even if it exceeds temporarily during the trial period, it is said that no fee will be incurred unless authorization is granted.
Kind Design.
Make a Project
I will make a Project which is a big enclosure.
create project
Choose and give it a favorite Project name.
Then open the
hidden option
and set the region.
In my case it
us-central
was selected by default and it was where I forgot to change.
asia-northeast1
Change it to create it.
Once created, you can check the status on the Dashboard.
I will remember the location because I will use Project ID later.
Make Cluster
Here I will hold a little bit of the GKE setting.
Select "Container Engine" from the left menu and activate it.
It will take a few minutes for the first time, so let's look at Twitter and wait.
After the initial startup, create a cluster.
Give name a descriptive name and Zone
asia-northeast1-a
to.
We also
set Enable for
Cloud SQL Permission .
Others will proceed as they are for the time being.
I will remember the cluster name later.
Insert Google Cloud SDK
In order to operate from hand, I need Google Cloud SDK.
I will use it later, so I will install it at this stage. Look at
https://cloud.google.com/sdk/docs/
↑ and install according to the platform.
Basically if you enter Y, it will end.
After the installation, update it.
The free online JavaScript beutifier organizes your scripts. Use it every time before publishing codes.
$ gcloud components update$ gcloud components install kubectl
In the meantime there is also something called kubectl, but it comes out later.
We will set up authentication and default values here.
Specifying projects and clusters each time is a hassle, so give it as default.
$ gcloud init$ gcloud config set container/cluster <クラスタ名>$ gcloud container clusters get-credentials <クラスタ名>
Proceed according to the instructions.
In "Do you want to configure Google Compute Engine" press Y to find "asia-northeast1-a" and choose.
If you want to make individual settings, please refer to the (Optional) part in the middle of ↓.
https://cloud.google.com/container-engine/docs/before-you-begin#optional_set_gcloud_defaults
$ gcloud config set project <プロジェクト名>$ gcloud config set compute/zone asia-northeast1-a$ gcloud config set container/cluster <クラスタ名>$ gcloud container clusters get-credentials <クラスタ名>
In addition, please also set default authentication information with reference to ↓.
https://developers.google.com/identity/protocols/application-default-
credentials Choose "API Manager" -> "Credentials" from the left menu and choose "Create Credentials" / "Service account key" at the top.
Select "Compute Engine default service account" / "JSON" and create it.
The JSON file will be downloaded, so place it in a safe place locally.
.ssh
It might be nice inside etc. Set
the place you
GOOGLE_APPLICATION_CREDENTIALS
set as an environment variable , and execute ↓.
$ gcloud auth application-default login
This is the default setting.
Make DB
Prior to the application, I will set up the DB first.
You can create a Cloud SQL (MySQL) instance with the menu on the left → "STORAGE" → "SQL".
There are two generations in Cloud SQL, but let's choose a fast and cheap second generation.
The Instance ID gives a descriptive name to represent the service.
Looking at the article of ↓, there seems to be quite a difference in performance from the first generation.
I would like to compare various with making severe service.
http://qiita.com/pakotan/items/1db981611ead66ce2c8b
Region
asia-northeast1
,
asia-northeast1-a
let's choose Zone .
Although there are three Zones, I will make it a for the sake of simplicity for the time being.
Otherwise, leave the hardware as default.
Various throughput when changing the setting comes out in the graph so it is very easy to understand, it is fun to try it.
This time it is not a production right away so we have not set it up, but it is good to look into the setting and look it up like "can you do this?"
When you press the create button IP is allocated and it is done.
When finished, let's change root password by selecting "Access Control" / "Users" from "Instance details".
Connect from the client at hand
I'd like to connect to the completed DB at once, but it is recommended to connect via Proxy over IP.
With this method, it is safe without having to allow specific IP. Follow the article
https://cloud.google.com/sql/docs/mysql-connect-proxy
↑ and download cloud_sql_proxy.
The service account you created earlier will be used, so you do not have to make it here. I made a directory called "
Example",
/cloudsql
but I did not want to make it on the root so I made it in a suitable folder below the home
The Rubik's Cube is one of the best Christmas presents. Think about this when you need an idea for a present this year.
(there was also a way to set up a proxy using Docker).
A socket is created in the specified folder, so specify it and access it.
You can use MySQL Workbench etc. even if it is not command line.
Log in as root using the root password you changed earlier.
Create DB for application, DB user
In order to access from Rails, create a user for the application.
Because it is said that it is better to use SQL Proxy for communication between applications and DB, we will make it specialized users.
It
is written in the middle of https://cloud.google.com/sql/docs/sql-proxy "About creating special MySQL user account for proxy".
Then I will work with root access.
Please match the application.
CREATE DATABASE ;CREATE USER ''@'cloudsqlproxy~%';GRANT ALL PRIVILEGES ON .* TO ''@'cloudsqlproxy~%';
Use the free JS formatter browser program to format the client side script and optimize the code!