Creating applications using Ruby on Rails
You can build and deploy a Ruby on Rails 4 application on OpenShift Container Platform by developing it locally.
Store the source in Git, then deploy the database, frontend, and route services. With this process, you can validate your application locally before deploying it to the cluster as a set of distinct services.
You must complete each part of this tutorial in order to before you deploy your application on OpenShift Container Platform. If a step fails, confirm that every preceding step completed successfully before you continue.
Prerequisites
- You have basic Ruby on Rails knowledge.
- You have Ruby 2.0.0+, Rubygems, and Bundler installed locally.
- You have basic Git knowledge.
- You have a running instance of OpenShift Container Platform 4.
- The OpenShift CLI (
oc) installed. - You are logged into a running OpenShift Container Platform cluster.
Setting up the database
You can install PostgreSQL on your local system for Ruby on Rails development. This gives your application a local database to connect to during development and testing before you deploy to OpenShift Container Platform.
Procedure
- Install the database by running the following command:terminal
$ sudo yum install -y postgresql postgresql-server postgresql-devel - Initialize the database by running the following command:terminal
$ sudo postgresql-setup initdbThis command creates the
/var/lib/pgsql/datadirectory, in which the data is stored. - Start the database by running the following command:terminal
$ sudo systemctl start postgresql.service - When the database is running, create your
railsuser by running the following command:terminal$ sudo -u postgres createuser -s railsNoteThe user that is created has no password.
Writing your application
You can create a Ruby on Rails application that uses PostgreSQL. Install the Rails gem, configure the database.yml file, and initialize the development and test databases. These steps ensure that your application can interact with PostgreSQL in both development and test environments.
Procedure
- Install the Rails gem by running the following command:terminal
$ gem install railsExample outputSuccessfully installed rails-4.3.0 1 gem installed - Create a new application with PostgreSQL as your database by running the following command:terminal
$ rails new rails-app --database=postgresql - Change into your new application directory by running the following command:terminal
$ cd rails-app - If you already have an application, ensure that the PostgreSQL adapter gem (
pg) is present in yourGemfile. If not, edit yourGemfileby adding the gem:terminalgem 'pg' - Generate a new
Gemfile.lockwith all your dependencies by running the following command:terminal$ bundle install - Update the
defaultsection in theconfig/database.ymlfile to use thepostgresqladapter, as shown in the following example:yamldefault: &default adapter: postgresql encoding: unicode pool: 5 host: localhost username: rails password: <password> - Create the
developmentandtestdatabases for your application by running the following command:terminal$ rake db:create
Creating a welcome page
You can run the Rails generator to create a custom welcome page for your Rails application. A welcome page gives you content to display when you run the Rails server and open the application in your browser.
Procedure
- Run the Rails generator by running the following command:terminal
$ rails generate controller welcome indexThe command creates all the necessary files.
- Edit line 2 in the
config/routes.rbfile as follows:rubyroot 'welcome#index' - Run the Rails server to verify that the page is available by running the following command:terminal
$ rails serverVerify that the page is available by visiting
http://localhost:3000in your browser. If the page does not display, check the server logs for errors.
Configuring application for OpenShift Container Platform
To configure your Rails application for OpenShift Container Platform, you must edit the default section in the config/database.yml file. This is required for OpenShift Container Platform to supply the correct database credentials at runtime so your application can connect to PostgreSQL on the cluster.
Procedure
- Edit the
defaultsection in yourconfig/database.ymlwith pre-defined variables as follows:Sample config/database YAML file<% user = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? "root" : ENV["POSTGRESQL_USER"] %> <% password = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? ENV["POSTGRESQL_ADMIN_PASSWORD"] : ENV["POSTGRESQL_PASSWORD"] %> <% db_service = ENV.fetch("DATABASE_SERVICE_NAME","").upcase %> default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV["POSTGRESQL_MAX_CONNECTIONS"] || 5 %> username: <%= user %> password: <%= password %> host: <%= ENV["#{db_service}_SERVICE_HOST"] %> port: <%= ENV["#{db_service}_SERVICE_PORT"] %> database: <%= ENV["POSTGRESQL_DATABASE"] %>
Storing your application in Git
You can commit your Rails application to Git and push the source to a remote repository. Remote storage keeps your source available for deployment on OpenShift Container Platform.
Prerequisites
- You have installed Git.
Procedure
- Verify that you are in your Rails application directory by running the following command:terminal
$ ls -1Example outputapp bin config config.ru db Gemfile Gemfile.lock lib log public Rakefile README.rdoc test tmp vendor - Initialize a Git repository in your Rails application directory by running the following command:terminal
$ git init - Stage all application files by running the following command:terminal
$ git add . - Commit the staged files by running the following command:terminal
$ git commit -m "initial commit" - Create a GitHub repository for your application.
- Set the remote that points to your
gitrepository by running the following command:terminal$ git remote add origin git@github.com:<namespace/repository-name>.git - Push your application to your remote Git repository by running the following command:terminal
$ git push
Deploying your application to OpenShift Container Platform
You can create an OpenShift Container Platform project to deploy your Ruby on Rails application. This separates your database, frontend, and route into distinct services that OpenShift Container Platform can manage independently.
Deploying your application on OpenShift Container Platform takes three steps:
- Creating a database service from the PostgreSQL image on OpenShift Container Platform.
- Creating a frontend service from the Ruby 2.0 builder image on OpenShift Container Platform and your Ruby on Rails source code, connected to the database service.
- Creating a route for your application.
Procedure
- Create a project for your Rails application by running the following command:terminal
$ oc new-project rails-app --description="My Rails application" --display-name="Rails Application"
Creating the database service
You must create a database service for your Rails application. Be sure to set the environment variables for the database name, username, and password. These are required for the service to connect correctly to your Rails application.
You can change the values of these environment variables to any values you choose. The variables are as follows:
POSTGRESQL_DATABASEPOSTGRESQL_USERPOSTGRESQL_PASSWORD
Setting these variables ensures that the following occurs:
- A database exists with the specified name.
- A user exists with the specified name.
- The user can access the specified database with the specified password.
Procedure
- Create the database service by running the following command:terminal
$ oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=passwordNoteTo also set a database administrator password, add
-e POSTGRESQL_ADMIN_PASSWORD=admin_pwto the command. - Monitor the pod status by running the following command:terminal
$ oc get pods --watch
Creating the frontend service
You can create a frontend service with the oc new-app command. Specifying your source repository and database environment variables enables OpenShift Container Platform to build your application image and deploy it on the cluster.
Procedure
- Create the frontend service and specify the database-related environment variables that were set up when creating the database service by running the following command:terminal
$ oc new-app path/to/source/code --name=rails-app -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=db_name -e DATABASE_SERVICE_NAME=postgresqlWith this command, OpenShift Container Platform fetches the source code, sets up the builder, builds your application image, and deploys the newly created image together with the specified environment variables. The application is named
rails-app. - Verify that the environment variables have been added by viewing the JSON document of the
rails-appdeployment config by running the following command:terminal$ oc get dc rails-app -o jsonThe output includes the following section:
Example outputenv": [ { "name": "POSTGRESQL_USER", "value": "username" }, { "name": "POSTGRESQL_PASSWORD", "value": "password" }, { "name": "POSTGRESQL_DATABASE", "value": "db_name" }, { "name": "DATABASE_SERVICE_NAME", "value": "postgresql" } ], - Check the build process by running the following command:terminal
$ oc logs -f build/rails-app-1 - After the build is complete, check the running pods in OpenShift Container Platform by running the following command:terminal
$ oc get podsThe output includes a line starting with
myapp-<number>-<hash>, which confirms that the application is running in OpenShift Container Platform. - Before your application is functional, you must initialize the database by running the database migration script. There are two ways you can do this:
- Manually from the running frontend container:
- Open a remote shell to the frontend pod by running the following command:terminal
$ oc rsh <frontend_pod_id> - Run the migration from inside the container by running the following command:terminal
$ RAILS_ENV=production bundle exec rake db:migrateIf you are running your Rails application in a
developmentortestenvironment, you do not have to specify theRAILS_ENVenvironment variable.
- Open a remote shell to the frontend pod by running the following command:
- You can also run the migration by adding pre-deployment lifecycle hooks to your template.
- Manually from the running frontend container:
Creating a route for your application
You can create a route for your application with the oc expose service command. The route makes the application accessible from outside the cluster.
Procedure
- Make the frontend service accessible externally by running the following command:terminal
$ oc expose service rails-app --hostname=www.example.comWarningEnsure that the hostname you specify resolves to the IP address of the router.