资源规划

组件 bigdata-node1 bigdata-node2 bigdata-node3
OS centos7.6 centos7.6 centos7.6
JDK jvm jvm jvm
Kafka kafka kafka kafka
Influxdb N.A Influxdb N.A
JmxTrans N.A jmxtrans N.A
Grafana N.A Grafana N.A

案例规划

CentOS7_JMX-Kafka - 图1

开启Kafka JMX监控

修改Kafka各节点JMX启动配置,开启监控功能:

  1. vi ~/modules/kafka_2.11-0.11.0.3/bin/kafka-server-start.sh

配置如下:

  1. if [ "x$KAFKA_HEAP_OPTS" = "x" ]; then
  2. export KAFKA_HEAP_OPTS="-server -Xms2G -Xmx2G -XX:PermSize=128m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=8 -XX:ConcGCThreads=5 -XX:InitiatingHeapOccupancyPercent=70"
  3. export JMX_PORT="9999"
  4. fi

分发脚本:

  1. cd ~/modules/kafka_2.11-0.11.0.3/bin
  2. scp -r kafka-server-start.sh vagrant@bigdata-node1:~/modules/kafka_2.11-0.11.0.3/bin/
  3. scp -r kafka-server-start.sh vagrant@bigdata-node2:~/modules/kafka_2.11-0.11.0.3/bin/

分发完成后,请重新启动Kafka集群。

创建Influxdb JMX存储

  1. -- 启动Influxdb
  2. influx
  3. -- 创建用户,并分配权限
  4. create user "root" with password '123456' with all privileges;
  5. create database zkdb;

创建JmxTrans JMX代理

指标配置:

  1. cd /home/vagrant/modules/jmxtrans-270/bin
  2. vi kafka1-jmxtrans.json

配置如下:

  1. {
  2. "servers": [
  3. {
  4. "port": "9999",
  5. "host": "192.168.0.101",
  6. "alias": "kafka-1",
  7. "queries": [
  8. {
  9. "obj": "java.lang:type=Memory",
  10. "attr": [
  11. "HeapMemoryUsage",
  12. "NonHeapMemoryUsage"
  13. ],
  14. "resultAlias": "jvmMemory",
  15. "outputWriters": [
  16. {
  17. "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
  18. "url": "http://192.168.0.102:8086/",
  19. "username": "root",
  20. "password": "123456",
  21. "database": "jmxDB",
  22. "tags": {
  23. "application": "kafka"
  24. }
  25. }
  26. ]
  27. }
  28. ]
  29. }
  30. ]
  31. }

启动JMX代理:

  1. cd /home/vagrant/modules/jmxtrans-270/
  2. # 脚本赋权
  3. sudo chmod +x ./bin/jmxtrans.sh
  4. # 加载所有*.json
  5. JAR_FILE=./lib/jmxtrans-all.jar ./bin/jmxtrans.sh start
  6. # 指定加载某类型的json
  7. JAR_FILE=./lib/jmxtrans-all.jar ./bin/jmxtrans.sh start ./bin/kafka*-jmxtrans.json
  8. # 指定文件方式启动
  9. bin/jmxtrans.sh start ./bin/kafka1-jmxtrans.json
  10. bin/jmxtrans.sh stop
  11. bin/jmxtrans.sh status

创建Grafana图表

配置数据源:
image.png
创建dashboard:
image.png

参考

Kafka监控参考:https://softwaremill.com/monitoring-apache-kafka-with-influxdb-grafana/
kafka监控实战(jmxtrans+InfluxDb+Grafana):https://www.jianshu.com/p/ac30f06d433f
JSON:kafka.jsonkafka-jmxtrans.json