haizhilingyu 的个人知识记录

Open Source, Open Mind,
Open Sight, Open Future!

kafka简单命令实现监听消费状态

使用Prometheus+Grafana监控kafka状态

使用docker-compose安装Prometheus+Grafana

Prometheus和Grafana都提供了docker版本,所以我直接使用docker-compose一键安装Prometheus+Grafana

docker-compose.yaml文件内容如下:

version: "3.0"
services:
  kafka-exporter:
    image: danielqsj/kafka-exporter
    command:
      - '--kafka.server=node2:9092'
    extra_hosts:
      - "node2:127.0.0.1"
    ports:
      - 9308:9308
  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus:/etc/prometheus
  grafana:
    image: grafana/grafana-enterprise
    ports:
      - 3000:3000

注意kafka-exporter的kafka.server配置尽量使用大数据集群中的域名代替,避免返回域名而找不到地址

也可以使用extra_hosts添加映射记录到容器内的/etc/hosts文件中,例如我上面的配置

配置Prometheus

可以看到docker-compose中映射了当前目录下prometheus文件夹到容器中,Prometheus使用prometheus.yml文件来作为监听哪些指标的配置,在这里添加kafka的指标监听

vim ./prometheus/prometheus.yml后粘贴如下内容并保存

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

    # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'kafka'
    scrape_interval: 5s

    static_configs:
      - targets: ['kafka-exporter:9308']

配置完成后使用docker-compose up -d一键启动所有容器

配置Grafana面板

配置后的效果如下图所示:
![[Pasted image 20240326145008.png]]

后续的自定义展示就可以基于这个Json配置进行修改然后展示


标题:kafka简单命令实现监听消费状态
作者:haizhilingyu
地址:https://xiweihai.site/articles/2024/03/26/1711462675181.html