New Karaf HTTP proxy feature

Apache Karaf 4.2.0 is now on vote, bringing a lot of improvements and new features. One of these new features is the HTTP proxy.

The idea of the Karaf HTTP proxy feature is to be able to “expose” a non Karaf running HTTP application into the the Karaf web container. For example, you have a legacy HTTP application running standalone.

This application is bound to http://localhost:9999/foo. For consistency, you want Karaf as your main HTTP web container, acting as a gateway/proxy to any other application. The Karaf HTTP proxy feature is for you.

Part of the HTTP feature

You may know the http feature, installing the Karaf web container. The new proxy feature is part of the http feature. So, you just have to install:

karaf@root()> feature:install http

You can see now three commands available in addition of the http:list one:

  • http:proxies
  • http:proxy-add
  • http:proxy-remove

To illustrate the usage of HTTP proxy, let’s take the example of Karaf WebConsole.

Example: proxying Karaf WebConsole

First, let’s install the Karaf WebConsole:

karaf@root()> feature:install webconsole

By default, the Karaf WebConsole is available on http://localhost:8181/system/console. We can see that with http:list:

karaf@root()> http:listID │ Servlet          │ Servlet-Name             │ State       │ Alias               │ Url───┼──────────────────┼──────────────────────────┼─────────────┼─────────────────────┼────────────────────────85 │ ResourceServlet  │ /system/console/res:/res │ Deployed    │ /system/console/res │ [/system/console/res/*]85 │ KarafOsgiManager │ ServletModel-2           │ Undeployed  │ /system/console     │ [/system/console/*]85 │ KarafOsgiManager │ ServletModel-6           │ Deployed    │ /system/console     │ [/system/console/*]

However, for convenience, we want to use /webconsole for WebConsole HTTP context.

To do so, we can proxy the current WebConsole location with another one using:

karaf@root()> http:proxy-add /webconsole http://localhost:8181/system/console

We can see the proxy in the list:

karaf@root()> http:proxiesURL         │ ProxyTo────────────┼─────────────────────────────────────/webconsole │ http://localhost:8181/system/console

Now, we can access the webconsole using http://localhost:8181/webconsole, Karaf will proxy the request to http://localhost:8181/system/console.

In this example, we are able to bind a Karaf HTTP application to another URL. Let’s see a second example, this time proxying applications located outside of Karaf.

Example: proxying Kibana in Karaf

This second example will proxy Kibana inside Karaf WebContainer. Kibana is running on a machine, in a dedicated JVM (not the Karaf one), and we want to proxy from the Karaf WebContainer.

We just need to modify Kibana to use a dedicated basePath. To do so, in the config/kibana.yml, we just set:

server.basePath: "/kibana"

We can start Kibana (bin/kibana).

This basePath is the proxy context we set in Karaf.

Now, in Karaf, we add the corresponding Kibana proxy to the Kibana location:

karaf@root()> http:proxy-add /kibana http://localhost:5601karaf@root()> http:proxiesURL     │ ProxyTo────────┼──────────────────────/kibana │ http://localhost:5601

Now, we can access Kibana “as it’s in Karaf”, using http://localhost:8181/kibana. The host can be distant, like for instance:

karaf@root()> http:proxy-add /kibana http://remote_host:5601

This behavior is be used in Karaf Decanter soon to “proxy” Kibana.

Why this proxy feature ?

Thanks to this proxy feature, Karaf can be the central application container in your ecosystem, acting as a service gateway. It’s especially very interesting for two new Karaf features:

  • Karaf Docker feature that will allow you to create, manage and control docker container from Karaf. Thanks to the proxy feature, the docker containers exposing HTTP applications will be proxied in Karaf
  • Karaf Vineyard is a new Karaf subproject in preparation dedicated to API Registry/Management/Gateway. Karaf HTTP proxy will be used in Vineyard.

Comments

Popular posts from this blog

Quarkus and "meta" extension

Getting started with Apache Karaf Minho

Apache Karaf Minho and OpenTelemetry