OpenShift Build and Deploy a JEE Application with S2I: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=


* https://access.redhat.com/documentation/en-us/red_hat_jboss_middleware_for_openshift/3/html/red_hat_jboss_enterprise_application_platform_for_openshift/
* https://access.redhat.com/documentation/en/red-hat-xpaas/0/paged/red-hat-xpaas-eap-image
* https://blog.openshift.com/getting-java-ee-code-running-docker-containers-openshift/
* https://blog.openshift.com/improving-build-time-java-builds-openshift/
* https://blog.openshift.com/improving-build-time-java-builds-openshift/


=Internal=
=Internal=


* [[OpenShift_Concepts#Build|Builds]]
* [[OpenShift_Build_Operations#Build_and_Deploy_a_JEE_Application|Build Operations]]
* [[OpenShift_Build_Operations#Build_and_Deploy_a_JEE_Application|Build Operations]]
* [[OpenShift_Application_Operations#Typical_JEE_Application|Application Operations]]
* [[OpenShift_Application_Operations#Build_and_Deploy_a_JEE_Application|Application Operations]]
* [[OpenShift Build a JEE Application and Deploy it as a Container|Build a JEE Application and Deploy it as a Container in OpenShift]]


=Overview=
=Overview=
Line 30: Line 35:
It can be explored with:
It can be explored with:


  oc get -o yaml template eap70-basic-s2i -n open shift
  oc get -o yaml template eap70-basic-s2i -n openshift


The template can be used "as-is", or it can be downloaded locally and modified - for example, we can change the labels applied by the template to the objects it creates, so instead of "app=eap70-basic-s2i", we can use a more custom name for our application. Some of the parameters, including the application name, are allowed to be changed on command line. It is a good idea to review the application template and identify all parameters that can be customized.
The template can be used "as-is", or it can be downloaded locally and modified - for example, we can change the labels applied by the template to the objects it creates, so instead of "app=eap70-basic-s2i", we can use a more custom name for our application. Some of the parameters, including the application name, are allowed to be changed on command line. It is a good idea to review the application template and identify all parameters that can be customized.
=Accessing the Source Repository=
==IP Connectivity==
If the source of the application is maintained in a source repository such as Gogs, running in a different project, and we want to use the internal DNS name (and IP address) of the repository, we need to enable IP connectivity between our project and the project that maintains the source repository, so the source can be checked out. This is done by [[OpenShift_Network_Operations#Joining_Projects|joining the projects]].
[[oadm|oadm pod-network]] join-projects --to=cicd lab
Another way would be to expose the source repository publicly over a [[OpenShift_Concepts#Route|route]] and configure the build to use the  public address.
==Repository Credentials==
If the source repository is protected, we need to place credentials into the builder configuration, and the builder service account environment:
oc secrets new-basicauth src-repository-basicauth --username=gogs --password=...
oc secrets link builder src-repository-basicauth
oc set build-secret --source bc/session-servlet src-repository-basicauth


=Create the Application=
=Create the Application=
Line 55: Line 78:
   --param SOURCE_REPOSITORY_REF=master \
   --param SOURCE_REPOSITORY_REF=master \
   --param CONTEXT_DIR=/ \
   --param CONTEXT_DIR=/ \
   --param GITHUB_WEBHOOK_SECRET=n2h6dtHw7Gw3 \
   --param GITHUB_WEBHOOK_SECRET=n2H6dtHw6Gw3 \
   --param IMAGE_STREAM_NAMESPACE=openshift \
   --param IMAGE_STREAM_NAMESPACE=openshift
  --param MAVEN_MIRROR_URL=


Once executed, the "new-app" command creates the following:


* a "session-servlet" service
* a route that exposes the service as "http://session-servlet.apps.openshift.novaordis.io"
* a project-scoped image stream
* a build configuration
* a deployment configuration


=Build=


The build process is driven by a configuration created automatically, which is similar to [[OpenShift_Build_Configuration_Definition#servlet-example_Build_Configuration|servlet-example Build Configuration]].


The build process starts immediately and its logs can be monitored with:


oc logs -f build/session-servlet-1


If successful, the build results in the creation of a new deployable image that gets uploaded into the [[OpenShift_Concepts#Integrated_Docker_Registry|integrated docker registry]]:


<font color=gray>
oc get is


=Process=
NAME              DOCKER REPO                                                          TAGS      UPDATED
session-servlet  docker-registry.default.svc:5000/s2i-build-example/session-servlet  latest    5 minutes ago


=="Join" Projects==
[[Image:s2i-build-example_IntegratedDockerRegistry.png]]


If the source repository pod runs in a collocated OpenShift project, and we want to use the internal DNS name, we need to [[OpenShift_Network_Operations#Joining_Projects|join the projects]].
<font color=red>How do I build new images and how do I control the tag?</font>


[[oadm|oadm pod-network]] join-projects --to=cicd lab
More build operations:


{{Internal|OpenShift_Build_Operations#Manually_Start_the_Build_Process|Build Operations}}


[[oc new-app]] <''openshift-project-template-name''> \
=Deployment=
  --param APPLICATION_NAME=<''application-name''> \
  --param SOURCE_REPOSITORY_URL=<''git-url''> \
  --param SOURCE_REPOSITORY_REF=master \
  --param CONTEXT_DIR=/


[[oc new-app]] eap64-basic-s2i \
The deployment process begins immediately, triggered by an image change in the project's image stream, and results in the creation of a backing pod:
  --param APPLICATION_NAME=tasks \
  --param SOURCE_REPOSITORY_URL=http&#58;//gogs.cicd.svc.cluster.local:3000/gogs/openshift-tasks-private \
  --param SOURCE_REPOSITORY_REF=master \
  --param CONTEXT_DIR=/


This command uses openshift project's "eap64-basic-s2i" template and creates:
oc get pod
NAME                      READY    STATUS      RESTARTS  AGE
...
session-servlet-1-d8f2b  1/1      Running    0          14m


* a build configuration
The pod provides the backing functionality exposed internally by the service, and externally by the route.
* an image stream
* a deployment configuration
* a route
* a service


<pre>
=External Route=
--> Deploying template "openshift/eap64-basic-s2i" to project lab


    Red Hat JBoss EAP 6.4 (no https)
The route created by the new application template is incomplete, in that it does not specify the target port, and in that, it is not usable. It either has to be edited and fixed with [[oc edit]] or it can be dropped and recreated as shown below:
    ---------
    Application template for EAP 6 applications built using S2I.


    A new EAP 6 based application has been created in your project.
oc delete route session-servlet


    * With parameters:
oc expose service session-servlet --hostname=session-servlet.apps.openshift.novaordis.io --port=8080
        * Application Name=tasks
        * Custom http Route Hostname=
        * Git Repository URL=https://gogs.cicd.svc.cluster.local:3000/gogs/openshift-tasks-private
        * Git Reference=master
        * Context Directory=/
        * Queues=
        * Topics=
        * HornetQ Password=VvEf5H0S # generated
        * Github Webhook Secret=8Xk5LAnr # generated
        * Generic Webhook Secret=mn5K7NNA # generated
        * ImageStream Namespace=openshift
        * JGroups Cluster Password=r1Elwibf # generated
        * Deploy Exploded Archives=false
        * Maven mirror URL=
        * ARTIFACT_DIR=


--> Creating resources ...
More details about route operations are available here:
    service "tasks" created
    route "tasks" created
    imagestream "tasks" created
    buildconfig "tasks" created
    deploymentconfig "tasks" created
--> Success
    Build scheduled, use 'oc logs -f bc/tasks' to track its progress.
    Run 'oc status' to view your app.
</pre>


Because the Git repository is private and protected by username/password, the initial build attempt will fail:
{{Internal|OpenShift Route Operations#Overview|OpenShift Route Operations}}


oc logs -f build/tasks-1
...
error: build error: failed to fetch requested repository "http&#58;//gogs.cicd.svc.cluster.local:3000/gogs/openshift-tasks-private" with provided credentials


==Add Source Repository Secrets==


If the source repository is protected, we need to place credentials into the builder configuration, and the builder service account environment:
<font color=gray>
 
oc secrets new-basicauth gogs-basicauth --username=gogs --password=...
oc secrets link builder gogs-basicauth
oc set build-secret --source bc/tasks gogs-basicauth
 
==Restart the Build==
 
oc start-build tasks
 
 
</font>

Latest revision as of 23:06, 24 January 2018

External

Internal

Overview

This article describes the process of setting up a project that builds from sources and deploys a simple JEE application. The source of the application is exposed in GitHub. We will use https://github.com/NovaOrdis/novaordis-session-servlet (Session Servlet Example). The final artifact will be deployed in a EAP 7 instance.

Create the OpenShift Project

The application will be hosted in its own OpenShift project:

oc new-project s2i-build-example \
  --display-name='S2I Build Example' \
  --description='A JEE Application that builds from source and deploys onto a EAP 7 instance'

Identify the Application Template

Since we intend to deploy our JEE application in top of a JEE container, the simplest method is to identify the application template that matches our requirements and use it. OpenShift comes with a series of application templates, which are pre-loaded in the "openshift" project:

oc get templates -n openshift

The application template that matches our requirements is "eap70-basic-s2i"

It can be explored with:

oc get -o yaml template eap70-basic-s2i -n openshift

The template can be used "as-is", or it can be downloaded locally and modified - for example, we can change the labels applied by the template to the objects it creates, so instead of "app=eap70-basic-s2i", we can use a more custom name for our application. Some of the parameters, including the application name, are allowed to be changed on command line. It is a good idea to review the application template and identify all parameters that can be customized.

Accessing the Source Repository

IP Connectivity

If the source of the application is maintained in a source repository such as Gogs, running in a different project, and we want to use the internal DNS name (and IP address) of the repository, we need to enable IP connectivity between our project and the project that maintains the source repository, so the source can be checked out. This is done by joining the projects.

oadm pod-network join-projects --to=cicd lab

Another way would be to expose the source repository publicly over a route and configure the build to use the public address.

Repository Credentials

If the source repository is protected, we need to place credentials into the builder configuration, and the builder service account environment:

oc secrets new-basicauth src-repository-basicauth --username=gogs --password=...
oc secrets link builder src-repository-basicauth
oc set build-secret --source bc/session-servlet src-repository-basicauth

Create the Application

oc new-app eap70-basic-s2i \
  --param APPLICATION_NAME=<application-name> \
  --param HOSTNAME_HTTP=<custom-hostname-instead-of-application_name.project.default_routing_subdomain> \
  --param SOURCE_REPOSITORY_URL=<git-repository-url> \
  --param SOURCE_REPOSITORY_REF=<git-branch/tag-reference> \
  --param CONTEXT_DIR=<path-withing-git-project-to-build> \
  --param GITHUB_WEBHOOK_SECRET=<github-webhook-secret> \
  --param IMAGE_STREAM_NAMESPACE=<imagestream-namespace> \
  --param MAVEN_MIRROR_URL=<maven-mirror-to-use-for-S2I-builds> 


Example:

oc new-app eap70-basic-s2i \
  --param APPLICATION_NAME=session-servlet \
  --param HOSTNAME_HTTP=session-servlet.apps.openshift.novaordis.io \
  --param SOURCE_REPOSITORY_URL=https://github.com/NovaOrdis/novaordis-session-servlet.git \
  --param SOURCE_REPOSITORY_REF=master \
  --param CONTEXT_DIR=/ \
  --param GITHUB_WEBHOOK_SECRET=n2H6dtHw6Gw3 \
  --param IMAGE_STREAM_NAMESPACE=openshift

Once executed, the "new-app" command creates the following:

Build

The build process is driven by a configuration created automatically, which is similar to servlet-example Build Configuration.

The build process starts immediately and its logs can be monitored with:

oc logs -f build/session-servlet-1

If successful, the build results in the creation of a new deployable image that gets uploaded into the integrated docker registry:

oc get is
NAME              DOCKER REPO                                                          TAGS      UPDATED
session-servlet   docker-registry.default.svc:5000/s2i-build-example/session-servlet   latest    5 minutes ago

S2i-build-example IntegratedDockerRegistry.png

How do I build new images and how do I control the tag?

More build operations:

Build Operations

Deployment

The deployment process begins immediately, triggered by an image change in the project's image stream, and results in the creation of a backing pod:

oc get pod
NAME                      READY     STATUS      RESTARTS   AGE
...
session-servlet-1-d8f2b   1/1       Running     0          14m

The pod provides the backing functionality exposed internally by the service, and externally by the route.

External Route

The route created by the new application template is incomplete, in that it does not specify the target port, and in that, it is not usable. It either has to be edited and fixed with oc edit or it can be dropped and recreated as shown below:

oc delete route session-servlet
oc expose service session-servlet --hostname=session-servlet.apps.openshift.novaordis.io --port=8080

More details about route operations are available here:

OpenShift Route Operations