To set up Prometheus monitoring for Apache ZooKeeper, you deploy the official ZooKeeper exporter (a Java agent) alongside each ZooKeeper server, then add a scrape job to your Prometheus configuration pointing at the exporter’s metrics endpoint. The exporter exposes ZooKeeper’s internal metrics in Prometheus format on a dedicated port, typically 9141, and requires no changes to ZooKeeper itself beyond enabling the JMX metrics it relies on.
Why ZooKeeper Needs a Separate Exporter
ZooKeeper does not natively expose metrics in Prometheus’s text-based exposition format. Its operational data—like the number of live connections, outstanding requests, and leader election status—is available through the four-letter-word commands (e.g., mntr, stat) and through Java Management Extensions (JMX). The Prometheus ZooKeeper exporter bridges this gap by reading those sources and translating them into a format Prometheus can scrape. Without an exporter, you would have to write custom scripts to parse mntr output, which is brittle and lacks the rich set of metrics that JMX provides.
How the Exporter Works
The official exporter, maintained by the Prometheus community, runs as a Java agent attached to the ZooKeeper JVM. It registers an HTTP handler on a configurable port (default 9141) that serves metrics in Prometheus format. The exporter collects data from two primary sources:
- JMX MBeans: ZooKeeper exposes a wealth of metrics via JMX, including request latency, packet counts, and server state (leader/follower). The exporter queries these MBeans and converts them to Prometheus metrics with appropriate names and labels.
- Four-letter words: The exporter also issues the
mntrcommand to the local ZooKeeper server to obtain cluster-level statistics likezk_num_alive_connectionsandzk_znode_count. This requires that the four-letter-word interface is enabled (it is by default, but some hardened setups disable it).
The exporter runs inside the same JVM as ZooKeeper, so it does not add a separate process to manage. However, this means it shares ZooKeeper’s resources; for very large deployments, some operators prefer a standalone exporter that connects remotely, but the official agent is the simplest and most common approach.
Step-by-Step Setup
Here is a typical installation sequence for a ZooKeeper node running on Linux:
- Download the exporter JAR from the official Prometheus repository (e.g.,
zookeeper_exporter-2.4.0.jar). Ensure the version matches your ZooKeeper’s Java compatibility. - Set the JMX port for the exporter. The exporter uses a JMX RMI port (default 9141) to expose its HTTP metrics. You can override it with the
-Dcom.sun.management.jmxremote.port=9141JVM argument. - Add the exporter as a javaagent to the ZooKeeper startup script. For example, in
zkServer.sh, add-javaagent:/path/to/zookeeper_exporter.jar=9141to theJVMFLAGSvariable. - Restart ZooKeeper to load the agent. Verify the exporter is serving metrics by curling
http://localhost:9141/metrics—you should see lines likezk_znode_count 100. - Configure Prometheus to scrape the exporter. In
prometheus.yml, add a job with targets pointing to each ZooKeeper node’s IP and port 9141. Use a static config or service discovery depending on your environment.
If you are using Kubernetes, you can deploy the exporter as a sidecar container or as a separate pod that connects to ZooKeeper’s JMX port (which requires enabling remote JMX, a more complex setup).
Choosing the Right Exporter
There are two main options: the official prometheus/zookeeper_exporter (Java agent) and a third-party exporter like dabealu/zookeeper-exporter (Python, uses the four-letter words only). The official one is generally recommended because it provides richer metrics via JMX and is actively maintained. The Python exporter is lighter and easier to run as a separate process, but it lacks JMX-derived metrics like request latency percentiles. For production, use the official agent unless you have a specific constraint (e.g., cannot add javaagent to the JVM).
Common Problems and Troubleshooting
Several issues arise frequently during setup:
- Metrics not appearing: Check that the exporter is actually running by hitting the metrics endpoint. If you get a 404, the agent may not have loaded—verify the
-javaagentpath and that Java is the same version used to start ZooKeeper. - Four-letter word disabled: If ZooKeeper is configured with
4lw.commands.whitelistthat excludesmntr, the exporter will fail to get cluster stats. Addmntrto the whitelist inzoo.cfg. - JMX port conflict: The exporter’s HTTP port (9141) is separate from the JMX RMI port (which is internal). Ensure no other service uses 9141.
- High cardinality: Some JMX metrics include labels like
client_ipwhich can explode in cardinality. Use Prometheus relabeling to drop unnecessary labels if needed.
Costs and Resource Considerations
The exporter adds negligible CPU and memory overhead—typically less than 1% of ZooKeeper’s own usage. However, enabling JMX and the four-letter words can increase log verbosity slightly. There is no licensing cost; both the exporter and Prometheus are open source. The main cost is operational: you must ensure the exporter is updated when ZooKeeper is upgraded, and you need to monitor the exporter itself (e.g., with a blackbox probe) to avoid silent gaps in metrics.
Alternatives and Comparisons
If you prefer not to use an exporter, you can scrape ZooKeeper’s admin server (available in ZooKeeper 3.5+) which provides Prometheus metrics natively via /metrics endpoint. This is the modern approach and eliminates the need for a separate exporter. However, it requires ZooKeeper 3.5.0 or later and enabling the admin server in zoo.cfg (admin.enableServer=true). For older versions, the exporter is the standard choice. Another alternative is to use a full monitoring stack like Grafana with a pre-built ZooKeeper dashboard; the exporter feeds into that seamlessly.
Frequently Asked Questions
Q: Do I need to install the exporter on every ZooKeeper node?
Yes, because the exporter collects local JMX metrics that are specific to each server. Scraping one node gives you only that node’s metrics, not the whole ensemble.
Q: Can I run the exporter as a separate process instead of a javaagent?
Yes, you can run the official exporter in standalone mode by pointing it to a remote ZooKeeper’s JMX port, but that requires enabling remote JMX (which has security implications). The javaagent is simpler and recommended.
Q: What is the default port for the exporter?
The default port is 9141, but you can change it with the -Dcom.sun.management.jmxremote.port=9141 argument or by editing the exporter’s configuration file if using standalone mode.
Q: How do I secure the exporter endpoint?
Since the exporter exposes internal metrics, it should not be publicly accessible. Use network policies to restrict access to Prometheus servers only, or add basic authentication via a reverse proxy. The exporter itself does not support authentication.
Q: Does the exporter work with ZooKeeper 3.4?
Yes, the official exporter supports ZooKeeper 3.4.x, but you must use a compatible Java version (Java 7 or 8). For newer ZooKeeper 3.6+, the admin server is a better option.
Related
- Hydra Mythology Meaning & How to Use It
- Hydra Greece Photos: What They Really Show
- Hydra Eye and Face Cleaner: What It Is
How pages are sourced and corrected: Sources and editorial policy.