Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Containerize Your Camel Route on Karaf Within OpenShift

DZone's Guide to

How to Containerize Your Camel Route on Karaf Within OpenShift

We set up a simple camel route using an FIS based on a Karaf image, which we will use to containerize your camel route on Karaf within OpenShift.

Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90 day trial 3Scale by Red Hat

The Red Hat JBoss Fuse solution offers a new approach to lightweight and modular ESBs and is perfectly suited to allow you to implement light integrations.

JBoss Fuse is based on the power of Apache Karaf. Apache Karaf lays in the easy deployment of your ActiveMQ Broker, your CXF web services, or your own Apache Camel routes.

Most of us are more familiar with that OSGI Environment and what they offer, like control of the classloader behavior and module isolation and APIs within a single app or JVM process.

For this post, we are going to set up a simple camel-route using an FIS (Fuse Integration Service) based on a Karaf image (jboss-fuse-6/fis-karaf-openshift), which we will use to containerize your camel route on Karaf within OpenShift!

To get started, you have a Maven Archetype catalog that includes some examples, but let's see my example available here.

This example shows a simple Apache Camel microservice that expose REST service and log a message in Karaf.

All the necessary dependencies of this Camel route are listed as features to our Karaf distribution on startup in the karaf-maven plugin:

 <!-- 2. create karaf assembly -->
      <!-- karaf-maven-plugin creates custom microservice distribution -->
      <plugin>
        <groupId>org.apache.karaf.tooling</groupId>
        <artifactId>karaf-maven-plugin</artifactId>
        <version>${karaf.plugin.version}</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>karaf-assembly</id>
            <goals>
              <goal>assembly</goal>
            </goals>
            <phase>install</phase>
          </execution>
          <execution>
            <id>karaf-archive</id>
            <goals>
              <goal>archive</goal>
            </goals>
            <phase>install</phase>
          </execution>
        </executions>
        <configuration>
          <!-- we are using karaf 2.4.x -->
          <karafVersion>v24</karafVersion>
          <useReferenceUrls>true</useReferenceUrls>
          <!-- do not include build output directory -->
          <includeBuildOutputDirectory>false</includeBuildOutputDirectory>
          <!-- no startupFeatures -->
          <startupFeatures>
            <feature>karaf-framework</feature>
            <feature>shell</feature>
            <feature>jaas</feature>
            <feature>aries-blueprint</feature>
            <feature>camel-blueprint</feature>
            <feature>camel-cxf</feature>
            <feature>camel-jetty</feature>
            <feature>camel-jackson</feature>
            <feature>camel-websocket</feature>
          </startupFeatures>
          <startupBundles>
            <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
          </startupBundles>
        </configuration>
      </plugin>

We can overridden files using resources from src/main/resources/assembly/. The included sample log file etc/org.ops4j.pax.logging.cfg allows you to customize your logging. In my case, the log level is set to debug.

The Camel route is implemented using solely the XML DSL (there is no Java code). The source code is provided in the following XML file: src/main/resources/OSGI-INF/blueprint/camel-rest.xml :

<camelContext id="rest-example-context"
xmlns="http://camel.apache.org/schema/blueprint">

 <restConfiguration component="jetty" port="9191" />
<rest path="FIS">
<get uri="/{name}">
<to uri="direct:order" />
</get>
</rest>
<route id="rest-route">
<from uri="direct:order" />
<setBody>
<simple>Welcome to: ${headers.name} on Fuse Integration Service in OpenShift</simple>
</setBody>
<log message=">>> ${body} : ${sys.runtime.id}" />
</route>
</camelContext>

The endpoint of our Rest service is exposed on the port 9191. This port should be also declared and exposed in the Docker by setting the HTTP port in Docker Maven plugin:

<env>
<JAVA_LIB_DIR>/deployments/lib</JAVA_LIB_DIR>
<JAVA_MAIN_CLASS>org.apache.camel.spring.Main</JAVA_MAIN_CLASS>
<HTTP_PORT>${http.port}</HTTP_PORT>
</env>
<ports>
  <port>${http.port}</port>
</ports>

The service is configured in pom.xml for fabric8 plugin to pick up:

<fabric8.service.name>${servicename}</fabric8.service.name>
<fabric8.service.port>${serviceport}</fabric8.service.port>
<fabric8.service.containerPort>${http.port}</fabric8.service.containerPort>

In order to deploy this example to OpenShift, you can use one of the following application development workflows:

  • Fabric8 Maven workflow.
  • OpenShift source-to-image (S2I) workflow.

In the case of a local development environment, you can simply use fabric8 maven workflow by choosing -Pf8-local-deploy as a goal that creates Docker and JSON templates and then applies them to OpenShift.

mvn -Pf8-local-deploy

When the example runs in OpenShift, you can use the OpenShift client tool to inspect the status:

$ oc get pods
NAME                         READY     STATUS    RESTARTS   AGE
karaf-camel-rest-ose-k8fqw   1/1       Running   0          1h

Then find the name of the pod that runs this example and output the logs from the running pods with:

$ oc logs karaf-camel-rest-ose-k8fqw

You can test the application by running the restful endpoint:

curl http://karaf-camel-rest-ose.rhel-cdk.10.1.2.2.xip.io/FIS/aboucham
Welcome to: aboucham on Fuse Integration Service in OpenShift

To access to the usual console hawt.io, this web console now is embedded by default in OpenShift Console, and you have to access to it by clicking on the button Open Java console":

karaf_hawtio_ose

In the application console, we see the route is active:

karaf_ose

Thanks for reading, and I hope that you enjoyed this post!

Discover how you can achielve enterpriese agility with microservices and API management

Topics:
integration ,containers ,camel ,openshift

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}