Chapter 2. Getting started with Camel K

This chapter explains how to install Red Hat Integration - Camel K on OpenShift and how to set up your development environment. This chapter also explains how to develop and deploy simple Camel K integrations written in Java, XML, and YAML.

2.1. Installing Camel K from the OpenShift OperatorHub

You can install the Red Hat Integration - Camel K Operator on your OpenShift cluster from the OperatorHub. The OperatorHub is available from the OpenShift Container Platform web console and provides an interface for cluster administrators to discover and install Operators. For more details on the OperatorHub, see the OpenShift documentation.

Prerequisites

  • You must have cluster administrator access to an OpenShift 4.3 cluster
Note

You do not need to create a pull secret when installing Camel K from the OpenShift OperatorHub. The Camel K Operator automatically reuses the OpenShift cluster-level authentication to pull the Camel K image from registry.redhat.io.

Procedure

  1. In the OpenShift Container Platform web console, log in using an account with cluster administrator privileges.
  2. Create a new OpenShift project:

    1. In the left navigation menu, click Home > Project > Create Project.
    2. Enter a project name, for example, my-camel-k-project, and click Create.
  3. In the left navigation menu, click Catalog > OperatorHub.
  4. In the Filter by keyword text box, enter Camel K to find the Red Hat Integration - Camel K Operator.
  5. Read the information about the Operator, and click Install. This displays the Create Operator Subscription page.
  6. Select the following subscription settings:

    • Installation Mode > A specific namespace on the cluster > my-camel-k-project
    • Update Channel > techpreview
    • Approval Strategy > Automatic

      Note

      The Installation mode > All namespaces on the cluster and Approval Strategy > Manual settings are also available if required by your environment.

  7. Click Subscribe. This displays the Operators > Installed Operators page.
  8. Wait a few moments until the Status for the Camel K Operator displays Succeeded and the subscription is Up to Date.

2.2. Installing OpenShift Serverless from the OperatorHub

You can install the OpenShift Serverless Operator on your OpenShift cluster from the OperatorHub. The OperatorHub is available from the OpenShift Container Platform web console and provides an interface for cluster administrators to discover and install Operators. For more details on the OperatorHub, see the OpenShift documentation.

The OpenShift Serverless Operator supports both Knative Serving and Knative Eventing features. For more details, see Getting started with OpenShift Serverless.

Prerequisites

Procedure

  1. In the OpenShift Container Platform web console, log in using an account with cluster administrator privileges.
  2. In the left navigation menu, click Catalog > OperatorHub.
  3. In the Filter by keyword text box, enter Serverless to find the OpenShift Serverless Operator.
  4. Read the information about the Operator, and click Install. This displays the Create Operator Subscription page.
  5. Select the default subscription settings:

    • Installation Mode > All namespaces on the cluster
    • Update Channel > Select the channel that matches your OpenShift version, for example, 4.3
    • Approval Strategy > Automatic

      Note

      The Approval Strategy > Manual setting is also available if required by your environment.

  6. Click Subscribe. This displays the Operators > Installed Operators page.
  7. Wait a few moments until the Status for the Serverless displays Succeeded and the subscription is Up to Date.

For more details, see Installing OpenShift Serverless in the OpenShift documentation.

Next steps

You must install the following Knative components using the steps in the OpenShift documentation:

2.3. Installing the Camel K and OpenShift command line tools

Camel K and OpenShift provide command line tools to deploy and manage your integrations in the cloud. This section explains how to install the following Command Line Interface (CLI) tools:

  • kamel - Camel K CLI
  • oc - OpenShift Container Platform CLI
  • kn - OpenShift Serverless CLI

These command line tools are all available on Linux, Windows, and Mac.

Prerequisites

Procedure

  1. In the OpenShift Container Platform web console, log in using an account with developer or administrator privileges.
  2. Click the OpenShift Help icon help icon in the toolbar, and select Command Line Tools.
  3. Download and extract the oc - OpenShift CLI archive if this tool is not already installed. For more details, see the OpenShift CLI documentation.
  4. Download and extract the kn - OpenShift Serverless CLI archive if this tool is not already installed. For more details, see the OpenShift Serverless CLI documentation.
  5. Download and extract the kamel - Camel K CLI archive to install.
  6. Add the kamel binary file to your system path. For example, on Linux, you can put kamel in /usr/bin.
  7. Log into your OpenShift cluster using the oc client tool, for example:

    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  8. Enter the following command to verify the installation of the kamel client tool:

    $ kamel --help

2.4. Setting up your Camel K development environment

You must set up your environment with the recommended development tooling before you can automatically deploy the Camel K quick start tutorials. This section explains how to install the recommended Visual Studio (VS) Code IDE and the extensions that it provides for Camel K.

Note

VS Code is recommended for ease of use and the best developer experience of Camel K. This includes automatic completion of Camel DSL code and Camel K traits, and automatic execution of tutorial commands. However, you can manually enter your code and tutorial commands using your chosen IDE instead of VS Code.

Procedure

  1. Install VS Code on your development platform. For example, on Red Hat Enterprise Linux:

    1. Install the required key and repository:

      $ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
      $ sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
    2. Update the cache and install the VS Code package:

      $ yum check-update
      $ sudo yum install code

      For details on installing on other platforms, see the VS Code installation documentation.

  2. Enter the code command to launch the VS Code editor. For more details, see the VS Code command line documentation.
  3. Install the VS Code Camel Extension Pack, which includes the extensions required for Camel K. For example, in VS Code:

    1. In the left navigation bar, click Extensions.
    2. In the search box, enter Apache Camel.
    3. Select the Extension Pack for Apache Camel by Red Hat, and click Install.

      Selecting the VS Code Extension Pack for Apache Camel by Red Hat

      For more details, see the instructions for the Extension Pack for Apache Camel by Red Hat.

  4. Install the VS Code Didact extension, which you can use to automatically run quick start tutorial commands by clicking links in the tutorial. For example, in VS Code:

    1. In the left navigation bar, click Extensions.
    2. In the search box, enter Didact.
    3. Select the extension, and click Install.

      For more details, see the instructions for the Didact extension.

2.5. Developing Camel K integrations in Java

This section shows how to develop a simple Camel K integration in Java DSL. Writing an integration in Java to be deployed using Camel K is the same as defining your routing rules in Camel. However, you do not need to build and package the integration as a JAR when using Camel K.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple Java integration file. For example:

    $ kamel init HelloCamelK.java
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the HelloCamelK.java integration automatically includes the Camel timer and log components to help you get started:

    // camel-k: language=java
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class HelloCamelK extends RouteBuilder {
      @Override
      public void configure() throws Exception {
    
          // Write your routes here, for example:
          from("timer:java?period=1s")
            .routeId("java")
            .setBody()
              .simple("Hello Camel K from ${routeId}")
            .to("log:info");
    
      }
    }

2.6. Developing Camel K integrations in XML

This section explains how to develop a simple Camel K integration in classic XML DSL. Writing an integration in XML to be deployed using Camel K is the same as defining your routing rules in Camel.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple XML integration file. For example:

    $ kamel init hello-camel-k.xml
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the hello-camel-k.xml integration automatically includes the Camel timer and log components to help you get started:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- camel-k: language=xml -->
    
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://camel.apache.org/schema/spring"
            xsi:schemaLocation="
                http://camel.apache.org/schema/spring
                http://camel.apache.org/schema/spring/camel-spring.xsd">
    
        <!-- Write your routes here, for example: -->
        <route id="xml">
            <from uri="timer:xml?period=1s"/>
            <setBody>
                <simple>Hello Camel K from ${routeId}</simple>
            </setBody>
            <to uri="log:info"/>
        </route>
    
    </routes>

2.7. Developing Camel K integrations in YAML

This section explains how to develop a simple Camel K integration in YAML DSL. Writing an integration in YAML to be deployed using Camel K is the same as defining your routing rules in Camel.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple XML integration file. For example:

    $ kamel init hello.camelk.yaml
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the hello.camelk.yaml integration automatically includes the Camel timer and log components to help you get started:

    # camel-k: language=yaml
    
    # Write your routes here, for example:
    - from:
        uri: "timer:yaml"
        parameters:
          period: "1s"
        steps:
          - set-body:
              constant: "Hello Camel K from yaml"
          - to: "log:info"
    Important

    Integrations written in YAML must have a file name with the pattern *.camelk.yaml or a first line of # camel-k: language=yaml.

2.8. Running Camel K integrations

You can run Camel K integrations in the cloud on your OpenShift cluster from the command line using the kamel run command.

Prerequisites

Procedure

  1. Log into your OpenShift cluster using the oc client tool, for example:

    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. Ensure that the Camel K Operator is running, for example:

    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. Enter the kamel run command to run your integration in the cloud on OpenShift. For example:

    Java example

    $ kamel run HelloCamelK.java
    integration "hello-camel-k" created

    XML example

    $ kamel run hello-camel-k.xml
    integration "hello-camel-k" created

    YAML example

    $ kamel run hello.camelk.yaml
    integration "hello" created

  4. Enter the kamel get command to check the status of the integration:

    $ kamel get
    NAME       PHASE           KIT
    hello      Building Kit    kit-bq666mjej725sk8sn12g

    When the integration runs for the first time, Camel K builds the integration kit for the container image, which downloads all the required Camel modules and adds them to the image classpath.

  5. Enter kamel get again to verify that the integration is running:

    $ kamel get
    NAME       PHASE   KIT
    hello      Running kit-bq666mjej725sk8sn12g
  6. Enter the kamel log command to print the log to stdout:

    $ kamel log hello
    [1] 2020-04-11 14:26:43.449 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.PropertiesFunctionsConfigurer@5223e5ee executed in phase Starting
    [1] 2020-04-11 14:26:43.457 INFO  [main] RuntimeSupport - Looking up loader for language: yaml
    [1] 2020-04-11 14:26:43.655 INFO  [main] RuntimeSupport - Found loader org.apache.camel.k.loader.yaml.YamlSourceLoader@1224144a for language yaml from service definition
    [1] 2020-04-11 14:26:43.658 INFO  [main] RoutesConfigurer - Loading routes from: file:/etc/camel/sources/i-source-000/hello.camelk.yaml?language=yaml
    [1] 2020-04-11 14:26:43.658 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesConfigurer@36c88a32 executed in phase ConfigureRoutes
    [1] 2020-04-11 14:26:43.661 INFO  [main] BaseMainSupport - Using properties from: file:/etc/camel/conf/application.properties
    [1] 2020-04-11 14:26:43.878 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.ContextConfigurer@65466a6a executed in phase ConfigureContext
    [1] 2020-04-11 14:26:43.879 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) is starting
    [1] 2020-04-11 14:26:43.880 INFO  [main] DefaultManagementStrategy - JMX is disabled
    [1] 2020-04-11 14:26:44.147 INFO  [main] DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
    [1] 2020-04-11 14:26:44.157 INFO  [main] DefaultCamelContext - Route: route1 started and consuming from: timer://yaml?period=1s
    [1] 2020-04-11 14:26:44.161 INFO  [main] DefaultCamelContext - Total 1 routes, of which 1 are started
    [1] 2020-04-11 14:26:44.162 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) started in 0.283 seconds
    [1] 2020-04-11 14:26:44.163 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@1c93084c executed in phase Started
    [1] 2020-04-11 14:26:45.183 INFO  [Camel (camel-k) thread #1 - timer://yaml] info - Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from yaml]
    ...
  7. Press Ctrl-C to terminate logging in the terminal.

Additional resources

2.9. Running Camel K integrations in development mode

You can run Camel K integrations in development mode on your OpenShift cluster from the command line. Using development mode, you can iterate quickly on integrations in development and get fast feedback on your code.

When you specify the kamel run command with the --dev option, this deploys the integration in the cloud immediately and shows the integration logs in the terminal. You can then change the code and see the changes automatically applied instantly to the remote integration Pod on OpenShift. The terminal automatically displays all redeployments of the remote integration in the cloud.

Note

The artifacts generated by Camel K in development mode are identical to those that you run in production. The purpose of development mode is faster development.

Prerequisites

Procedure

  1. Log into your OpenShift cluster using the oc client tool, for example:

    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. Ensure that the Camel K Operator is running, for example:

    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. Enter the kamel run command with --dev to run your integration in development mode on OpenShift in the cloud. The following shows a simple Java example:

    $ kamel run HelloCamelK.java --dev
    integration "hello-camel-k" created
    Progress: integration "hello-camel-k" in phase Initialization
    Progress: integration "hello-camel-k" in phase Building Kit
    Progress: integration "hello-camel-k" in phase Deploying
    Progress: integration "hello-camel-k" in phase Running
    IntegrationPlatformAvailable for Integration hello-camel-k: camel-k
    Integration hello-camel-k in phase Initialization
    No IntegrationKitAvailable for Integration hello-camel-k: creating a new integration kit
    Integration hello-camel-k in phase Building Kit
    IntegrationKitAvailable for Integration hello-camel-k: kit-bq8t5cudeam3u3sj13tg
    Integration hello-camel-k in phase Deploying
    No CronJobAvailable for Integration hello-camel-k: different controller strategy used (deployment)
    DeploymentAvailable for Integration hello-camel-k: deployment name is hello-camel-k
    No ServiceAvailable for Integration hello-camel-k: no http service required
    No ExposureAvailable for Integration hello-camel-k: no target service found
    Integration hello-camel-k in phase Running
    [2] Monitoring pod hello-camel-k-866ccb5976-sjh8x[1] Monitoring pod hello-camel-k-866ccb5976-l288p[2] 2020-04-11 14:44:53.691 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.ContextConfigurer@159f197
    [2] 2020-04-11 14:44:53.694 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesConfigurer@1f0f1111
    [2] 2020-04-11 14:44:53.695 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesDumper@6e0dec4a
    [2] 2020-04-11 14:44:53.695 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.PropertiesFunctionsConfigurer@794cb805
    [2] 2020-04-11 14:44:53.712 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.PropertiesFunctionsConfigurer@794cb805 executed in phase Starting
    [2] 2020-04-11 14:44:53.721 INFO  [main] RuntimeSupport - Looking up loader for language: java
    [2] 2020-04-11 14:44:53.723 INFO  [main] RuntimeSupport - Found loader org.apache.camel.k.loader.java.JavaSourceLoader@3911c2a7 for language java from service definition
    [2] 2020-04-11 14:44:54.220 INFO  [main] RoutesConfigurer - Loading routes from: file:/etc/camel/sources/i-source-000/HelloCamelK.java?language=java
    [2] 2020-04-11 14:44:54.220 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesConfigurer@1f0f1111 executed in phase ConfigureRoutes
    [2] 2020-04-11 14:44:54.224 INFO  [main] BaseMainSupport - Using properties from: file:/etc/camel/conf/application.properties
    [2] 2020-04-11 14:44:54.385 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.ContextConfigurer@159f197 executed in phase ConfigureContext
    [2] 2020-04-11 14:44:54.386 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) is starting
    [2] 2020-04-11 14:44:54.387 INFO  [main] DefaultManagementStrategy - JMX is disabled
    [2] 2020-04-11 14:44:54.630 INFO  [main] DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
    [2] 2020-04-11 14:44:54.639 INFO  [main] DefaultCamelContext - Route: java started and consuming from: timer://java?period=1s
    [2] 2020-04-11 14:44:54.643 INFO  [main] DefaultCamelContext - Total 1 routes, of which 1 are started
    [2] 2020-04-11 14:44:54.643 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) started in 0.258 seconds
    [2] 2020-04-11 14:44:54.644 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@6e0dec4a executed in phase Started
    [2] 2020-04-11 14:44:55.671 INFO  [Camel (camel-k) thread #1 - timer://java] info - Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from java]
    ...
  4. Edit the content of your integration DSL file, save your changes, and see the changes displayed instantly in the terminal. For example:

    ...
    integration "hello-camel-k" updated
    ...
    [3] 2020-04-11 14:45:06.792 INFO  [main] DefaultCamelContext - Route: java started and consuming from: timer://java?period=1s
    [3] 2020-04-11 14:45:06.795 INFO  [main] DefaultCamelContext - Total 1 routes, of which 1 are started
    [3] 2020-04-11 14:45:06.796 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) started in 0.323 seconds
    [3] 2020-04-11 14:45:06.796 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@6e0dec4a executed in phase Started
    [3] 2020-04-11 14:45:07.826 INFO  [Camel (camel-k) thread #1 - timer://java] info - Exchange[ExchangePattern: InOnly, BodyType: String, Body: Ciao Camel K from java]
    ...
  5. Press Ctrl-C to terminate logging in the terminal.

Additional resources