Apache Karaf 4.2.1 & Docker

Apache Karaf 4.2.1 & Docker

Deployed early September 2018, Apache Karaf™ 4.2.1 has a major update of the 4.2.x series by making corrections, improvements and new features such as :

  • better support for Java 9, 10 & 11
  • Sample sets directly available in the Karaf distribution, allowing you to get started easily.
  • Better KarafTestSupport allowing you to easily create your own integration tests

One of the new features added in this version is Docker support: an excellent container platform. The combination of Docker (system container) and Apache Karaf ™ (application container) gives a highly flexible set as well as a powerful approach for your applications, ecosystem. You thus decide on the provisioning approach you wish to adopt:

  • Static by using Karaf's static profile directly executed in Docker
  • Dynamic with a regular Karaf distribution used in Docker

Version 4.2.1 provides two tools:

  1. A Build tooling (scripts) which makes it easy to create a Docker Karaf image that can be used to create Docker containers
  2. A dockerfeature which allows to manipulate Docker directly from Karaf (usable from an instance in progress)

Adaptation of the article:" Apache Karaf & Docker» of blog Nanthrax written by Jean-baptiste Onofré (Technical Advisor)

The characteristics of the Docker Karaf

assembly/docker/build.sh is not the only new feature in Apache Karaf 4.2.1. docker has also been added. It allows you to interact with Docker directly from an Apache Karaf instance running.

The prerequisites

Function docker Apache Karaf interacts with Docker using tcp. You must activate it by passing an option to the deamon Docker : /usr/bin/dockerd -H fd:// -H tcp://localhost:2375

For example, on Ubuntu System, you can configure the deamon Docker into the file /lib/systemd/system/docker.service by modifying the property ExecStartExecStart=/usr/bin/dockerd -H fd:// -H tcp://localhost:2375

You must restart the Deamon Docker after have modified the files docker.service>$ service docker restart

You can verify the state of the Docker

> service docker status
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-09-11 11:30:08 CEST; 3min 7s ago
     Docs: https://docs.docker.com
 Main PID: 31978 (dockerd)
    Tasks: 38
   CGroup: /system.slice/docker.service
           ├─31978 /usr/bin/dockerd -H fd:// -H tcp://localhost:2375
           └─32004 docker-containerd --config /var/run/docker/containerd/containerd.toml
sept. 11 11:30:07 precision dockerd[31978]: time="2018-09-11T11:30:07.569933812+02:00" level=warning msg="Your kernel does not support cgroup rt period"
sept. 11 11:30:07 precision dockerd[31978]: time="2018-09-11T11:30:07.569949221+02:00" level=warning msg="Your kernel does not support cgroup rt runtime"
sept. 11 11:30:07 precision dockerd[31978]: time="2018-09-11T11:30:07.570903883+02:00" level=info msg="Loading containers: start."
sept. 11 11:30:08 precision dockerd[31978]: time="2018-09-11T11:30:08.258633531+02:00" level=info msg="Default bridge (docker0) is assigned with an IP address Daemon option --bip can be used to set a preferred IP address"
sept. 11 11:30:08 precision dockerd[31978]: time="2018-09-11T11:30:08.289844901+02:00" level=info msg="Loading containers: done."
sept. 11 11:30:08 precision dockerd[31978]: time="2018-09-11T11:30:08.360892312+02:00" level=info msg="Docker daemon" commit=7390fc6 graphdriver(s)=overlay2 version=17.12.1-ce
sept. 11 11:30:08 precision dockerd[31978]: time="2018-09-11T11:30:08.361071172+02:00" level=info msg="Daemon has completed initialization"
sept. 11 11:30:08 precision dockerd[31978]: time="2018-09-11T11:30:08.365804009+02:00" level=info msg="API listen on"
sept. 11 11:30:08 precision dockerd[31978]: time="2018-09-11T11:30:08.365826737+02:00" level=info msg="API listen on /var/run/docker.sock"
sept. 11 11:30:08 precision systemd[1]: Started Docker Application Container Engine.

This docker service can be used locally or remotely using the Karaf Docker function.

Installation of the Karaf Docker function

It is very easy to install the function docker On Apache karaf instance in progress karaf@root()> feature:install docker

When the function docker installed, news commands docker:* shell are available on Karaf

docker:info is available to obtain details on the Docker service:

karaf@root()> docker:info 
Containers: 0
Debug: false
Driver: overlay2
ExecutionDriver: null
IPv4Forwarding: true
Images: 1
IndexServerAddress: https://index.docker.io/v1/
InitPath: null
InitSha1: null
KernelVersion: 4.15.0-33-generic
MemoryLimit: true
NEventsListener: false
NFd: 20
NGoroutines: 33
SwapLimit: false

By default, Karaf Docker uses the URL http://localhost:2375 to interact with Docker. If you want to connect to another Docker service (e. g. remotely), you must use the command --url

karaf@root()> docker:info --url http://localhost:2375

docker:version provides details of the Docker service you are using:

karaf@root()> docker:version
Version: 17.12.1-ce
Os: linux
Kernel version: 4.15.0-33-generic
Go version: go1.10.1
Git commit: 7390fc6
Arch: amd64
API version: 1.35
Build time: 2018-02-28T17:46:05.000000000+00:00
Experimental: null

Any Docker action can be performed using the Karaf shell command, or directly in the DockerMBean provided by Karaf.

Case study: ElasticSearch docker with http proxy

The Apache Karaf documentation describes all commands docker:* : for more information .

Handle the Docker container directly in Karaf and display in Karaf

To illustrate this use case, we use a Docker image to supply ElasticSearch. First, let's search the image directly in Karaf using the commands docker:search shell :

karaf@root()> docker:search elasticsearch
Name                                │ Description                                                                                          │ Automated │ Official │ Star Count
elasticsearch                       │ Elasticsearch is a powerful open source search and analytics engine that makes data easy to explore. │ false     │ true     │ 3027

The ElasticSearch image is extracted using the command docker:pull :

karaf@root()> docker:pull -v elasticsearch

The images available in the Docker service are visible using the command docker:images :

karaf@root()> docker:images
Id                                                                      │ RepoTags               │ Created    │ Labels │ Size      │ Virtual Size
sha256:362c5cb1669b144eef89d39a6e9153c546c14923e10fde17701920615c0e2add │ [elasticsearch:latest] │ 1536162308 │        │ 485920188 │ 485920188

Now, we can create a container using the Elasticearch image and control it directly from Karaf:

karaf@root()> docker:create --cmd '/docker-entrypoint.sh' --tty --image elasticsearch --exposedPort 9200/tcp --hostPortBinding 9200 elasticsearch

The ElasticSearch container available with the command docker:ps is thus visible:

karaf@root()> docker:ps -a
Id                                                               │ Names           │ Command                                     │ Created    │ Image         │ Image ID                                                                │ Status  │ State   │ Ports │ Size │ Size Root
6db4369b07eb3894034c77ea3e28b08e80ec723a711ee90ff6f1a3917256a57b │ [elasticsearch] │ /docker-entrypoint.sh /docker-entrypoint.sh │ 1536674098 │ elasticsearch │ sha256:362c5cb1669b144eef89d39a6e9153c546c14923e10fde17701920615c0e2add │ Created │ created │       │ 0    │ 0

We start our ElasticSearch container:

karaf@root()> feature:install http

The functionality http provides support for HTTP proxies. Thus, we can simply add a Karaf HTTP service proxy to the ElasticSearch Docker container:

karaf@root()> http:proxy-add /elasticsearch http://localhost:9200

The available HTTP proxy is visible in Karaf:

karaf@root()> http:proxies 
URL            │ ProxyTo
/elasticsearch │ http://localhost:9200

Now, ElasticSearch is accessible via Karaf using http://localhost:8181/elasticsearch.

Basically, the Karaf function docker is a way to interact with the Docker service directly with the docker's commands. It also offers a unique feature: procurement.

The Karaf Docker supply

You have a Karaf instance running where you install a lot of features, bundles, modify the configuration,..... You want to create a Docker container using this instance? Simplify the use of the command docker:provision. It will create a Docker container with your instance as the container volume, starting it automatically.

To create the container:

karaf@root()> docker:provision mykaraf

You now have a new container mykaraf

karaf@root()> docker:ps -a
Id                                                               │ Names     │ Command                     │ Created    │ Image             │ Image ID                                                                │ Status  │ State   │ Ports │ Size │ Size Root
d3f3feb99f7f63735fed24946b2d4fe76d83742db0cada4c743ba3535cb73ed4 │ [mykaraf] │ /opt/apache-karaf/bin/karaf │ 1536681767 │ java:8-jre-alpine │ sha256:fdc893b19a147681ee764b2edab6c494d60fe99d83b14b8794bbcbc040ec7aa7 │ Created │ created │       │ 0    │ 0

Now, the container can be launched even outside the Karaf instance:

$> docker start mykaraf

The mykaraf container has started:

$> docker ps CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                                              NAMES d3f3feb99f7f        java:8-jre-alpine   "/opt/apache-karaf/b…"   3 minutes ago       Up 1 second>1099/tcp,>8101/tcp,>8181/tcp,>44444/tcp   mykaraf

As Karaf Docker automatically created the port connection, the connection to the Karaf instance running in the Docker container using ssh is now possible:

$> ssh -p 8101 karaf@localhost ... karaf@root()>

Discover soon a second article on Apache Karaf 4.2.1 updates: Vanilla and custom distributions.

Blocked in your roadmaps?

Would you like to train your teams?

en_GBEnglish (UK)
fr_FRFrançais en_GBEnglish (UK)