This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.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 复制链接链接已复制到粘贴板!
-
odois installed. -
occlient 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
Log in to an OpenShift Container Platform cluster:
odo login -u developer -p developer
$ odo login -u developer -p developerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a project:
odo project create myproject
$ odo project create myprojectCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
✓ Project 'myproject' is ready for use ✓ New project created and now using project : myproject
✓ Project 'myproject' is ready for use ✓ New project created and now using project : myprojectCopy to Clipboard Copied! Toggle word wrap Toggle overflow
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
Download the example front-end application:
git clone https://github.com/openshift/nodejs-ex frontend
$ git clone https://github.com/openshift/nodejs-ex frontendCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change the current directory to the front-end directory:
cd frontend
$ cd frontendCopy to Clipboard Copied! Toggle word wrap Toggle overflow List the contents of the directory to see that the front end is a Node.js application.
ls
$ lsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
README.md openshift server.js views helm package.json tests
README.md openshift server.js views helm package.json testsCopy to Clipboard Copied! Toggle word wrap Toggle overflow 注意The front-end component is written in an interpreted language (Node.js); it does not need to be built.
Create a component configuration of Node.js component-type named
frontend:odo create nodejs frontend
$ odo create nodejs frontendCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
✓ Validating component [5ms] Please use `odo push` command to create the component with source deployed
✓ Validating component [5ms] Please use `odo push` command to create the component with source deployedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a URL to access the frontend interface.
odo url create myurl
$ odo url create myurlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
✓ URL myurl created for component: nodejs-nodejs-ex-pmdp
✓ URL myurl created for component: nodejs-nodejs-ex-pmdpCopy to Clipboard Copied! Toggle word wrap Toggle overflow Push the component to the OpenShift Container Platform cluster.
odo push
$ odo pushCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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
$ odo service createCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Your password or username will be passed to the front-end application as environment variables.
2.7.5. Deploying a database manually 复制链接链接已复制到粘贴板!
List the available services:
odo catalog list services
$ odo catalog list servicesCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Choose the
mongodb-persistenttype of service and see the required parameters:odo catalog describe service mongodb-persistent
$ odo catalog describe service mongodb-persistentCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
$ odo service create mongodb-persistent --plan default --wait -p DATABASE_SERVICE_NAME=mongodb -p MEMORY_LIMIT=512Mi -p MONGODB_DATABASE=sampledb -p VOLUME_CAPACITY=1GiCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Link the database to the front-end service:
odo link mongodb-persistent
$ odo link mongodb-persistentCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow See the environment variables of the application and the database in the pod:
Get the pod name:
oc get pods
$ oc get podsCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
NAME READY STATUS RESTARTS AGE mongodb-1-gsznc 1/1 Running 0 28m nodejs-nodejs-ex-mhbb-app-4-vkn9l 1/1 Running 0 1mCopy to Clipboard Copied! Toggle word wrap Toggle overflow Connect to the pod:
oc rsh nodejs-nodejs-ex-mhbb-app-4-vkn9l
$ oc rsh nodejs-nodejs-ex-mhbb-app-4-vkn9lCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check the environment variables:
env
sh-4.2$ envCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
uri=mongodb://172.30.126.3:27017 password=dHIOpYneSkX3rTLn database_name=sampledb username=user43U admin_password=NCn41tqmx7RIqmfv
uri=mongodb://172.30.126.3:27017 password=dHIOpYneSkX3rTLn database_name=sampledb username=user43U admin_password=NCn41tqmx7RIqmfvCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Open the URL in the browser and notice the database configuration in the bottom right:
odo url list
$ odo url listCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.7.7. Deleting an application 复制链接链接已复制到粘贴板!
Deleting an application will delete all components associated with the application.
Procedure
List the applications in the current project:
odo app list
$ odo app listCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
The project '<project_name>' has the following applications: NAME appThe project '<project_name>' has the following applications: NAME appCopy to Clipboard Copied! Toggle word wrap Toggle overflow List the components associated with the applications. These components will be deleted with the application:
odo component list
$ odo component listCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
APP NAME TYPE SOURCE STATE app nodejs-nodejs-ex-elyf nodejs file://./ PushedAPP NAME TYPE SOURCE STATE app nodejs-nodejs-ex-elyf nodejs file://./ PushedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the application:
odo app delete <application_name>
$ odo app delete <application_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
? Are you sure you want to delete the application: <application_name> from project: <project_name>
? Are you sure you want to delete the application: <application_name> from project: <project_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Confirm the deletion with
Y. You can suppress the confirmation prompt using the-fflag.