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.6. Creating a multicomponent application with odo
odo
allows you to create a multicomponent application, modify it, and link its components in an easy and automated way.
This example describes how to deploy a multicomponent application - a shooter game. The application consists of a front-end Node.js component and a back-end Java component.
2.6.1. Prerequisites 复制链接链接已复制到粘贴板!
-
odo
is installed. - You have a running cluster. Developers can use CodeReady Containers (CRC) to deploy a local cluster quickly.
- Maven is installed.
2.6.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 developer
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a project:
odo project create myproject
$ odo project create myproject
Copy 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 : myproject
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.6.3. Deploying the back-end component 复制链接链接已复制到粘贴板!
To create a Java component, import the Java builder image, download the Java application and push the source code to your cluster with odo
.
Procedure
Import
openjdk18
into the cluster:oc import-image openjdk18 \ --from=registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift --confirm
$ oc import-image openjdk18 \ --from=registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift --confirm
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Tag the image as
builder
to make it accessible for odo:oc annotate istag/openjdk18:latest tags=builder
$ oc annotate istag/openjdk18:latest tags=builder
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run
odo catalog list components
to see the created image:odo catalog list components
$ odo catalog list components
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a directory for your components:
mkdir my_components && cd my_components
$ mkdir my_components && cd my_components
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Download the example back-end application:
git clone https://github.com/openshift-evangelists/Wild-West-Backend backend
$ git clone https://github.com/openshift-evangelists/Wild-West-Backend backend
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change to the back-end source directory:
cd backend
$ cd backend
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check that you have the correct files in the directory:
ls
$ ls
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
debug.sh pom.xml src
debug.sh pom.xml src
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build the back-end source files with Maven to create a JAR file:
mvn package
$ mvn package
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a component configuration of Java component-type named
backend
:odo create openjdk18 backend --binary target/wildwest-1.0.jar
$ odo create openjdk18 backend --binary target/wildwest-1.0.jar
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
✓ Validating component [1ms] Please use `odo push` command to create the component with source deployed
✓ Validating component [1ms] Please use `odo push` command to create the component with source deployed
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Now the configuration file
config.yaml
is in the local directory of the back-end component that contains information about the component for deployment.Check the configuration settings of the back-end component in the
config.yaml
file using:odo config view
$ odo config view
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Push the component to the OpenShift Container Platform cluster.
odo push
$ odo push
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using
odo push
, OpenShift Container Platform creates a container to host the back-end component, deploys the container into a pod running on the OpenShift Container Platform cluster, and starts thebackend
component.Validate:
The status of the action in odo:
odo log -f
$ odo log -f
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
2019-09-30 20:14:19.738 INFO 444 --- [ main] c.o.wildwest.WildWestApplication : Starting WildWestApplication v1.0 onbackend-app-1-9tnhc with PID 444 (/deployments/wildwest-1.0.jar started by jboss in /deployments)
2019-09-30 20:14:19.738 INFO 444 --- [ main] c.o.wildwest.WildWestApplication : Starting WildWestApplication v1.0 onbackend-app-1-9tnhc with PID 444 (/deployments/wildwest-1.0.jar started by jboss in /deployments)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The status of the back-end component:
odo list
$ odo list
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
APP NAME TYPE SOURCE STATE app backend openjdk18 file://target/wildwest-1.0.jar Pushed
APP NAME TYPE SOURCE STATE app backend openjdk18 file://target/wildwest-1.0.jar Pushed
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.6.4. 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 frontend
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change the current directory to the front-end directory:
cd frontend
$ cd frontend
Copy 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
$ ls
Copy 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 tests
Copy 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 frontend
Copy 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 deployed
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Push the component to a running container.
odo push
$ odo push
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.6.5. Linking both components 复制链接链接已复制到粘贴板!
Components running on the cluster need to be connected in order to interact. OpenShift Container Platform provides linking mechanisms to publish communication bindings from a program to its clients.
Procedure
List all the components that are running on the cluster:
odo list
$ odo list
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
OpenShift Components: APP NAME PROJECT TYPE SOURCETYPE STATE app backend testpro openjdk18 binary Pushed app frontend testpro nodejs local Pushed
OpenShift Components: APP NAME PROJECT TYPE SOURCETYPE STATE app backend testpro openjdk18 binary Pushed app frontend testpro nodejs local Pushed
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Link the current front-end component to the back end:
odo link backend --port 8080
$ odo link backend --port 8080
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
✓ Component backend has been successfully linked from the component frontend Following environment variables were added to frontend component: - COMPONENT_BACKEND_HOST - COMPONENT_BACKEND_PORT
✓ Component backend has been successfully linked from the component frontend Following environment variables were added to frontend component: - COMPONENT_BACKEND_HOST - COMPONENT_BACKEND_PORT
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The configuration information of the back-end component is added to the front-end component and the front-end component restarts.
2.6.6. Exposing components to the public 复制链接链接已复制到粘贴板!
Procedure
Navigate to the
frontend
directory:cd frontend
$ cd frontend
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create an external URL for the application:
odo url create frontend --port 8080
$ odo url create frontend --port 8080
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
✓ URL frontend created for component: frontend To create URL on the OpenShift cluster, use `odo push`
✓ URL frontend created for component: frontend To create URL on the OpenShift cluster, use `odo push`
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the changes:
odo push
$ odo push
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Open the URL in a browser to view the application.
If an application requires permissions to the active service account to access the OpenShift Container Platform namespace and delete active pods, the following error may occur when looking at odo log
from the back-end component:
Message: Forbidden!Configured service account doesn’t have access. Service account may have been revoked
To resolve this error, add permissions for the service account role:
oc policy add-role-to-group view system:serviceaccounts -n <project>
$ oc policy add-role-to-group view system:serviceaccounts -n <project>
oc policy add-role-to-group edit system:serviceaccounts -n <project>
$ oc policy add-role-to-group edit system:serviceaccounts -n <project>
Do not do this on a production cluster.
2.6.7. Modifying the running application 复制链接链接已复制到粘贴板!
Procedure
Change the local directory to the front-end directory:
cd frontend
$ cd frontend
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Monitor the changes on the file system using:
odo watch
$ odo watch
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
index.html
file to change the displayed name for the game.注意A slight delay is possible before odo recognizes the change.
odo pushes the changes to the front-end component and prints its status to the terminal:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Refresh the application page in the web browser. The new name is now displayed.
2.6.8. 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 list
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
The project '<project_name>' has the following applications: NAME app
The project '<project_name>' has the following applications: NAME app
Copy 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 list
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
APP NAME TYPE SOURCE STATE app nodejs-nodejs-ex-elyf nodejs file://./ Pushed
APP NAME TYPE SOURCE STATE app nodejs-nodejs-ex-elyf nodejs file://./ Pushed
Copy 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-f
flag.