Search

Chapter 4. Example workflows for S2I on OpenShift

download PDF

4.1. Remote debugging Java application for OpenShift image

The example in the procedure shows the remote debugging of a Java application deployed on OpenShift by using the S2I for OpenShift image. You can enable the capability by setting the value of the environment variables JAVA_DEBUG to true and JAVA_DEBUG_PORT to 9009, respectively.

Note

If the JAVA_DEBUG variable is set to true and no value is provided for the JAVA_DEBUG_PORT variable, JAVA_DEBUG_PORT is set to 5005 by default.

Prepare for deployment

Procedure

  1. Log in to the OpenShift instance by running following command and by providing your credentials:

    $ oc login
  2. Create a new project:

    $ oc new-project js2i-remote-debug-demo

Deployment

You can enable remote debugging for your new and existing applications.

Enabling remote debugging for a new application

Procedure

  • Create a new application by using the S2I for OpenShift image and example Java source code. Ensure that you set the JAVA_DEBUG and the JAVA_DEBUG_PORT environment variables before creating your application:

    $ oc new-app --context-dir=getting-started --name=quarkus-quickstart \
     'registry.access.redhat.com/ubi8/openjdk-11~https://github.com/quarkusio/quarkus-quickstarts.git#2.12.1.Final'
      -e JAVA_DEBUG=true \
      -e JAVA_DEBUG_PORT=9009

    Proceed to Connect local debugging port to a port on the pod.

Enabling remote debugging for an existing application

Procedure

  1. Switch to the appropriate OpenShift project:

     $ oc project js2i-remote-debug-demo
  2. Retrieve the name of the deploymentconfig:

    $ oc get dc -o name
     deploymentconfig/openshift-quickstarts
  3. Edit the deploymentconfig and add the JAVA_DEBUG=true and JAVA_DEBUG_PORT=9009 environment variables.
  4. Specify object to edit at the path .spec.template.spec.containers and type of Container:

    $ oc edit dc/openshift-quickstarts
    Note

    Launch an editor to run oc edit command in your terminal. You can change the editor that is launched by defining your environment’s EDITOR variable.

Proceed to Connect local debugging port to a port on the pod.

Post-deployment

Connect local debugging port to a port on the pod

Procedure

  1. Get the name of the pod running the application (Status Running):

    Example showing openshift-quickstarts-1-1uymm as the pod name.

    $ oc get pods
    NAME                            READY     STATUS      RESTARTS   AGE
    openshift-quickstarts-1-1uymm   1/1       Running     0          3m
    openshift-quickstarts-1-build   0/1       Completed   0          6m
  2. Use the OpenShift or Kubernetes port forwarding feature to listen on a local port and forward to a port on the OpenShift pod. <running-pod> is the value of the NAME field for the pod with Status "running" from the previous command output:

    $ oc port-forward <running-pod> 5005:9009
    Forwarding from 127.0.0.1:5005 -> 9009
    Forwarding from [::1]:5005 -> 9009
    Note

    In the previous example, 5005 is the port number on the local system, while 9009 is the remote port number of the OpenShift pod running the S2I for OpenShift image. Therefore, future debugging connections made to local port 5005 are forwarded to port 9009 of the OpenShift pod, running the Java Virtual Machine (JVM).

Important

The command might prevent you from typing further in the terminal. In this case, launch a new terminal for performing the next steps.

Attach debugger to an application

Procedure

  1. Attach the debugger on the local system to the remote JVM running on the S2I for OpenShift image:

    $ jdb -attach 5005
    Set uncaught java.lang.Throwable
    Set deferred uncaught java.lang.Throwable
    Initializing jdb ...
    >
    ...
    Note

    Once the local debugger to the remote OpenShift pod debugging connection is initiated, an entry similar to handling connection for 5005 is shown in the console where the previous oc port-forward command was issued.

  2. Debug the application:

    $ jdb -attach 5005
    Set uncaught java.lang.Throwable
    Set deferred uncaught java.lang.Throwable
    Initializing jdb ...
    > threads
    Group system:
      (java.lang.ref.Reference$ReferenceHandler)0x79e Reference Handler            cond. waiting
      (java.lang.ref.Finalizer$FinalizerThread)0x79f  Finalizer                    cond. waiting
      (java.lang.Thread)0x7a0                         Signal Dispatcher            running
    Group main:
      (java.util.TimerThread)0x7a2                    server-timer                 cond. waiting
      (org.jolokia.jvmagent.CleanupThread)0x7a3       Jolokia Agent Cleanup Thread cond. waiting
      (org.xnio.nio.WorkerThread)0x7a4                XNIO-1 I/O-1                 running
      (org.xnio.nio.WorkerThread)0x7a5                XNIO-1 I/O-2                 running
      (org.xnio.nio.WorkerThread)0x7a6                XNIO-1 I/O-3                 running
      (org.xnio.nio.WorkerThread)0x7a7                XNIO-1 Accept                running
      (java.lang.Thread)0x7a8                         DestroyJavaVM                running
    Group jolokia:
      (java.lang.Thread)0x7aa                         Thread-3                     running
    >

Additional resources

4.2. Running flat classpath JAR on source-to-image for OpenShift

The example in the procedure describes the process of running flat classpath java applications on S2I for OpenShift.

Prepare for Deployment

Procedure

  1. Log in to the OpenShift instance by providing your credentials:

    $ oc login
  2. Create a new project:

    $ oc new-project js2i-flatclasspath-demo

Deployment

Procedure

  1. Create a new application using the S2I for OpenShift image and Java source code:

    $ oc new-app --context-dir=getting-started --name=quarkus-quickstart \
     'registry.access.redhat.com/ubi8/openjdk-11~https://github.com/quarkusio/quarkus-quickstarts.git#2.12.1.Final'

Post-deployment

Procedure

  1. Get the service name:

    $ oc get svc
  2. Expose the service as a route to be able to use it from the browser:

    $ oc expose svc/openshift-quickstarts --port=8080
  3. Get the route:

    $ oc get route
  4. Access the application in your browser by using the URL (value of HOST/PORT field from previous command output).
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.