最近在搞Kafka集群監控,之前也是看了網上的很多資料。之所以使用jmxtrans+influxdb+grafana是因為界面酷炫,可以定製化,缺點是不能操作Kafka集群,可能需要配合Kafka Manager一起使用。 環境信息 CentOS Linux release 7.6.1810 (Co ...
最近在搞Kafka集群監控,之前也是看了網上的很多資料。之所以使用jmxtrans+influxdb+grafana是因為界面酷炫,可以定製化,缺點是不能操作Kafka集群,可能需要配合Kafka Manager一起使用。
環境信息
- CentOS Linux release 7.6.1810 (Core)
- jdk1.8.0_201
- zookeeper-3.4.14
- kafka_2.11-2.2.0
開啟Kafka JMX埠
JMX(Java Management Extensions,即Java管理擴展)是一個為應用程式、設備、系統等植入管理功能的框架。JMX可以跨越一系列異構操作系統平臺、系統體繫結構和網路傳輸協議,靈活的開發無縫集成的系統、網路和服務管理應用。Kafka做為一款Java應用,已經定義了豐富的性能指標,(可以參考Kafka監控指標),通過JMX可以輕鬆對其進行監控。
在${KAFKA_HOME}/bin/路徑下修改kafka-run-class.sh
腳本,第一行增加JMX_PORT=9999
即可。
JMX_PORT=9999
重啟Kafka
./bin/kafka-server-stop.sh ./bin/kafka-server-start.sh -daemon ./config/server.properties
重啟後查看Kafka以及JMX埠狀態
ps -ef | grep kafka root 8273 1 99 02:32 pts/0 00:00:09 /opt/jdk1.8.0_201/bin/java -Xmx1G -Xms1G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 ...... kafka.Kafka ./config/server.properties netstat -anop | grep 9999 tcp6 0 0 :::9999 :::* LISTEN 8273/java off (0.00/0/0)
安裝InfluxDB
InfluxDB是一個時間序列資料庫,用於處理海量寫入與負載查詢。InfluxDB旨在用作涉及大量時間戳數據的任何用例(包括DevOps監控,應用程式指標,物聯網感測器數據和實時分析)的後端存儲。
下載InfluxDB rpm安裝包
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.5.x86_64.rpm --2019-04-10 02:52:30-- https://dl.influxdata.com/influxdb/releases/influxdb-1.7.5.x86_64.rpm Resolving dl.influxdata.com (dl.influxdata.com)... 54.192.151.21, 54.192.151.81, 54.192.151.87, ... Connecting to dl.influxdata.com (dl.influxdata.com)|54.192.151.21|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 46536692 (44M) [application/octet-stream] Saving to: ‘influxdb-1.7.5.x86_64.rpm’ 100%[================================================================================================================================================================================>] 46,536,692 440KB/s in 60s 2019-04-10 02:53:37 (756 KB/s) - ‘influxdb-1.7.5.x86_64.rpm’ saved [46536692/46536692]
安裝rpm包
rpm -ivh influxdb-1.7.5.x86_64.rpm Preparing... ################################# [100%] Updating / installing... 1:influxdb-1.7.5-1 ################################# [100%] Created symlink from /etc/systemd/system/influxd.service to /usr/lib/systemd/system/influxdb.service. Created symlink from /etc/systemd/system/multi-user.target.wants/influxdb.service to /usr/lib/systemd/system/influxdb.service.
啟動InfluxDB
service influxdb start
Redirecting to /bin/systemctl start influxdb.service
查看InfluxDB狀態
ps -ef | grep influxdb influxdb 8475 1 2 03:01 ? 00:00:00 /usr/bin/influxd -config /etc/influxdb/influxdb.conf root 8486 7007 0 03:02 pts/0 00:00:00 grep --color=auto influxdb service influxdb status Redirecting to /bin/systemctl status influxdb.service ● influxdb.service - InfluxDB is an open-source, distributed, time series database Loaded: loaded (/usr/lib/systemd/system/influxdb.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2019-04-10 03:01:48 EDT; 22s ago Docs: https://docs.influxdata.com/influxdb/ Main PID: 8475 (influxd) CGroup: /system.slice/influxdb.service └─8475 /usr/bin/influxd -config /etc/influxdb/influxdb.conf Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.375804Z lvl=info msg="Starting precreation service" log_id=0EiWgWRl000 service=shard-precreation check_interval=10m advance_period=30m Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.375810Z lvl=info msg="Starting snapshot service" log_id=0EiWgWRl000 service=snapshot Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.375816Z lvl=info msg="Starting continuous query service" log_id=0EiWgWRl000 service=continuous_querier Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.375826Z lvl=info msg="Starting HTTP service" log_id=0EiWgWRl000 service=httpd authentication=false Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.375830Z lvl=info msg="opened HTTP access log" log_id=0EiWgWRl000 service=httpd path=stderr Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.375936Z lvl=info msg="Listening on HTTP" log_id=0EiWgWRl000 service=httpd addr=[::]:8086 https=false Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.375949Z lvl=info msg="Starting retention policy enforcement service" log_id=0EiWgWRl000 service=retention check_interval=30m Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.376138Z lvl=info msg="Listening for signals" log_id=0EiWgWRl000 Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.376389Z lvl=info msg="Storing statistics" log_id=0EiWgWRl000 service=monitor db_instance=_internal db_rp=monitor interval=10s Apr 10 03:01:48 node1 influxd[8475]: ts=2019-04-10T07:01:48.376534Z lvl=info msg="Sending usage statistics to usage.influxdata.com" log_id=0EiWgWRl000
使用InfluxDB客戶端
influx Connected to http://localhost:8086 version 1.7.5 InfluxDB shell version: 1.7.5 Enter an InfluxQL query >
創建用戶和資料庫
> CREATE USER "admin" WITH PASSWORD 'admin' WITH ALL PRIVILEGES
> create database "jmxDB"
創建完成InfluxDB的用戶和資料庫暫時就夠用了,其它簡單操作如下,後面會用到
#創建資料庫 create database "db_name" #顯示所有的資料庫 show databases #刪除資料庫 drop database "db_name" #使用資料庫 use db_name #顯示該資料庫中所有的表 show measurements #創建表,直接在插入數據的時候指定表名 insert test,host=127.0.0.1,monitor_name=test count=1 #刪除表 drop measurement "measurement_name" #退出 quit
安裝jmxtrans
jmxtrans的作用是自動去jvm中獲取所有jmx格式數據,並按照某種格式(json文件配置格式)輸出到其他應用程式(本例中的influxDB)。
下載jmxtrans rpm安裝包
wget http://central.maven.org/maven2/org/jmxtrans/jmxtrans/270/jmxtrans-270.rpm --2019-04-10 03:18:14-- http://central.maven.org/maven2/org/jmxtrans/jmxtrans/270/jmxtrans-270.rpm Resolving central.maven.org (central.maven.org)... 151.101.40.209 Connecting to central.maven.org (central.maven.org)|151.101.40.209|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 18750744 (18M) [application/x-rpm] Saving to: ‘jmxtrans-270.rpm’ 100%[================================================================================================================================================================================>] 18,750,744 342KB/s in 43s
2019-04-10 03:18:59 (422 KB/s) - ‘jmxtrans-270.rpm’ saved [18750744/18750744]
安裝rpm包
rpm -ivh jmxtrans-270.rpm
Preparing... ################################# [100%]
Updating / installing...
1:jmxtrans-270-1 ################################# [100%]
jmxtrans相關路徑
jmxtrans安裝目錄:/usr/share/jmxtrans json文件預設目錄:/var/lib/jmxtrans/ 日誌路徑:/var/log/jmxtrans/jmxtrans.log
配置json,jmxtrans的github上有一段示例配置
{
"servers" : [ {
"port" : "1099",
"host" : "w2",
"queries" : [ {
"obj" : "java.lang:type=Memory",
"attr" : [ "HeapMemoryUsage", "NonHeapMemoryUsage" ],
"resultAlias":"jvmMemory",
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
"url" : "http://127.0.0.1:8086/",
"username" : "admin",
"password" : "admin",
"database" : "jmxDB",
"tags" : {"application" : "kafka"}
} ]
} ]
} ]
}
- host:監控伺服器
- port:jmx埠
- obj:對應jmx的ObjectName,就是我們要監控的指標
- attr:對應ObjectName的屬性,可以理解為我們要監控的指標的值
- resultAlias:對應metric 的名稱,在InfluxDB裡面就是MEASUREMENTS名
- tags:對應InfluxDB的tag功能,對與存儲在同一個MEASUREMENTS裡面的不同監控指標可以做區分,我們在用Grafana繪圖的時候會用到,建議對每個監控指標都打上tags
啟動jmxtrans
service jmxtrans start
Starting JmxTrans...
查看日誌沒有報錯即為成功
tail /var/log/jmxtrans/jmxtrans.log INFO | jvm 1 | 2019/04/10 04:44:31 | Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. INFO | jvm 1 | 2019/04/10 04:44:31 | Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. INFO | jvm 1 | 2019/04/10 04:44:31 | INFO | jvm 1 | 2019/04/10 04:44:31 | 2019-04-10 04:44:31 [WrapperSimpleAppMain] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'ServerScheduler' initialized from an externally opened InputStream. INFO | jvm 1 | 2019/04/10 04:44:31 | 2019-04-10 04:44:31 [WrapperSimpleAppMain] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 1.8.6 INFO | jvm 1 | 2019/04/10 04:44:31 | 2019-04-10 04:44:31 [WrapperSimpleAppMain] INFO org.quartz.core.QuartzScheduler - JobFactory set to: com.googlecode.jmxtrans.guice.GuiceJobFactory@23822296 2019-04-10 04:44:31 [WrapperSimpleAppMain] level com.googlecode.jmxtrans.JmxTransformer [JmxTransformer.java:177] - Starting Jmxtrans on : /var/lib/jmxtrans 2019-04-10 04:44:31 [WrapperSimpleAppMain] level org.quartz.core.QuartzScheduler [QuartzScheduler.java:519] - Scheduler ServerScheduler_$_node11554885871753 started. INFO | jvm 1 | 2019/04/10 04:44:31 | 2019-04-10 04:44:31 [WrapperSimpleAppMain] INFO c.googlecode.jmxtrans.JmxTransformer - Starting Jmxtrans on : /var/lib/jmxtrans INFO | jvm 1 | 2019/04/10 04:44:31 | 2019-04-10 04:44:31 [WrapperSimpleAppMain] INFO org.quartz.core.QuartzScheduler - Scheduler ServerScheduler_$_node11554885871753 started.
附上兩段通用的json文件
base_127.0.0.1.json
{ "servers": [{ "port": "9999", "host": "127.0.0.1", "queries": [{ "obj": "kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec", "attr": ["Count", "OneMinuteRate"], "resultAlias": "BytesInPerSec", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "application": "BytesInPerSec" } }] }, { "obj": "kafka.server:type=BrokerTopicMetrics,name=BytesOutPerSec", "attr": ["Count", "OneMinuteRate"], "resultAlias": "BytesOutPerSec", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "application": "BytesOutPerSec" } }] }, { "obj": "kafka.server:type=BrokerTopicMetrics,name=BytesRejectedPerSec", "attr": ["Count", "OneMinuteRate"], "resultAlias": "BytesRejectedPerSec", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "application": "BytesRejectedPerSec" } }] }, { "obj": "kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec", "attr": ["Count", "OneMinuteRate"], "resultAlias": "MessagesInPerSec", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "application": "MessagesInPerSec" } }] }, { "obj": "kafka.network:type=RequestMetrics,name=RequestsPerSec,request=FetchConsumer", "attr": ["Count"], "resultAlias": "RequestsPerSec", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "request": "FetchConsumer" } }] }, { "obj": "kafka.network:type=RequestMetrics,name=RequestsPerSec,request=FetchFollower", "attr": ["Count"], "resultAlias": "RequestsPerSec", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "request": "FetchFollower" } }] }, { "obj": "kafka.network:type=RequestMetrics,name=RequestsPerSec,request=Produce", "attr": ["Count"], "resultAlias": "RequestsPerSec", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "request": "Produce" } }] }, { "obj": "java.lang:type=Memory", "attr": ["HeapMemoryUsage", "NonHeapMemoryUsage"], "resultAlias": "MemoryUsage", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "application": "MemoryUsage" } }] }, { "obj": "java.lang:type=GarbageCollector,name=*", "attr": ["CollectionCount", "CollectionTime"], "resultAlias": "GC", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB", "tags": { "application": "GC" } }] }, { "obj": "java.lang:type=Threading", "attr": ["PeakThreadCount", "ThreadCount"], "resultAlias": "Thread", "outputWriters": [{ "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory", "url": "http://127.0.0.1:8086/", "username": "admin", "password": "admin", "database": "jmxDB",