Chapter 3. Get Started
3.1. Initial setup Copy linkLink copied to clipboard!
The instructions in this guide follow on from the OpenShift Primer, assuming a supported OpenShift configuration or a non-production OpenShift instance like that described in the OpenShift Primer.
The JWS for OpenShift application templates are distributed for Tomcat 9.
3.2. Configure Authentication to the Red Hat Container Registry Copy linkLink copied to clipboard!
Before you can import and use the Red Hat JBoss Web Server image, you must first configure authentication to the Red Hat Container Registry.
Red Hat recommends that you create an authentication token using a registry service account to configure access to the Red Hat Container Registry. This means that you don’t have to use or store your Red Hat account’s username and password in your OpenShift configuration.
- Follow the instructions on Red Hat Customer Portal to create an authentication token using a registry service account.
- Download the YAML file containing the OpenShift secret for the token. You can download the YAML file from the OpenShift Secret tab on your token’s Token Information page.
Create the authentication token secret for your OpenShift project using the YAML file that you downloaded:
oc create -f 1234567_myserviceaccount-secret.yaml
oc create -f 1234567_myserviceaccount-secret.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the secret for your OpenShift project using the following commands, replacing the secret name below with the name of your secret created in the previous step.
oc secrets link default 1234567-myserviceaccount-pull-secret --for=pull oc secrets link builder 1234567-myserviceaccount-pull-secret --for=pull
oc secrets link default 1234567-myserviceaccount-pull-secret --for=pull oc secrets link builder 1234567-myserviceaccount-pull-secret --for=pullCopy to Clipboard Copied! Toggle word wrap Toggle overflow
See the OpenShift documentation for more information on other methods for configuring access to secured registries.
See the Red Hat Customer Portal for more information on configuring authentication to the Red Hat Container Registry.
3.3. Import the Latest Red Hat JBoss Web Server Image Streams and Templates Copy linkLink copied to clipboard!
You must import the latest Red Hat JBoss Web Server for OpenShift image streams and templates for your JDK into the namespace of your OpenShift project.
Log in to the Red Hat Container Registry using your Customer Portal credentials to import the Red Hat JBoss Web Server image streams, templates and update image streams. For more information, see Red Hat Container Registry Authentication.
Import command for JDK 8 (RHEL7)
This command imports the following image streams and templates.
- The RHEL7 JDK 8 imagestream: jboss-webserver54-openjdk8-tomcat9-openshift-rhel7
- All templates specified in the command.
Import command for JDK 8 (RHEL8)
This command imports the following image streams and templates.
- The RHEL8 JDK 8 imagestream: jboss-webserver54-openjdk8-tomcat9-openshift-ubi8
- All templates specified in the command.
Import command for JDK 11 (RHEL7)
This command imports the following image streams and templates.
- The RHEL7 JDK 11 imagestream: jboss-webserver54-openjdk11-tomcat9-openshift-rhel7
- All templates specified in the command.
Import command for JDK 11 (RHEL8)
This command imports the following image streams and templates.
- The RHEL8 JDK 11 image stream: jboss-webserver54-openjdk11-tomcat9-openshift-ubi8
- All templates specified in the command.
3.3.1. Update Commands Copy linkLink copied to clipboard!
- In order to update the core JWS 5.4 tomcat 9 OpenJDK8 RHEL7 OpenShift image, you must execute
oc -n openshift import-image \ jboss-webserver54-openjdk8-tomcat9-openshift-rhel7:1.0
$ oc -n openshift import-image \
jboss-webserver54-openjdk8-tomcat9-openshift-rhel7:1.0
- In order to update the core JWS 5.4 tomcat 9 OpenJDK8 RHEL8 OpenShift, you must execute
oc -n openshift import-image \ jboss-webserver54-openjdk8-tomcat9-openshift-ubi8:1.0
$ oc -n openshift import-image \
jboss-webserver54-openjdk8-tomcat9-openshift-ubi8:1.0
- In order to update the core JWS 5.4 tomcat 9 OpenJDK11 RHEL7 OpenShift image, you must execute
oc -n openshift import-image \ jboss-webserver54-openjdk11-tomcat9-openshift-rhel7:1.0
$ oc -n openshift import-image \
jboss-webserver54-openjdk11-tomcat9-openshift-rhel7:1.0
- In order to update the core JWS 5.4 tomcat 9 OpenJDK11 RHEL8 OpenShift image, you must execute
oc -n openshift import-image \ jboss-webserver54-openjdk11-tomcat9-openshift-ubi8:1.0
$ oc -n openshift import-image \
jboss-webserver54-openjdk11-tomcat9-openshift-ubi8:1.0
The 1.0 tag at the end of each image you import refers to the stream version that is set in the image stream.
3.4. Using the JWS for OpenShift Source-to-Image (S2I) process Copy linkLink copied to clipboard!
To run and configure the JWS for OpenShift images, use the OpenShift S2I process with the application template parameters and environment variables.
The S2I process for the JWS for OpenShift images works as follows:
If there is a Maven settings.xml file in the
configuration/source directory, it is moved to$HOME/.m2/of the new image.See the Apache Maven Project website for more information on Maven and the Maven settings.xml file.
If there is a pom.xml file in the source repository, a Maven build is triggered using the contents of the
$MAVEN_ARGSenvironment variable.By default, the
packagegoal is used with theopenshiftprofile, including the arguments for skipping tests (-DskipTests) and enabling the Red Hat GA repository (-Dcom.redhat.xpaas.repo.redhatga).The results of a successful Maven build are copied to
/opt/webserver/webapps/. This includes all WAR files from the source directory specified by the$ARTIFACT_DIRenvironment variable. The default value of$ARTIFACT_DIRis thetarget/directory.Use the
MAVEN_ARGS_APPENDenvironment variable to modify the Maven arguments.-
All WAR files from the
deployments/source directory are copied to/opt/webserver/webapps/. -
All files in the
configuration/source directory are copied to/opt/webserver/conf/(excluding the Maven settings.xml file). All files in the
lib/source directory are copied to/opt/webserver/lib/.NoteIf you want to use custom Tomcat configuration files, the file names should be the same as for a normal Tomcat installation. For example, context.xml and server.xml.
See the Artifact Repository Mirrors section for guidance on configuring the S2I process to use a custom Maven artifacts repository mirror.
3.4.1. Create a JWS for OpenShift application using existing maven binaries Copy linkLink copied to clipboard!
Existing applications are deployed on OpenShift using the oc start-build command.
Prerequisite: An existing .war, .ear, or .jar of the application to deploy on JWS for OpenShift.
Prepare the directory structure on the local file system.
Create a source directory containing any content required by your application not included in the binary (if required, see Using the JWS for OpenShift Source-to-Image (S2I) process), then create a subdirectory
deployments/:mkdir -p <build_dir>/deployments
$ mkdir -p <build_dir>/deploymentsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the binaries (
.war,.ear,.jar) todeployments/:cp /path/to/binary/<filenames_with_extensions> <build_dir>/deployments/
$ cp /path/to/binary/<filenames_with_extensions> <build_dir>/deployments/Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteApplication archives in the
deployments/subdirectory of the source directory are copied to the$JWS_HOME/tomcat/webapps/directory of the image being built on OpenShift. For the application to deploy, the directory hierarchy containing the web application data must be structured correctly (see Section 3.4, “Using the JWS for OpenShift Source-to-Image (S2I) process”).Log in to the OpenShift instance:
oc login <url>
$ oc login <url>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new project if required:
oc new-project <project-name>
$ oc new-project <project-name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Identify the JWS for OpenShift image stream to use for your application with
oc get is -n openshift:oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshift
$ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshiftCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe option
-n openshiftspecifies the project to use.oc get is -n openshiftretrieves (get) the image stream resources (is) from theopenshiftproject.Create the new build configuration, specifying image stream and application name:
oc new-build --binary=true \ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel7:latest \ --name=<my-jws-on-openshift-app>
$ oc new-build --binary=true \ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel7:latest \ --name=<my-jws-on-openshift-app>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Instruct OpenShift to use the source directory created above for binary input of the OpenShift image build:
oc start-build <my-jws-on-openshift-app> --from-dir=./<build_dir> --follow
$ oc start-build <my-jws-on-openshift-app> --from-dir=./<build_dir> --followCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new OpenShift application based on the image:
oc new-app <my-jws-on-openshift-app>
$ oc new-app <my-jws-on-openshift-app>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Expose the service to make the application accessible to users:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Retrieve the address of the exposed route:
oc get routes --no-headers -o custom-columns='host:spec.host' my-jws-on-openshift-app
oc get routes --no-headers -o custom-columns='host:spec.host' my-jws-on-openshift-appCopy to Clipboard Copied! Toggle word wrap Toggle overflow - To access the application in your browser: http://<address_of_exposed_route> / <my-war-ear-jar-filename-without-extension>
3.4.2. Example: Creating a JWS for OpenShift application using existing maven binaries Copy linkLink copied to clipboard!
The example below uses the tomcat-websocket-chat quickstart using the procedure from Section 3.4.1, “Create a JWS for OpenShift application using existing maven binaries”.
3.4.2.1. Prerequisites: Copy linkLink copied to clipboard!
Get the WAR application archive or build the application locally.
Clone the source code:
git clone https://github.com/jboss-openshift/openshift-quickstarts.git
$ git clone https://github.com/jboss-openshift/openshift-quickstarts.gitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Build the application:
cd openshift-quickstarts/tomcat-websocket-chat/
$ cd openshift-quickstarts/tomcat-websocket-chat/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Prepare the directory structure on the local file system.
Create the source directory for the binary build on your local file system and the
deployments/subdirectory. Copy the WAR archive todeployments/:ls pom.xml README.md src/ target/
[tomcat-websocket-chat]$ ls pom.xml README.md src/ target/Copy to Clipboard Copied! Toggle word wrap Toggle overflow mkdir -p ocp/deployments
$ mkdir -p ocp/deploymentsCopy to Clipboard Copied! Toggle word wrap Toggle overflow cp target/websocket-chat.war ocp/deployments/
$ cp target/websocket-chat.war ocp/deployments/Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.4.2.2. To setup the example application on OpenShift Copy linkLink copied to clipboard!
Log in to the OpenShift instance:
oc login <url>
$ oc login <url>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new project if required:
oc new-project jws-bin-demo
$ oc new-project jws-bin-demoCopy to Clipboard Copied! Toggle word wrap Toggle overflow Identify the JWS for OpenShift image stream to use for your application with
oc get is -n openshift:oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshift
$ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshiftCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create new build configuration, specifying image stream and application name:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Start the binary build. Instruct OpenShift to use source directory for the binary input for the OpenShift image build:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new OpenShift application based on the image:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Expose the service to make the application accessible to users:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Retrieve the address of the exposed route:
oc get routes --no-headers -o custom-columns='host:spec.host' jws-wsch-app
oc get routes --no-headers -o custom-columns='host:spec.host' jws-wsch-appCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Access the application in your browser: http://<address_of_exposed_route>/websocket-chat
3.4.3. Create a JWS for OpenShift application from source code Copy linkLink copied to clipboard!
For detailed instructions on creating new OpenShift applications from source code, see OpenShift.com - Creating an application from source code.
Before proceeding, ensure that the applications' data is structured correctly (see Section 3.4, “Using the JWS for OpenShift Source-to-Image (S2I) process”).
Log in to the OpenShift instance:
oc login <url>
$ oc login <url>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new project if required:
oc new-project <project-name>
$ oc new-project <project-name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Identify the JWS for OpenShift image stream to use for your application with
oc get is -n openshift:oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshift
$ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshiftCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the new OpenShift application from source code using Red Hat JBoss Web Server for OpenShift images, use the
--image-streamoption:oc new-app \ <source_code_location>\ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel7\ --name=<openshift_application_name>
$ oc new-app \ <source_code_location>\ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel7\ --name=<openshift_application_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Example:
oc new-app \ https://github.com/jboss-openshift/openshift-quickstarts.git#master \ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel7\ --context-dir='tomcat-websocket-chat' \ --name=jws-wsch-app
$ oc new-app \ https://github.com/jboss-openshift/openshift-quickstarts.git#master \ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel7\ --context-dir='tomcat-websocket-chat' \ --name=jws-wsch-appCopy to Clipboard Copied! Toggle word wrap Toggle overflow The source code is added to the image and the source code is compiled. The build configuration and services are also created.
To expose the application:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To retrieve the address of the exposed route:
oc get routes --no-headers -o custom-columns='host:spec.host' <openshift_application_name>
oc get routes --no-headers -o custom-columns='host:spec.host' <openshift_application_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - To access the application in your browser: http://<address_of_exposed_route>/<java_application_name>
3.5. Adding additional jar files in tomcat/lib/ directory Copy linkLink copied to clipboard!
Additional jar files can be added to tomcat/lib/ directory using docker.
For adding jar files in tomcat/lib/
Get the image started in docker
docker run --network host -i -t -p 8080:8080 ImageURL
docker run --network host -i -t -p 8080:8080 ImageURLCopy to Clipboard Copied! Toggle word wrap Toggle overflow Find the
CONTAINER IDdocker ps | grep <ImageName>
docker ps | grep <ImageName>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the library to
tomcat/lib/directorydocker cp <yourLibrary> <CONTAINER ID>:/opt/jws-5.4/tomcat/lib/
docker cp <yourLibrary> <CONTAINER ID>:/opt/jws-5.4/tomcat/lib/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Commit the changes to a new image
docker commit <CONTAINER ID> <NEW IMAGE NAME>
docker commit <CONTAINER ID> <NEW IMAGE NAME>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new image tag
docker tag <NEW IMAGE NAME>:latest <NEW IMAGE REGISTRY URL>:<TAG>
docker tag <NEW IMAGE NAME>:latest <NEW IMAGE REGISTRY URL>:<TAG>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Push the image to a registry
docker push <NEW IMAGE REGISTRY URL>
docker push <NEW IMAGE REGISTRY URL>Copy to Clipboard Copied! Toggle word wrap Toggle overflow