Creating an application with a database

    Prerequisites

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

    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:

    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

    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
      1. $ cd frontend
    2. 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
    3. 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
    4. Create a URL to access the frontend interface.

      Example output

      1. URL myurl created for component: nodejs-nodejs-ex-pmdp
    5. 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. Syncing files to the component [408ms]
      11. Building component [7s]
      12. Changes successfully pushed to component

    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'
      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. rails-pgsql-persistent default
    1. Choose the mongodb-persistent type of service and see the required parameters:

      1. $ odo catalog describe service mongodb-persistent

      Example output

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

      Example output

      1. Request information
      2. Page view count: 24
      3. DB Connection Info: