![file](https://image-static.segmentfault.com/410/017/4100175336-5d5f6247e06a2_articlex)
對於大數據集羣來說,監控功能是非常必要的,通過日誌判斷故障低效,我們需要完整的指標來幫我們管理Kafka集羣。本文討論Kafka的監控以及一些常用的第三方監控工具。
一、Kafka Monitoring
首先介紹kafka的監控原理,第三方工具也是通過這些來進行監控的,我們也可以自己去是實現監控,官網關於監控的文檔地址如下:
http://kafka.apache.org/docum...](http://kafka.apache.org/docum...
kafka使用Yammer Metrics進行監控,Yammer Metrics是一個java的監控庫。
kafka默認有很多的監控指標,默認都使用JMX接口遠程訪問,具體方法是在啓動broker和clients之前設置JMX_PORT:
JMX_PORT=9997 bin/kafka-server-start.sh config/server.properties
Kafka的每個監控指標都是以JMX MBEAN的形式定義的,MBEAN是一個被管理的資源實例。
我們可以使用Jconsole (Java Monitoring and Management Console),一種基於JMX的可視化監視、管理工具。
來可視化監控的結果:
圖2 Jconsole
隨後在Mbean下可以找到各種kafka的指標。
Mbean的命名規範是 kafka.xxx:type=xxx,xxx=xxx
主要分爲以下幾類:
(監控指標較多,這裏只截取部分,具體請查看官方文檔)
Graphing and Alerting 監控:
kafka.server爲服務器相關,kafka.network爲網絡相關。
Description | Mbean name | Normal value | ||
---|---|---|---|---|
Message in rate | kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec | |||
Byte in rate from clients | kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec | |||
Byte in rate from other brokers | kafka.server:type=BrokerTopicMetrics,name=ReplicationBytesInPerSec | |||
Request rate | kafka.network:type=RequestMetrics,name=RequestsPerSec,request={Produce\ | FetchConsumer\ | FetchFollower} | |
Error rate | kafka.network:type=RequestMetrics,name=ErrorsPerSec,request=([-.w]+),error=([-.w]+) | Number of errors in responses counted per-request-type, per-error-code. If a response contains multiple errors, all are counted. error=NONE indicates successful responses. |
Common monitoring metrics for producer/consumer/connect/streams監控:
kafka運行過程中的監控。
Metric/Attribute name | Description | Mbean name | ||||
---|---|---|---|---|---|---|
connection-close-rate | Connections closed per second in the window. | kafka.[producer\ | consumer\ | connect]:type=[producer\ | consumer\ | connect]-metrics,client-id=([-.w]+) |
connection-close-total | Total connections closed in the window. | kafka.[producer\ | consumer\ | connect]:type=[producer\ | consumer\ | connect]-metrics,client-id=([-.w]+) |
Common Per-broker metrics for producer/consumer/connect/streams監控:
每一個broker的監控。
Metric/Attribute name | Description | Mbean name | ||||
---|---|---|---|---|---|---|
outgoing-byte-rate | The average number of outgoing bytes sent per second for a node. | kafka.[producer\ | consumer\ | connect]:type=[consumer\ | producer\ | connect]-node-metrics,client-id=([-.w]+),node-id=([0-9]+) |
outgoing-byte-total | The total number of outgoing bytes sent for a node. | kafka.[producer\ | consumer\ | connect]:type=[consumer\ | producer\ | connect]-node-metrics,client-id=([-.w]+),node-id=([0-9]+) |
Producer監控:
producer調用過程中的監控。
Metric/Attribute name | Description | Mbean name |
---|---|---|
waiting-threads | The number of user threads blocked waiting for buffer memory to enqueue their records. | kafka.producer:type=producer-metrics,client-id=([-.w]+) |
buffer-total-bytes | The maximum amount of buffer memory the client can use (whether or not it is currently used). | kafka.producer:type=producer-metrics,client-id=([-.w]+) |
buffer-available-bytes | The total amount of buffer memory that is not being used (either unallocated or in the free list). | kafka.producer:type=producer-metrics,client-id=([-.w]+) |
bufferpool-wait-time | The fraction of time an appender waits for space allocation. | kafka.producer:type=producer-metrics,client-id=([-.w]+) |
Consumer監控:
consumer調用過程中的監控。
Metric/Attribute name | Description | Mbean name |
---|---|---|
commit-latency-avg | The average time taken for a commit request | kafka.consumer:type=consumer-coordinator-metrics,client-id=([-.w]+) |
commit-latency-max | The max time taken for a commit request | kafka.consumer:type=consumer-coordinator-metrics,client-id=([-.w]+) |
commit-rate | The number of commit calls per second | kafka.consumer:type=consumer-coordinator-metrics,client-id=([-.w]+) |
commit-total | The total number of commit calls | kafka.consumer:type=consumer-coordinator-metrics,client-id=([-.w]+) |
Connect監控:
Attribute name | Description | |
---|---|---|
connector-count | The number of connectors run in this worker. | |
connector-startup-attempts-total | The total number of connector startups that this worker has attempted. |
Streams 監控:
Metric/Attribute name | Description | Mbean name |
---|---|---|
commit-latency-avg | The average execution time in ms for committing, across all running tasks of this thread. | kafka.streams:type=stream-metrics,client-id=([-.w]+) |
commit-latency-max | The maximum execution time in ms for committing across all running tasks of this thread. | kafka.streams:type=stream-metrics,client-id=([-.w]+) |
poll-latency-avg | The average execution time in ms for polling, across all running tasks of this thread. | kafka.streams:type=stream-metrics,client-id=([-.w]+) |
這些指標涵蓋了我們使用kafka過程中的各種情況,還有kafka.log記錄日誌信息。每一個Mbean下都有具體的參數。
通過這些參數,比如出站進站速率,ISR變化速率,Producer端的batch大小,線程數,Consumer端的延時大小,流速等等,當然我們也要關注JVM,還有OS層面的監控,這些都有通用的工具,這裏不做贅述。
kafka的監控原理已經基本瞭解,其他第三方監控工具也大部分是在這個層面進行的完善,下面來介紹幾款主流的監控工具。
二、JmxTool
JmxTool並不是一個框架,而是Kafka默認提供的一個工具,用於實時查看JMX監控指標。。
打開終端進入到Kafka安裝目錄下,輸入命令bin/kafka-run-class.sh kafka.tools.JmxTool便可以得到JmxTool工具的幫助信息。
比如我們要監控入站速率,可以輸入命令:
bin/kafka-run-class.sh kafka.tools.JmxTool --object-name kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec --jmx-url service:jmx:rmi:///jndi/rmi://:9997/jmxrmi --date-format "YYYY-MM-dd HH:mm:ss" --attributes FifteenMinuteRate --reporting-interval 5000
BytesInPerSec的值每5秒會打印在控制檯上:
>kafka_2.12-2.0.0 rrd$ bin/kafka-run-class.sh kafka.tools.JmxTool --object-name kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec --jmx-url service:jmx:rmi:///jndi/rmi://:9997/jmxrmi --date-format "YYYY-MM-dd HH:mm:ss" --attributes FifteenMinuteRate --reporting-interval 5000
Trying to connect to JMX url: service:jmx:rmi:///jndi/rmi://:9997/jmxrmi.
"time","kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec:FifteenMinuteRate"
2018-08-10 14:52:15,784224.2587058166
2018-08-10 14:52:20,1003401.2319497257
2018-08-10 14:52:25,1125080.6160773218
2018-08-10 14:52:30,1593394.1860063889
三、Kafka-Manager
雅虎公司2015年開源的kafka監控框架,使用scala編寫。github地址如下:https://github.com/yahoo/kafk...
使用條件:
配置:conf/application.conf
kafka-manager.zkhosts="my.zookeeper.host.com:2181,other.zookeeper.host.com:2181"
部署:這裏要用到sbt部署
./sbt clean dist
啓動:
bin/kafka-manager
指定端口:
$ bin/kafka-manager -Dconfig.file=/path/to/application.conf -Dhttp.port=8080
權限:
$ bin/kafka-manager -Djava.security.auth.login.config=/path/to/my-jaas.conf
隨後訪問local host:8080
就可以看到監控頁面了:
圖 topic
圖 broker
頁面非常的簡潔,也有很多豐富的功能,開源免費,推薦使用,只是目前版本支持到Kafka 0.8.. or 0.9.. or 0.10.. or 0.11,需要特別注意。
四、kafka-monitor
linkin開源的kafka監控框架,github地址如下:https://github.com/linkedin/k...
基於 Gradle 2.0以上版本,支持java 7和java 8.
支持kafka從0.8-2.0,用戶可根據需求下載不同分支即可。
使用:
編譯:
$ git clone https://github.com/linkedin/kafka-monitor.git
$ cd kafka-monitor
$ ./gradlew jar
修改配置:config/kafka-monitor.properties
"zookeeper.connect" = "localhost:2181"
啓動:
$ ./bin/kafka-monitor-start.sh config/kafka-monitor.properties
單集羣啓動:
$ ./bin/single-cluster-monitor.sh --topic test --broker-list localhost:9092 --zookeeper localhost:2181
多集羣啓動:
$ ./bin/kafka-monitor-start.sh config/multi-cluster-monitor.properties
隨後訪問localhost:8080 看到監控頁面
圖 kafkamonitor
同時我們還可以通過http請求查詢其他指標:
curl localhost:8778/jolokia/read/kmf.services:type=produce-service,name=*/produce-availability-avg
總體來說,他的web功能比較簡單,用戶使用不多,http功能很有用,支持版本較多。
五、Kafka Offset Monitor
官網地址http://quantifind.github.io/K...
github地址 https://github.com/quantifind...
使用:下載以後執行
java -cp KafkaOffsetMonitor-assembly-0.3.0.jar:kafka-offset-monitor-another-db-reporter.jar \
com.quantifind.kafka.offsetapp.OffsetGetterWeb \
--zk zk-server1,zk-server2 \
--port 8080 \
--refresh 10.seconds \
--retain 2.days
--pluginsArgs anotherDbHost=host1,anotherDbPort=555
隨後查看localhost:8080
圖 offsetmonitor1
圖offsetmonitor2
這個項目更關注於對offset的監控,頁面很豐富,但是15年以後不再更新,無法支持最新版本kafka。繼續維護的版本地址如下https://github.com/Morningsta...。
六、Cruise-control
linkin於2017年8月開源了cruise-control框架,用於監控大規模集羣,包括一系列的運維功能,據稱在linkedin有着兩萬多臺的kafka集羣,項目還在持續更新中。
項目github地址:https://github.com/linkedin/c...
使用:
下載
git clone https://github.com/linkedin/cruise-control.git && cd cruise-control/
編譯
./gradlew jar
修改 config/cruisecontrol.properties
bootstrap.servers zookeeper.connect
啓動:
./gradlew jar copyDependantLibs
./kafka-cruise-control-start.sh [-jars PATH_TO_YOUR_JAR_1,PATH_TO_YOUR_JAR_2] config/cruisecontrol.properties [port]
啓動後訪問:
http://localhost:9090/kafkacruisecontrol/state
沒有頁面,所有都是用rest api的形式提供的。
接口列表如下:https://github.com/linkedin/c...
這個框架靈活性很大,用戶可以根據自己的情況來獲取各種指標優化自己的集羣。
七、Doctorkafka
DoctorKafka是Pinterest 開源 Kafka 集羣自愈和工作負載均衡工具。
Pinterest 是一個進行圖片分享的社交站點。他們使用 Kafka 作爲中心化的消息傳輸工具,用於數據攝取、流處理等場景。隨着用戶數量的增加,Kafka 集羣也越來越龐大,對它的管理日趨複雜,並變成了運維團隊的沉重負擔,因此他們研發了 Kafka 集羣自愈和工作負載均衡工具 DoctorKafka,最近他們已經在 GitHub 上將該項目開源。
使用:
下載:
git clone [git-repo-url] doctorkafka
cd doctorkafka
編譯:
mvn package -pl kafkastats -am
啓動:
java -server \
-Dlog4j.configurationFile=file:./log4j2.xml \
-cp lib/*:kafkastats-0.2.4.8.jar \
com.pinterest.doctorkafka.stats.KafkaStatsMain \
-broker 127.0.0.1 \
-jmxport 9999 \
-topic brokerstats \
-zookeeper zookeeper001:2181/cluster1 \
-uptimeinseconds 3600 \
-pollingintervalinseconds 60 \
-ostrichport 2051 \
-tsdhostport localhost:18126 \
-kafka_config /etc/kafka/server.properties \
-producer_config /etc/kafka/producer.properties \
-primary_network_ifacename eth0
頁面如下:
圖dockerkafka
DoctorKafka 在啓動之後,會階段性地檢查每個集羣的狀態。當探測到 broker 出現故障時,它會將故障 broker 的工作負載轉移給有足夠帶寬的 broker。如果在集羣中沒有足夠的資源進行重分配的話,它會發出告警。屬於一個自動維護集羣健康的框架。
八、Burrow
Burrow是LinkedIn開源的一款專門監控consumer lag的框架。
github地址如下:https://github.com/linkedin/B...
使用Burrow監控kafka, 不需要預先設置lag的閾值, 他完全是基於消費過程的動態評估
Burrow支持讀取kafka topic和,zookeeper兩種方式的offset,對於新老版本kafka都可以很好支持
Burrow支持http, email類型的報警
Burrow默認只提供HTTP接口(HTTP endpoint),數據爲json格式,沒有web UI。
安裝使用:
$ Clone github.com/linkedin/Burrow to a directory outside of $GOPATH. Alternatively, you can export GO111MODULE=on to enable Go module.
$ cd to the source directory.
$ go mod tidy
$ go install
示例:
列出所有監控的Kafka集羣
curl -s http://localhost:8000/v3/kafka |jq
{
"error": false,
"message": "cluster list returned",
"clusters": [
"kafka",
"kafka"
],
"request": {
"url": "/v3/kafka",
"host": "kafka"
}
}
其他的框架,還有kafka-web-console:https://github.com/claudemamo...
kafkat:https://github.com/airbnb/kafkat
capillary:https://github.com/keenlabs/c...
chaperone:https://github.com/uber/chape...
還有很多,但是我們要結合自己的kafka版本情況進行選擇。
更多實時計算,Kafka等相關技術博文,歡迎關注實時流式計算
baidu.com/resource/2125883ce7fb457d6a4e201566531933.png)baidu.com/resource/21270384fea13d634305301566531915.png)