Getting started with Camel K tutorial

This tutorial is a quickstart introduction to Apache Camel K. What is Apache Camel K? in a nutshell Apache Camel K is a lightweight integration framework built from Apache Camel that runs natively on Kubernetes and is specifically designed for serverless and microservice architectures.

The idea of Camel K is bare simple: let developers use the Enterprise Integration Patterns (EIP) natively on Kubernetes, expressing them using the powerful Camel DSL. As Camel K requires using Kubernetes we will test in with minikube (it will work just the same with OpenShift).

The procedure for installing Minikube is detailed at: https://minikube.sigs.k8s.io/docs/start/

As first step, start minikube:

$ minikube start 

With Minikube running, the next step is to deploy the registry.

$ minikube addons enable registry 

To start using Camel K you need the “kamel” shell tool, that is available into the release page for the latest version of the camel-k-client tool: https://github.com/apache/camel-k/releases

Download and uncompress the archive. and put the binary file “kamel” in your system’s path. For example, in /usr/bin.

Now, you can proceed with the installation:

$ kamel install 

When the installation is completed, you should be to see the camel-k operator in your Kubernetes’ pods:

$ kubectl get pods 

NAME                                READY   STATUS    RESTARTS   AGE 
camel-k-operator-57bbcbd6dc-ndqj5   1/1     Running   0          6m34s 

There are plenty of available examples in https://github.com/apache/camel-k/releases . For example, let’s give a run to Sample.java

import org.apache.camel.builder.RouteBuilder;

public class Sample extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("timer:tick").log("Hello Camel K!");
  }
}

In order to run this sample Camel Route, you can use the “run” option. We will also add the “–dev” flag to allow live reload of code:

$ kamel run Sample.java --dev 

The Camel Route will soon start:

[1] 2021-01-20 08:35:56,508 INFO  [io.quarkus] (main) camel-k-integration 1.3.0 on JVM (powered by Quarkus 1.10.3.Final) started in 2.123s.  [1] 2021-01-20 08:35:56,509 INFO  [io.quarkus] (main) Profile prod activated.  [1] 2021-01-20 08:35:56,511 INFO  [io.quarkus] (main) Installed features: [camel-bean, camel-core, camel-endpointdsl, camel-k-core, camel-k-loader-java, camel-k-runtime, camel-main, camel-support-common, camel-timer, cdi] [1] 2021-01-20 08:35:57,516 INFO  [route1] (Camel (camel-1) thread #0 - timer://tick) Hello Camel K!

Notice you don’t need to specify any dependency specification in the folder, Camel K will figure it out, and inject it during the build. So all you need is to JUST write your application.

Also, as we are running in dev mode, that allow users to create integration incrementally and with immediate “buildless” redeploys. For example, change just the log message in the sample class:

public void configure() throws Exception {
  from("timer:tick").log("Hello Camel K just changed!");
}

The application will be updated automatically as you save the code:

[1] 2021-01-20 08:43:49,811 INFO  [route1] (Camel (camel-1) thread #0 - timer://tick) Hello Camel K just changed!

We can be even more adventurous, we will use a property file to define the content of the message.Let’s edit a file named sample.properties to have this content:

message=Hello World from Camel K!

And change the Sample.java class accordingly:

import org.apache.camel.builder.RouteBuilder;

public class Sample extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("timer:tick").log("{{message}}");
  }
}

In order to use the external property file, just add the option –property-file at start up:

$ kamel run Sample.java --property-file sample.properties --dev 

And here’s back your original log message:

[1] 2021-01-20 08:48:29,496 INFO  [route1] (Camel (camel-1) thread #0 - timer://tick) Hello World from Camel K!

Languages support in Camel-K

Camel-K is not just for java. Several other languages are supported, such as:

  • Groovy (currently the preferred language for writing Camel K integration code)
  • Kotlin
  • Java
  • XML
  • JavaScript

Just have a look at the hello.groovy example:

from("timer:tick?period=3s")   
   .setBody().constant("Hello from Camel K in Groovy!!!")   
     .to("log:message")

As you can see the code is even more concise. To run the Groovy exampple, you can use the same commandline option:

$ kamel run hello.groovy --dev 

And that’s it:

INFO  [message] (Camel (camel-1) thread #0 - timer://tick) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from Camel K in Groovy!!!]

We just scratched the surface with some Camel-K quickstarts. That was an example on how integration code immediately runs in the cloud. Nothing else is needed! Check out the Camel-K documentation to learn more: https://camel.apache.org/camel-k/latest/index.html