Creating an application with a database

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

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 OKD 4 and later.

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 OKD cluster:

    1. $ odo login -u developer -p developer
  2. Create a project:

    1. $ odo project create myproject

    Example output

    1. Project 'myproject' is ready for use
    2. New project created and now using project : myproject

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:

    1. $ git clone https://github.com/openshift/nodejs-ex frontend
  2. Change the current directory to the front-end directory:

    1. $ cd frontend
  3. List the contents of the directory to see that the front end is a Node.js application.

    1. $ ls

    Example output

    1. README.md openshift server.js views
    2. helm package.json tests

    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:

    1. $ odo create --s2i nodejs frontend

    Example output

    1. Validating component [5ms]
    2. Please use `odo push` command to create the component with source deployed
  5. Create a URL to access the frontend interface.

    1. $ odo url create myurl

    Example output

    1. URL myurl created for component: nodejs-nodejs-ex-pmdp
  6. Push the component to the OKD cluster.

    1. $ odo push

    Example output

    1. Validation
    2. Checking component [7ms]
    3. Configuration changes
    4. Initializing component
    5. Creating component [134ms]
    6. Applying URL changes
    7. URL myurl: http://myurl-app-myproject.192.168.42.79.nip.io created
    8. Pushing to component nodejs-nodejs-ex-mhbb of type local
    9. Checking files for pushing [657850ns]
    10. Waiting for component to start [6s]
    11. Syncing files to the component [408ms]
    12. Building component [7s]
    13. Changes successfully pushed to component

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:

    1. $ odo service create

    Example output

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

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

Deploying a database manually

  1. List the available services:

    1. $ odo catalog list services

    Example output

    1. NAME PLANS
    2. django-psql-persistent default
    3. jenkins-ephemeral default
    4. jenkins-pipeline-example default
    5. mariadb-persistent default
    6. mongodb-persistent default
    7. mysql-persistent default
    8. nodejs-mongo-persistent default
    9. postgresql-persistent default
    10. rails-pgsql-persistent default
  2. Choose the mongodb-persistent type of service and see the required parameters:

    1. $ odo catalog describe service mongodb-persistent

    Example output

    1. *********************** | *****************************************************
    2. Name | default
    3. ----------------- | -----------------
    4. Display Name |
    5. ----------------- | -----------------
    6. Short Description | Default plan
    7. ----------------- | -----------------
    8. Required Params without a |
    9. default value |
    10. ----------------- | -----------------
    11. Required Params with a default | DATABASE_SERVICE_NAME
    12. value | (default: 'mongodb'),
    13. | MEMORY_LIMIT (default:
    14. | '512Mi'), MONGODB_VERSION
    15. | (default: '3.2'),
    16. | MONGODB_DATABASE (default:
    17. | 'sampledb'), VOLUME_CAPACITY
    18. | (default: '1Gi')
    19. ----------------- | -----------------
    20. Optional Params | MONGODB_ADMIN_PASSWORD,
    21. | NAMESPACE, MONGODB_PASSWORD,
    22. | MONGODB_USER
  3. Pass the required parameters as flags and wait for the deployment of the database:

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

Connecting the database to the front-end application

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

    1. $ odo link mongodb-persistent

    Example output

    1. Service mongodb-persistent has been successfully linked from the component nodejs-nodejs-ex-mhbb
    2. Following environment variables were added to nodejs-nodejs-ex-mhbb component:
    3. - database_name
    4. - password
    5. - uri
    6. - username
    7. - admin_password
  2. See the environment variables of the application and the database in the pod:

    1. Get the pod name:

      1. $ oc get pods

      Example output

      1. NAME READY STATUS RESTARTS AGE
      2. mongodb-1-gsznc 1/1 Running 0 28m
      3. nodejs-nodejs-ex-mhbb-app-4-vkn9l 1/1 Running 0 1m
    2. Connect to the pod:

      1. $ oc rsh nodejs-nodejs-ex-mhbb-app-4-vkn9l
    3. Check the environment variables:

      1. sh-4.2$ env

      Example output

      1. uri=mongodb://172.30.126.3:27017
      2. password=dHIOpYneSkX3rTLn
      3. database_name=sampledb
      4. username=user43U
      5. admin_password=NCn41tqmx7RIqmfv
  3. Open the URL in the browser and notice the database configuration in the bottom right:

    1. $ odo url list

    Example output

    1. Request information
    2. Page view count: 24
    3. DB Connection Info:
    4. Type: MongoDB
    5. URL: mongodb://172.30.126.3:27017/sampledb