在linux节点上安装kafka exporter随记

来自三线的随记

Kafka exporter for Prometheus.

For other metrics from Kafka, have a look at the JMX exporter.


binary file github repository and documentation

https://github.com/danielqsj/kafka_exporter

下載 kafka_exporter 二進制文件壓縮包

在節點上執行命令下載

wget "https://github.com/danielqsj/kafka_exporter/releases/download/v1.2.0/kafka_exporter-1.2.0.linux-amd64.tar.gz" 

解壓壓縮包

[root@master kafka]# ls
kafka_exporter-1.2.0.linux-amd64.tar.gz
[root@master kafka]# tar -xzf kafka_exporter-1.2.0.linux-amd64.tar.gz
[root@master kafka]# ls
kafka_exporter-1.2.0.linux-amd64  kafka_exporter-1.2.0.linux-amd64.tar.gz

將 kafka_exporter-1.2.0.linux-amd64 目錄下的 kafka_exporter 二進制文件複製到 /usr/local/bin 路徑下

注意需要給予該二進制文件nobody用戶可執行權限

[root@master kafka]# cd kafka_exporter-1.2.0.linux-amd64/
[root@master kafka_exporter-1.2.0.linux-amd64]# ls
kafka_exporter  LICENSE
[root@master kafka_exporter-1.2.0.linux-amd64]# cp kafka_exporter /usr/local/bin/
[root@master kafka_exporter-1.2.0.linux-amd64]# ls -l /usr/local/bin/kafka_exporter
-rwxr-xr-x. 1 root root 13578776 Oct 23 11:57 /usr/local/bin/kafka_exporter

創建systemd service文件

根據實際情況修改其中的kafka.server配置值(kafka實例監聽地址)

修改後直接複製下文全部文字命令並在節點上執行即可

cat <<EOF > /etc/systemd/system/kafka_exporter.service
[Unit]
Description=kafka_exporter
After=network.target 

[Service]
User=nobody
Group=nobody
Type=simple
ExecStart=/usr/local/bin/kafka_exporter\\
          --kafka.server=127.0.0.1:9092\\
          --web.listen-address=:9308

[Install]
WantedBy=multi-user.target
EOF

重載系統 systemd 配置

執行命令 systemctl daemon-reload

啟動服務並且設置服務自啟

執行命令 systemctl enable --now kafka_exporter

[root@master kafka_exporter-1.2.0.linux-amd64]# systemctl enable --now kafka_exporter
Created symlink from /etc/systemd/system/multi-user.target.wants/kafka_exporter.service to /etc/systemd/system/kafka_exporter.service.

查看服務運行狀態

執行命令systemctl status kafka_exporter

能夠得到類似下文的回顯結果

主要看Active屬性值為active(running)

以及日誌中提示Listening on :9308就好了

[root@master kafka_exporter-1.2.0.linux-amd64]# systemctl status kafka_exporter
● kafka_exporter.service - kafka_exporter
   Loaded: loaded (/etc/systemd/system/kafka_exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-10-23 15:15:54 CST; 19s ago
 Main PID: 16549 (kafka_exporter)
    Tasks: 6
   Memory: 1.3M
   CGroup: /system.slice/kafka_exporter.service
           └─16549 /usr/local/bin/kafka_exporter --kafka.server=127.0.0.1:9092 --web.listen-address=:9308

Oct 23 15:15:54 master systemd[1]: Started kafka_exporter.
Oct 23 15:15:54 master kafka_exporter[16549]: time="2020-10-23T15:15:54+08:00" level=info msg="Starting kafka_exporter (version=1.2.0, branch=HEAD, revision=830660212e6c109e69dcb1cb58f5159fe3...porter.go:474"
Oct 23 15:15:54 master kafka_exporter[16549]: time="2020-10-23T15:15:54+08:00" level=info msg="Build context (go=go1.10.3, user=root@981cde178ac4, date=20180707-14:34:48)" source="kafka_exporter.go:475"
Oct 23 15:15:54 master kafka_exporter[16549]: time="2020-10-23T15:15:54+08:00" level=info msg="Done Init Clients" source="kafka_exporter.go:213"
Oct 23 15:15:54 master kafka_exporter[16549]: time="2020-10-23T15:15:54+08:00" level=info msg="Listening on :9308" source="kafka_exporter.go:499"
Hint: Some lines were ellipsized, use -l to show in full.

測試接口

在節點上使用命令curl -s {{節點IP}}:9308/metrics | grep kafka_brokers訪問接口

能夠得到類似下文結果(kafka_brokers 值非0)

[root@master kafka_exporter-1.2.0.linux-amd64]# curl  -s 192.168.150.166:9308/metrics | grep kafka_brokers
# HELP kafka_brokers Number of Brokers in the Kafka Cluster.
# TYPE kafka_brokers gauge
kafka_brokers 1

或直接使用瀏覽器訪問{{節點IP}}:9308/metrics能夠看到相關 kafka 實例指標信息則為正常