2.7. Creating an application with a database


This example describes how to deploy and connect a database to a front-end application.

2.7.1. Prerequisites

  • odo is installed.
  • oc client is installed.
  • You have a running cluster. Developers can use CodeReady Containers (CRC) to deploy a local cluster quickly.
  • The Service Catalog is installed and enabled on your cluster.

    注意

    Service Catalog is deprecated on OpenShift Container Platform 4 and later.

2.7.2. Creating a project

Create a project to keep your source code, tests, and libraries organized in a separate single unit.

Procedure

  1. Log in to an OpenShift Container Platform cluster:

    $ odo login -u developer -p developer
    Copy to Clipboard Toggle word wrap
  2. Create a project:

    $ odo project create myproject
    Copy to Clipboard Toggle word wrap

    Example output

     ✓  Project 'myproject' is ready for use
     ✓  New project created and now using project : myproject
    Copy to Clipboard Toggle word wrap

2.7.3. Deploying the front-end component

To create and deploy a front-end component, download the Node.js application and push the source code to your cluster with odo.

Procedure

  1. Download the example front-end application:

    $ git clone https://github.com/openshift/nodejs-ex frontend
    Copy to Clipboard Toggle word wrap
  2. Change the current directory to the front-end directory:

    $ cd frontend
    Copy to Clipboard Toggle word wrap
  3. List the contents of the directory to see that the front end is a Node.js application.

    $ ls
    Copy to Clipboard Toggle word wrap

    Example output

    README.md       openshift       server.js       views
    helm            package.json    tests
    Copy to Clipboard Toggle word wrap

    注意

    The front-end component is written in an interpreted language (Node.js); it does not need to be built.

  4. Create a component configuration of Node.js component-type named frontend:

    $ odo create nodejs frontend
    Copy to Clipboard Toggle word wrap

    Example output

     ✓  Validating component [5ms]
    Please use `odo push` command to create the component with source deployed
    Copy to Clipboard Toggle word wrap

  5. Create a URL to access the frontend interface.

    $ odo url create myurl
    Copy to Clipboard Toggle word wrap

    Example output

     ✓  URL myurl created for component: nodejs-nodejs-ex-pmdp
    Copy to Clipboard Toggle word wrap

  6. Push the component to the OpenShift Container Platform cluster.

    $ odo push
    Copy to Clipboard Toggle word wrap

    Example output

    Validation
     ✓  Checking component [7ms]
    
     Configuration changes
     ✓  Initializing component
     ✓  Creating component [134ms]
    
     Applying URL changes
     ✓  URL myurl: http://myurl-app-myproject.192.168.42.79.nip.io created
    
     Pushing to component nodejs-nodejs-ex-mhbb of type local
     ✓  Checking files for pushing [657850ns]
     ✓  Waiting for component to start [6s]
     ✓  Syncing files to the component [408ms]
     ✓  Building component [7s]
     ✓  Changes successfully pushed to component
    Copy to Clipboard Toggle word wrap

2.7.4. Deploying a database in interactive mode

odo provides a command-line interactive mode which simplifies deployment.

Procedure

  • Run the interactive mode and answer the prompts:

    $ odo service create
    Copy to Clipboard Toggle word wrap

    Example output

    ? Which kind of service do you wish to create database
    ? Which database service class should we use mongodb-persistent
    ? Enter a value for string property DATABASE_SERVICE_NAME (Database Service Name): mongodb
    ? Enter a value for string property MEMORY_LIMIT (Memory Limit): 512Mi
    ? Enter a value for string property MONGODB_DATABASE (MongoDB Database Name): sampledb
    ? Enter a value for string property MONGODB_VERSION (Version of MongoDB Image): 3.2
    ? Enter a value for string property VOLUME_CAPACITY (Volume Capacity): 1Gi
    ? Provide values for non-required properties No
    ? How should we name your service  mongodb-persistent
    ? Output the non-interactive version of the selected options No
    ? Wait for the service to be ready No
     ✓  Creating service [32ms]
     ✓  Service 'mongodb-persistent' was created
    Progress of the provisioning will not be reported and might take a long time.
    You can see the current status by executing 'odo service list'
    Copy to Clipboard Toggle word wrap

注意

Your password or username will be passed to the front-end application as environment variables.

2.7.5. Deploying a database manually

  1. List the available services:

    $ odo catalog list services
    Copy to Clipboard Toggle word wrap

    Example output

    NAME                         PLANS
    django-psql-persistent       default
    jenkins-ephemeral            default
    jenkins-pipeline-example     default
    mariadb-persistent           default
    mongodb-persistent           default
    mysql-persistent             default
    nodejs-mongo-persistent      default
    postgresql-persistent        default
    rails-pgsql-persistent       default
    Copy to Clipboard Toggle word wrap

  2. Choose the mongodb-persistent type of service and see the required parameters:

    $ odo catalog describe service mongodb-persistent
    Copy to Clipboard Toggle word wrap

    Example output

      ***********************        | *****************************************************
      Name                           | default
      -----------------              | -----------------
      Display Name                   |
      -----------------              | -----------------
      Short Description              | Default plan
      -----------------              | -----------------
      Required Params without a      |
      default value                  |
      -----------------              | -----------------
      Required Params with a default | DATABASE_SERVICE_NAME
      value                          | (default: 'mongodb'),
                                     | MEMORY_LIMIT (default:
                                     | '512Mi'), MONGODB_VERSION
                                     | (default: '3.2'),
                                     | MONGODB_DATABASE (default:
                                     | 'sampledb'), VOLUME_CAPACITY
                                     | (default: '1Gi')
      -----------------              | -----------------
      Optional Params                | MONGODB_ADMIN_PASSWORD,
                                     | NAMESPACE, MONGODB_PASSWORD,
                                     | MONGODB_USER
    Copy to Clipboard Toggle word wrap

  3. Pass the required parameters as flags and wait for the deployment of the database:

    $ odo service create mongodb-persistent --plan default --wait -p DATABASE_SERVICE_NAME=mongodb -p MEMORY_LIMIT=512Mi -p MONGODB_DATABASE=sampledb -p VOLUME_CAPACITY=1Gi
    Copy to Clipboard Toggle word wrap

2.7.6. Connecting the database to the front-end application

  1. Link the database to the front-end service:

    $ odo link mongodb-persistent
    Copy to Clipboard Toggle word wrap

    Example output

     ✓  Service mongodb-persistent has been successfully linked from the component nodejs-nodejs-ex-mhbb
    
    Following environment variables were added to nodejs-nodejs-ex-mhbb component:
    - database_name
    - password
    - uri
    - username
    - admin_password
    Copy to Clipboard Toggle word wrap

  2. See the environment variables of the application and the database in the pod:

    1. Get the pod name:

      $ oc get pods
      Copy to Clipboard Toggle word wrap

      Example output

      NAME                                READY     STATUS    RESTARTS   AGE
      mongodb-1-gsznc                     1/1       Running   0          28m
      nodejs-nodejs-ex-mhbb-app-4-vkn9l   1/1       Running   0          1m
      Copy to Clipboard Toggle word wrap

    2. Connect to the pod:

      $ oc rsh nodejs-nodejs-ex-mhbb-app-4-vkn9l
      Copy to Clipboard Toggle word wrap
    3. Check the environment variables:

      sh-4.2$ env
      Copy to Clipboard Toggle word wrap

      Example output

      uri=mongodb://172.30.126.3:27017
      password=dHIOpYneSkX3rTLn
      database_name=sampledb
      username=user43U
      admin_password=NCn41tqmx7RIqmfv
      Copy to Clipboard Toggle word wrap

  3. Open the URL in the browser and notice the database configuration in the bottom right:

    $ odo url list
    Copy to Clipboard Toggle word wrap

    Example output

    Request information
    Page view count: 24
    
    DB Connection Info:
    Type:	MongoDB
    URL:	mongodb://172.30.126.3:27017/sampledb
    Copy to Clipboard Toggle word wrap

2.7.7. Deleting an application

重要

Deleting an application will delete all components associated with the application.

Procedure

  1. List the applications in the current project:

    $ odo app list
    Copy to Clipboard Toggle word wrap

    Example output

        The project '<project_name>' has the following applications:
        NAME
        app
    Copy to Clipboard Toggle word wrap

  2. List the components associated with the applications. These components will be deleted with the application:

    $ odo component list
    Copy to Clipboard Toggle word wrap

    Example output

        APP     NAME                      TYPE       SOURCE        STATE
        app     nodejs-nodejs-ex-elyf     nodejs     file://./     Pushed
    Copy to Clipboard Toggle word wrap

  3. Delete the application:

    $ odo app delete <application_name>
    Copy to Clipboard Toggle word wrap

    Example output

        ? Are you sure you want to delete the application: <application_name> from project: <project_name>
    Copy to Clipboard Toggle word wrap

  4. Confirm the deletion with Y. You can suppress the confirmation prompt using the -f flag.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat