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
Log in to an OKD cluster:
Create a project:
$ odo project create myproject
Example output
✓ Project 'myproject' is ready for use
✓ 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
Download the example front-end application:
$ git clone https://github.com/openshift/nodejs-ex frontend
-
$ cd frontend
List the contents of the directory to see that the front end is a Node.js application.
$ ls
Example output
README.md openshift server.js views
helm package.json tests
Create a component configuration of Node.js component-type named
frontend
:$ odo create --s2i nodejs frontend
Example output
✓ Validating component [5ms]
Please use `odo push` command to create the component with source deployed
Create a URL to access the frontend interface.
Example output
✓ URL myurl created for component: nodejs-nodejs-ex-pmdp
Push the component to the OKD cluster.
$ odo push
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]
✓ Syncing files to the component [408ms]
✓ Building component [7s]
✓ Changes successfully pushed to component
odo provides a command-line interactive mode which simplifies deployment.
Procedure
Run the interactive mode and answer the prompts:
$ odo service create
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'
-
$ odo catalog list services
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
rails-pgsql-persistent default
Choose the
mongodb-persistent
type of service and see the required parameters:$ odo catalog describe service mongodb-persistent
Example output
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
Link the database to the front-end service:
$ odo link mongodb-persistent
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
See the environment variables of the application and the database in the pod:
Get the pod name:
$ oc get pods
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
Connect to the pod:
$ oc rsh nodejs-nodejs-ex-mhbb-app-4-vkn9l
Check the environment variables:
sh-4.2$ env
Example output
uri=mongodb://172.30.126.3:27017
password=dHIOpYneSkX3rTLn
database_name=sampledb
username=user43U
admin_password=NCn41tqmx7RIqmfv
Open the URL in the browser and notice the database configuration in the bottom right:
Example output
Request information
Page view count: 24
DB Connection Info: