Using JMX to monitor and administer Apache Karaf
By default, Apache Karaf can be administered via Java Management Extensions (JMX). However, systems builders often need to tweak the default configurations to get their deployment integrated into their network. In this recipe, we'll show you how to make these changes.
Getting ready
The ingredients of this recipe include the Apache Karaf distribution kit, access to JDK, and a source code editor. The sample configuration for this recipe is available at https://github.com/jgoodyear/ApacheKarafCookbook/tree/master/chapter1/chapter1-recipe5.
Tip
Administrators should take care when exposing JMX access to their Karaf instance. Enabling of SSL and use of strong passwords is recommended.
How to do it…
- The first step is editing the management configuration. Apache Karaf ships with a default management configuration. To make our modifications, we update the
etc/org.apache.karaf.management.cfg
file. Consider the following code:# # Port number for RMI registry connection # rmiRegistryPort = 11099 # # Port number for RMI server connection # rmiServerPort = 44445
The default ports, 1099 and 44444, are usually fine for general deployment. Change these ports only if you are experiencing port conflicts on your deployment. Now, consider the following snippet:
# # Role name used for JMX access authorization # If not set, this defaults to the ${karaf.admin.role} configured in etc/system.properties # jmxRole=admin
Towards the bottom of the configuration file, there will be a commented-out entry for
jmxRole
; enable this by removing the hash character. - The next step is updating the user's file. We must now update the
etc/users.properties
file with the following code:karaf = karaf,_g_:admingroup _g_\:admingroup = group,admin,manager,viewer,jmxRole
The
users.properties
file is used to configure users, groups, and roles in Karaf. We appendjmxRole
to the admin group. The syntax for this file follows theUsername = password, groups
format. - The last step is testing our configuration. After making the previous configuration changes, we'll need to restart our Karaf instance. Now, we can test our JMX setup. Have a look at the following screenshot:
After restarting Karaf, use a JMX-based admin tool of your choice (the previous screenshot shows JConsole) to connect to the container. Due to image size restrictions, the full URL couldn't be displayed. The full URL is
service:jmx:rmi://127.0.0.1:44445/jndi/rmi://127.0.0.1:11099/karaf-root
. The syntax of the URL isservice:jmx:rmi://host:${rmiServerPort}/jndi/rmi://host:${rmiRegistryPort}/${karaf-instance-name}
.