對於大數據集羣來講,監控功能是很是必要的,經過日誌判斷故障低效,咱們須要完整的指標來幫咱們管理Kafka集羣。本文討論Kafka的監控以及一些經常使用的第三方監控工具。html
首先介紹kafka的監控原理,第三方工具也是經過這些來進行監控的,咱們也能夠本身去是實現監控,官網關於監控的文檔地址以下:java
kafka.apache.org/documentati…](kafka.apache.org/documentati…)node
kafka使用Yammer Metrics進行監控,Yammer Metrics是一個java的監控庫。git
kafka默認有不少的監控指標,默認都使用JMX接口遠程訪問,具體方法是在啓動broker和clients以前設置JMX_PORT:github
JMX_PORT=9997 bin/kafka-server-start.sh config/server.properties
複製代碼
Kafka的每一個監控指標都是以JMX MBEAN的形式定義的,MBEAN是一個被管理的資源實例。web
咱們可使用Jconsole (Java Monitoring and Management Console),一種基於JMX的可視化監視、管理工具。docker
來可視化監控的結果:apache
圖2 Jconsolejson
隨後在Mbean下能夠找到各類kafka的指標。bootstrap
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並非一個框架,而是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
複製代碼
雅虎公司2015年開源的kafka監控框架,使用scala編寫。github地址以下:github.com/yahoo/kafka…
使用條件:
配置: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,須要特別注意。
linkin開源的kafka監控框架,github地址以下:github.com/linkedin/ka…
基於 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功能頗有用,支持版本較多。
官網地址http://quantifind.github.io/KafkaOffsetMonitor/
github地址 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/Morningstar/kafka-offset-monitor。
linkin於2017年8月開源了cruise-control框架,用於監控大規模集羣,包括一系列的運維功能,據稱在linkedin有着兩萬多臺的kafka集羣,項目還在持續更新中。
項目github地址:github.com/linkedin/cr…
使用:
下載
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的形式提供的。
接口列表以下:github.com/linkedin/cr…
這個框架靈活性很大,用戶能夠根據本身的狀況來獲取各類指標優化本身的集羣。
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是LinkedIn開源的一款專門監控consumer lag的框架。
github地址以下:github.com/linkedin/Bu…
使用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:github.com/claudemamo/…
kafkat:github.com/airbnb/kafk…
capillary:github.com/keenlabs/ca…
chaperone:github.com/uber/chaper…
還有不少,可是咱們要結合本身的kafka版本狀況進行選擇。
更多實時計算,Kafka等相關技術博文,歡迎關注實時流式計算