This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.11.4. Deploying your application to OpenShift Container Platform
You can deploy you application to OpenShift Container Platform.
After creating the rails-app
project, you are automatically switched to the new project namespace.
Deploying your application in OpenShift Container Platform involves three steps:
- Creating a database service from OpenShift Container Platform’s PostgreSQL image.
- Creating a frontend service from OpenShift Container Platform’s Ruby 2.0 builder image and your Ruby on Rails source code, which are wired with the database service.
- Creating a route for your application.
Procedure
To deploy your Ruby on Rails application, create a new project for the application:
oc new-project rails-app --description="My Rails application" --display-name="Rails Application"
$ oc new-project rails-app --description="My Rails application" --display-name="Rails Application"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.4.1. Creating the database service 复制链接链接已复制到粘贴板!
Your Rails application expects a running database service. For this service use PostgeSQL database image.
To create the database service, use the oc new-app
command. To this command you must pass some necessary environment variables which are used inside the database container. These environment variables are required to set the username, password, and name of the database. You can change the values of these environment variables to anything you would like. The variables are as follows:
- POSTGRESQL_DATABASE
- POSTGRESQL_USER
- POSTGRESQL_PASSWORD
Setting these variables ensures:
- 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:
oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password
$ oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To also set the password for the database administrator, append to the previous command with:
-e POSTGRESQL_ADMIN_PASSWORD=admin_pw
-e POSTGRESQL_ADMIN_PASSWORD=admin_pw
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Watch the progress:
oc get pods --watch
$ oc get pods --watch
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.4.2. Creating the frontend service 复制链接链接已复制到粘贴板!
To bring your application to OpenShift Container Platform, you must specify a repository in which your application lives.
Procedure
Create the frontend service and specify database related environment variables that were setup when creating the database service:
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=postgresql
$ 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=postgresql
Copy to Clipboard Copied! Toggle word wrap Toggle overflow With 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 the environment variables have been added by viewing the JSON document of the
rails-app
deployment config:oc get dc rails-app -o json
$ oc get dc rails-app -o json
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You should see the following section:
- Example output
Check the build process:
oc logs -f build/rails-app-1
$ oc logs -f build/rails-app-1
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Once the build is complete, look at the running pods in OpenShift Container Platform:
oc get pods
$ oc get pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You should see a line starting with
myapp-<number>-<hash>
, and that is your application 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:
Exec into frontend container with
rsh
command:oc rsh <frontend_pod_id>
$ oc rsh <frontend_pod_id>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the migration from inside the container:
RAILS_ENV=production bundle exec rake db:migrate
$ RAILS_ENV=production bundle exec rake db:migrate
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you are running your Rails application in a
development
ortest
environment you do not have to specify theRAILS_ENV
environment variable.
- By adding pre-deployment lifecycle hooks in your template.
11.4.3. Creating a route for your application 复制链接链接已复制到粘贴板!
You can expose a service to create a route for your application.
Procedure
To expose a service by giving it an externally-reachable hostname like
www.example.com
use OpenShift Container Platform route. In your case you need to expose the frontend service by typing:oc expose service rails-app --hostname=www.example.com
$ oc expose service rails-app --hostname=www.example.com
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Ensure the hostname you specify resolves into the IP address of the router.