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"

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

  1. Create the database service:

    $ oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password

    To also set the password for the database administrator, append to the previous command with:

    -e POSTGRESQL_ADMIN_PASSWORD=admin_pw
  2. Watch the progress:

    $ oc get pods --watch

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

  1. 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

    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.

  2. 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

    You should see the following section:

  3. Example output
env": [
    {
        "name": "POSTGRESQL_USER",
        "value": "username"
    },
    {
        "name": "POSTGRESQL_PASSWORD",
        "value": "password"
    },
    {
        "name": "POSTGRESQL_DATABASE",
        "value": "db_name"
    },
    {
        "name": "DATABASE_SERVICE_NAME",
        "value": "postgresql"
    }

],
  1. Check the build process:

    $ oc logs -f build/rails-app-1
  2. Once the build is complete, look at the running pods in OpenShift Container Platform:

    $ oc get pods

    You should see a line starting with myapp-<number>-<hash>, and that is your application running in OpenShift Container Platform.

  3. 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>
      • Run the migration from inside the container:

        $ RAILS_ENV=production bundle exec rake db:migrate

        If you are running your Rails application in a development or test environment you do not have to specify the RAILS_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
警告

Ensure the hostname you specify resolves into the IP address of the router.

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.