Kafka Brokers
Remote JMX
JmxTool
Kafka Configuration
Server Monitoring

How to enable remote JMX on Kafka brokers (for JmxTool)?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. While operational, monitoring Kafka's performance and health is crucial. One effective way to monitor Kafka is by enabling Java Management Extensions (JMX), which allows you to monitor, manage, and troubleshoot Kafka brokers remotely. Here we will detail the process of enabling remote JMX on Kafka brokers specifically for use with tools like JmxTool.

Enabling Remote JMX on Kafka Brokers

1. Configure Kafka Brokers to Expose JMX Ports

Kafka brokers do not enable JMX remote access by default; you need to manually enable it. You can do this by editing the Kafka broker's configuration. Locate and modify the server.properties file typically found in the config directory of your Kafka installation.

Add the following lines to enable JMX:

properties
# Kafka JMX configuration to open the port 9999 for JMX tools
JMX_PORT=9999
KAFKA_JMX_OPTS="-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=[Your-Hostname-Here] -Dcom.sun.management.jmxremote.rmi.port=9999"

Replace [Your-Hostname-Here] with the broker's hostname or IP address where you want to enable JMX monitoring.

2. Restart Kafka Broker

After updating the configuration file, restart each broker to apply the changes. Use the appropriate command based on your Kafka setup:

bash
# Example command to restart Kafka broker
bin/kafka-server-stop.sh
bin/kafka-server-start.sh config/server.properties

3. Testing JMX Connection

To ensure that JMX is enabled and accessible remotely, use the jconsole tool available with the Java Development Kit (JDK):

bash
jconsole [Your-Hostname-Here]:9999

If the settings are correct, you will see JConsole connect to the Kafka broker, where you can monitor various metrics like memory usage, thread count, and Kafka-specific metrics.

Using JmxTool with Remote JMX

Kafka ships with a tool called kafka.tools.JmxTool. This can be used to view JMX metrics from the command line. Here’s how to use it:

bash
# Command to use JmxTool
kafka-run-class.sh kafka.tools.JmxTool --jmx-url service:jmx:rmi:///jndi/rmi://[Your-Hostname-Here]:9999/jmxrmi --object-name kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec

Replace [Your-Hostname-Here] accordingly. This example connects to the JMX server and retrieves metrics related to the number of messages received per second.

Security Considerations

Enabling remote JMX without authentication and SSL/TLS can expose sensitive data and control capabilities over the network. It's highly recommended to secure the JMX connection. For a production environment, consider enabling authentication and SSL as following:

properties
# Secure JMX configurations
KAFKA_JMX_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=true -Djava.rmi.server.hostname=[Your-Hostname-Here] -Dcom.sun.management.jmxremote.ssl.need.client.auth=true -Djavax.net.ssl.keyStore=/path/to/keystore -Djavax.net.ssl.keyStorePassword=your-password -Djavax.net.ssl.trustStore=/path/to/truststore -Djavax.net.ssl.trustStorePassword=your-password"

Adjust paths and passwords as necessary.

Summary Table

Configuration ParameterDescriptionExample Values
JMX_PORTPort for JMX to listen on9999
-Dcom.sun.management.jmxremoteEnables remote monitoringtrue
-Dcom.sun.management.jmxremote.authenticateEnables/disables authenticationfalse (for no auth), true (for auth)
-Dcom.sun.management.jmxremote.sslEnables/disables SSL securityfalse (for no SSL), true (for SSL)
-Djava.rmi.server.hostnameHostname for JMX/RMI server interactionsIP address or hostname of Kafka broker
-Dcom.sun.management.jmxremote.rmi.portRMI registry port for remote JMXSame as JMX_PORT (e.g., 9999)

By following the above steps and considerations, you can successfuly set up remote JMX monitoring for Kafka brokers, enhancing visibility into cluster operations and performance.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.