若Zookeeper通过systemd管理(默认安装方式),可使用以下命令快速查看服务状态:
sudo systemctl status zookeeper
该命令会显示Zookeeper是否处于running状态、近期日志片段及服务启动时间等信息。若需实时跟踪日志,可添加-f参数:
sudo journalctl -u zookeeper -f
此方法适用于快速确认服务是否正常运行。
Zookeeper自带的zkServer.sh脚本可提供更详细的服务器状态信息,包括节点角色(Leader/Follower/Standby):
/path/to/zookeeper/bin/zkServer.sh status
执行后会输出类似以下内容,明确节点在集群中的角色:
Mode: leader # 或 follower/standby
该脚本是排查节点状态异常的常用工具。
通过JMX可监控Zookeeper的内部性能指标(如连接数、请求延迟、节点数量等)。需先在zoo.cfg中启用JMX:
jmx.port=9999
jmx.local.only=false
重启Zookeeper使配置生效,再使用jconsole或jvisualvm连接:
jconsole localhost:9999
连接后可查看MBeans标签下的Zookeeper相关指标(如org.apache.ZooKeeperService),实现实时监控。
安装Prometheus:下载并解压Prometheus,编辑prometheus.yml添加Zookeeper抓取目标:
scrape_configs:
- job_name: 'zookeeper'
static_configs:
- targets: ['localhost:9999'] # 若使用Zookeeper Exporter,替换为exporter端口(如9143)
启动Prometheus:./prometheus --config.file=prometheus.yml。
安装Grafana:通过sudo apt install grafana安装,启动后添加Prometheus为数据源,导入Zookeeper监控面板(如ID为12520的官方面板),实现CPU使用率、内存占用、连接数、请求延迟等指标的可视化。
Telegraf的inputs.zookeeper插件可直接采集Zookeeper指标:
sudo apt install telegraf。/etc/telegraf/telegraf.conf,添加以下内容:[[inputs.zookeeper]]
interval = "60s"
servers = ["localhost:2181"] # Zookeeper服务器地址
timeout = "5s"
sudo systemctl restart telegraf,数据将自动发送至Prometheus。通过检查Zookeeper默认端口(2181)的监听状态,确认服务是否正常启动:
sudo netstat -tuln | grep 2181
# 或使用ss命令(更高效)
sudo ss -tuln | grep 2181
若输出中包含LISTEN状态,则说明端口已正常监听。
zookeeper-top是一个实时监控Zookeeper节点状态的命令行工具,可显示节点的连接数、请求处理速度等信息:
# 克隆并编译项目
git clone https://github.com/sgroschupf/zookeeper-top.git
cd zookeeper-top
mvn package
# 运行工具
./target/zookeeper-top-1.0-SNAPSHOT.jar
运行后会实时刷新显示Zookeeper节点的状态,适用于快速排查节点负载问题。