全部產品
Search
文件中心

Managed Service for Prometheus:如何部署和配置Kafka JMX Agent

更新時間:Sep 10, 2026

本文介紹如何部署和配置Kafka JMX Agent。

使用限制

目前支援在Container Service類型的Prometheus執行個體和ECS類型的Prometheus執行個體中接入Kafka JMX Agent。

步驟一:部署Kafka JMX Agent

  1. 下載kafka JMX-Agent包到Kafka所在的Pod或ECS內。

  2. 在Kafka Producer、Broker、Consumer的JVM啟動參數中,增加參數-javaagent:/{jmx-agent儲存目錄}/kafka-jmx_prometheus_javaagent-1.18.1.jar={jmx監聽連接埠},具體參數樣本如下。

    • 儲存目錄:需要替換為您實際的儲存目錄。

    • jmx監聽連接埠:需要替換為您實際的JMX監聽連接埠。

    
    #ps -ef|grep javaagent
    admin    30038  30036 63 10:44 ?    00:00:14 /opt/taobao/java/bin/java -javaagent:/kafka-jmx_prometheus_javaagent-1_18_1.jar=5556 -Xmx4g -Xms4g -Xmn2g -Xss1024k -server -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:MaxGCPauseMillis=20 -XX:G1ReservePercent=25 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8 -XX:InitiatingHeapOccupancyPercent=30 -XX:+DisableExplicitGC -Djava.awt.headless=true -XX:OmitStackTraceInFastThrow -XX:+AlwaysPreTouch -XX:MaxDirectMemorySize=15g -XX:-UseLargePages -XX:-UseBiasedLocking -Dsun.nio.PageAlignDirectMemory=true -verbose:gc -Xloggc:/home/admin/logs/kafka_gc_%p.log -XX:ErrorFile=/home/admin/logs/hs_err_pid_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy -XX:+HeapDumpOnOutOfMemoryError -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m ...
    

    執行 ps -ef|grep javaagent 命令,確認輸出中包含 -javaagent:/kafka-jmx_prometheus_javaagent-1_18_1.jar=5556,說明 Kafka JMX Agent 已成功載入並監聽連接埠 5556。

  3. 重新啟動Kafka Producer、Broker、Consumer。

  4. 確認Kafka Producer、Broker、Consumer的Kafka JMX Agent是否正常工作。即在各Pod或ECS上,執行命令curl localhost:{jmx監聽連接埠}/metrics,觀察是否有正常的Metric返回。如果有正常的Metric返回,說明Kafka Producer、Broker、Consumer的Kafka JMX Agent已正常工作。

    jmx監聽連接埠:需要替換為您實際的JMX監聽連接埠。

    
    [root@kaikxxx ~]# curl localhost:5556/metrics
    # HELP jvm_memory_pool_allocated_bytes_total Total bytes allocated in a given JVM memory pool. Only updated after GC, not continuously.
    # TYPE jvm_memory_pool_allocated_bytes_total counter
    jvm_memory_pool_allocated_bytes_total{pool="G1 Old Gen",} 1.7518xxx
    jvm_memory_pool_allocated_bytes_total{pool="Code Cache",} 1.4520xxx
    jvm_memory_pool_allocated_bytes_total{pool="G1 Eden Space",} 1.4xxx
    jvm_memory_pool_allocated_bytes_total{pool="G1 Survivor Space",}
    jvm_memory_pool_allocated_bytes_total{pool="Compressed Class Space",} 5352720.0
    jvm_memory_pool_allocated_bytes_total{pool="Metaspace",} 4.xxx
    # HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.
    # TYPE jvm_gc_collection_seconds summary
    jvm_gc_collection_seconds_count{gc="G1 Young Generation",} 21.0
    jvm_gc_collection_seconds_sum{gc="G1 Young Generation",} 0.346
    jvm_gc_collection_seconds_count{gc="G1 Old Generation",} 0.0
    jvm_gc_collection_seconds_sum{gc="G1 Old Generation",} 0.0
    # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
    # TYPE process_cpu_seconds_total counter
    process_cpu_seconds_total 22.85
    # HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
    # TYPE process_start_time_seconds gauge
    process_start_time_seconds 1.6654xxx
    # HELP process_open_fds Number of open file descriptors.
    # TYPE process_open_fds gauge
    process_open_fds 204.0
    # HELP process_max_fds Maximum number of open file descriptors.
    # TYPE process_max_fds gauge
    process_max_fds 65535.0
    # HELP process_virtual_memory_bytes Virtual memory size in bytes.
    # TYPE process_virtual_memory_bytes gauge
    process_virtual_memory_bytes 4.428xxx
    # HELP process_resident_memory_bytes Resident memory size in bytes.
    # TYPE process_resident_memory_bytes gauge
    process_resident_memory_bytes 9.36xxx
    # HELP jmx_config_reload_failure_total Number of times configuration have failed to be reloaded.
    # TYPE jmx_config_reload_failure_total counter
    jmx_config_reload_failure_total 0.0
    # HELP jmx_exporter_build_info A metric with a constant '1' value labeled with the version of the JMX exporter.
    # TYPE jmx_exporter_build_info gauge
    jmx_exporter_build_info{version="0.17.1-SNAPSHOT",name="jmx_prometheus_javaagent",} 1.0
    # HELP jvm_memory_objects_pending_finalization The number of objects waiting in the finalizer queue.
    # TYPE jvm_memory_objects_pending_finalization gauge
    jvm_memory_objects_pending_finalization 0.0
    # HELP jvm_memory_bytes_used Used bytes of a given JVM memory area.
    # TYPE jvm_memory_bytes_used gauge
    jvm_memory_bytes_used{area="heap",} 4.0462xxx
    jvm_memory_bytes_used{area="nonheap",} 6.57xxx
    # HELP jvm_memory_bytes_committed Committed (bytes) of a given JVM memory area.
    # TYPE jvm_memory_bytes_committed gauge
    jvm_memory_bytes_committed{area="heap",} 1.07374xxx
    jvm_memory_bytes_committed{area="nonheap",} 7.0xxx
    # HELP jvm_memory_bytes_max Max (bytes) of a given JVM memory area.
    # TYPE jvm_memory_bytes_max gauge
    jvm_memory_bytes_max{area="heap",} 1.0737xxx
    jvm_memory_bytes_max{area="nonheap",} -1.0
    # HELP jvm_memory_bytes_init Initial bytes of a given JVM memory area.
    # TYPE jvm_memory_bytes_init gauge
    jvm_memory_bytes_init{area="heap",} 1.0737xxx
    jvm_memory_bytes_init{area="nonheap",} 255xxx
    # HELP jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool.
    

(可選)步驟二:配置containerPort

若您是Container Service類型的Prometheus執行個體,您還需要配置containerPort,以便可觀測監控 Prometheus 版能順利抓取到Kafka JMX Agent的資料。若您是ECS類型的Prometheus執行個體,請跳過此步驟。

  1. 登入Container Service管理主控台,在左側導覽列選擇叢集列表。

  2. 在叢集列表頁面,單擊目的地組群名稱,然後在左側導覽列,選擇工作負載 > 無狀態。

  3. 單擊目標工作負載操作列的編輯,在編輯頁面,配置連接埠名稱、容器連接埠和連接埠協議,然後單擊更新。

    • 名稱:Kafka JMX Agent連接埠名,例如app1-kafka-jmx。

    • 容器連接埠:在步驟一:部署Kafka JMX Agent中定義的JMX監聽連接埠。

    • 協議:選擇TCP協議。

步驟三:配置Kafka Pod或ECS的標籤

可觀測監控 Prometheus 版可以通過識別Pod或ECS的標籤實現服務發現(Service Discovery),因此您需要定義一個標籤,以便可觀測監控 Prometheus 版能夠識別到。如果您的業務中已經存在類似的標籤,則可以跳過此步驟。

Container Service類型的Prometheus執行個體配置標籤

在Kafka Producer、Broker、Consumer的Pod上配置標籤{自訂標籤}:{自訂標籤值},例如:arms-kafka-exporter:my-kafka1,具體操作如下:

  1. 登入Container Service管理主控台,在左側導覽列選擇叢集列表。

  2. 在叢集列表頁面,單擊目的地組群名稱,然後在左側導覽列,選擇工作負載 > 容器組。

  3. 在容器組頁簽,單擊目標容器組操作列的編輯,然後新增標籤,編輯結束後單擊更新。

    
    apiVersion: v1
    kind: Pod
    metadata:
      annotations:
        k8s.aliyun.com/pod-ips: 10.119.6x.xxx
      creationTimestamp: '2025-01-10T05:58:06Z'
      generateName: nginx-deployment-basic-8f9xxx    t-
      labels:
        app: nginx
        pod-template-hash: 8f95d5bd4
      managedFields:
      - apiVersion: v1
        fieldsType: FieldsV1
        fieldsV1:
          'f:metadata':
    

    其中 labels 欄位包含 app: nginx 和 pod-template-hash: 8f95d5bd4,即為新增的標籤內容。

ECS類型的Prometheus執行個體配置標籤

在Kafka Producer、Broker、Consumer的ECS上配置標籤{自訂標籤}:{自訂標籤值},例如:arms-kafka-exporter:my-kafka1,ECS建立標籤的具體操作,請參見標籤。

後續操作

Kafka JMX-Agent配置完成後,您可以在可觀測監控 Prometheus 版控制台完成接入配置並查看其監控資料。具體操作,請參見如何使用Prometheus監控Kafka。