鎵嬫妸鎵嬫暀浣犱娇鐢?Prometheus 鐩戞帶 JVM

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎵嬫妸鎵嬫暀浣犱娇鐢?Prometheus 鐩戞帶 JVM相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%ae%b9%e5%99%a8%e5%8c%96' title='瀹瑰櫒鍖?>瀹瑰櫒鍖?/a>   鍦板潃   绀轰緥   RoCE   杩涚▼   mat   ima   yam   oar   

姒傝堪

褰撲綘鐨?Java 涓氬姟瀹瑰櫒鍖栦笂 K8S 鍚庯紝濡傛灉瀵瑰叾杩涜鐩戞帶鍛紵Prometheus 绀惧尯寮€鍙戜簡 JMX Exporter 鏉ュ鍑?JVM 鐨勭洃鎺ф寚鏍囷紝浠ヤ究浣跨敤 Prometheus 鏉ラ噰闆嗙洃鎺ф暟鎹€傛湰鏂囧皢浠嬬粛濡備綍鍒╃敤 Prometheus 涓?JMX Exporter 鏉ョ洃鎺т綘 Java 搴旂敤鐨?JVM銆?/p>

浠€涔堟槸 JMX Exporter ?

JMX Exporter 鍒╃敤 Java 鐨?JMX 鏈哄埗鏉ヨ鍙?JVM 杩愯鏃剁殑涓€浜涚洃鎺ф暟鎹紝鐒跺悗灏嗗叾杞崲涓?Prometheus 鎵€璁ょ煡鐨?metrics 鏍煎紡锛屼互渚胯 Prometheus 瀵瑰叾杩涜鐩戞帶閲囬泦銆?/p>

閭d箞锛孞MX 鍙堟槸浠€涔堝憿锛熷畠鐨勫叏绉版槸锛?code>Java Management Extensions銆?椤惧悕鎬濅箟锛屾槸绠$悊 Java 鐨勪竴绉嶆墿灞曟鏋讹紝JMX Exporter 姝f槸鍩轰簬姝ゆ鏋舵潵璇诲彇 JVM 鐨勮繍琛屾椂鐘舵€佺殑銆?/p>

濡備綍浣跨敤 JMX Exporter 鏆撮湶 JVM 鐩戞帶鎸囨爣 ?

涓嬮潰浠嬬粛濡備綍閫氳繃 JMX Exporter 鏉ユ毚闇?Java 搴旂敤鐨?JVM 鐩戞帶鎸囨爣銆?/p>

JMX Exporter 鐨勪袱绉嶇敤娉?/h3>

JMX-Exporter 鎻愪緵浜嗕袱绉嶇敤娉?

  1. 鍚姩鐙珛杩涚▼銆侸VM 鍚姩鏃舵寚瀹氬弬鏁帮紝鏆撮湶 JMX 鐨?RMI 鎺ュ彛锛孞MX-Exporter 璋冪敤 RMI 鑾峰彇 JVM 杩愯鏃剁姸鎬佹暟鎹紝杞崲涓?Prometheus metrics 鏍煎紡锛屽苟鏆撮湶绔彛璁?Prometheus 閲囬泦銆?/li>
  2. JVM 杩涚▼鍐呭惎鍔?in-process)銆侸VM 鍚姩鏃舵寚瀹氬弬鏁帮紝閫氳繃 javaagent 鐨勫舰寮忚繍琛?JMX-Exporter 鐨?jar 鍖咃紝杩涚▼鍐呰鍙?JVM 杩愯鏃剁姸鎬佹暟鎹紝杞崲涓?Prometheus metrics 鏍煎紡锛屽苟鏆撮湶绔彛璁?Prometheus 閲囬泦銆?/li>

瀹樻柟涓嶆帹鑽愪娇鐢ㄧ涓€绉嶆柟寮忥紝涓€鏂归潰閰嶇疆澶嶆潅锛屽彟涓€鏂归潰鍥犱负瀹冮渶瑕佷竴涓崟鐙殑杩涚▼锛岃€岃繖涓繘绋嬫湰韬殑鐩戞帶鍙堟垚浜嗘柊鐨勯棶棰橈紝鎵€浠ユ湰鏂囬噸鐐瑰洿缁曠浜岀鐢ㄦ硶璁插浣曞湪 K8S 鐜涓嬩娇鐢?JMX Exporter 鏆撮湶 JVM 鐩戞帶鎸囨爣銆?/p>

鎵撳寘闀滃儚

浣跨敤绗簩绉嶇敤娉曪紝鍚姩 JVM 鏃堕渶瑕佹寚瀹?JMX Exporter 鐨?jar 鍖呮枃浠跺拰閰嶇疆鏂囦欢銆俲ar 鍖呮槸浜岃繘鍒舵枃浠讹紝涓嶅ソ閫氳繃 configmap 鎸傝浇锛岄厤缃枃浠舵垜浠嚑涔庝笉闇€瑕佷慨鏀癸紝鎵€浠ュ缓璁槸鐩存帴灏?JMX Exporter 鐨?jar 鍖呭拰閰嶇疆鏂囦欢閮芥墦鍖呭埌涓氬姟瀹瑰櫒闀滃儚涓€?/p>

棣栧厛鍑嗗涓€涓埗浣滈暅鍍忕殑鐩綍锛屾斁鍏?JMX Exporter 閰嶇疆鏂囦欢 prometheus-jmx-config.yaml:

---
ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false

娉?/strong>: 鏇村閰嶇疆椤硅鍙傝€冨畼鏂规枃妗c€?/p>

鐒跺悗鍑嗗 jar 鍖呮枃浠讹紝鍙互鍦?jmx_exporter 鐨?Github 椤甸潰鎵惧埌鏈€鏂扮殑 jar 鍖呬笅杞藉湴鍧€锛屼笅杞藉埌褰撳墠鐩綍:

wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.13.0/jmx_prometheus_javaagent-0.13.0.jar

鍐嶅噯澶?Dockerfile锛岃繖閲屼互 tomcat 涓轰緥:

FROM tomcat:jdk8-openjdk-slim
ADD prometheus-jmx-config.yaml /prometheus-jmx-config.yaml
ADD jmx_prometheus_javaagent-0.13.0.jar /jmx_prometheus_javaagent-0.13.0.jar

鏈€鍚庣紪璇戦暅鍍?

docker build . -t ccr.ccs.tencentyun.com/imroc/tomcat:jdk8

鎼炲畾锛佸鏋滄兂瑕佹洿绠€鍗曪紝鍙互鍒╃敤 docker 澶氶樁娈垫瀯寤猴紝鐪佹帀鎵嬪姩涓嬭浇 jar 鍖呯殑姝ラ锛孌ockerfile 绀轰緥:

FROM ubuntu:16.04 as jar
WORKDIR /
RUN apt-get update -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y wget
RUN wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.13.0/jmx_prometheus_javaagent-0.13.0.jar

FROM tomcat:jdk8-openjdk-slim
ADD prometheus-jmx-config.yaml /prometheus-jmx-config.yaml
COPY --from=jar /jmx_prometheus_javaagent-0.13.0.jar /jmx_prometheus_javaagent-0.13.0.jar

閮ㄧ讲 Java 搴旂敤

鏈変簡鎵撳寘濂界殑闀滃儚锛屼笅涓€姝ユ垜浠湅涓嬪浣曢儴缃插簲鐢ㄥ埌 K8S锛屽叧閿偣鍦ㄤ簬濡備綍淇敼 JVM 鍚姩鍙傛暟浠ヤ究鍚姩鏃跺姞杞?JMX Exporter銆侸VM 鍚姩鏃朵細璇诲彇 JAVA_OPTS 鐜鍙橀噺锛屼綔涓洪澶栫殑鍚姩鍙傛暟锛屾墍浠ユ垜浠儴缃叉椂鍙互涓哄簲鐢ㄥ鍔犱竴涓嬭繖涓幆澧冨彉閲忥紝绀轰緥锛?/p>

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat
        image: ccr.ccs.tencentyun.com/imroc/tomcat:jdk8
        env:
        - name: JAVA_OPTS
          value: "-javaagent:/jmx_prometheus_javaagent-0.13.0.jar=8088:/prometheus-jmx-config.yaml"

---
apiVersion: v1
kind: Service
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  type: ClusterIP
  ports:
  - port: 8080
    protocol: TCP
    name: http
  - port: 8088
    protocol: TCP
    name: jmx-metrics
  selector:
    app: tomcat
  • 鍚姩鍙傛暟鏍煎紡: -javaagent:<jar>=<port>:<config>銆?/li>
  • 杩欓噷浣跨敤浜?8088 绔彛鏉ユ毚闇?JVM 鐨勭洃鎺ф寚鏍囷紝鍙嚜琛屾洿鏀广€?/li>

娣诲姞 Prometheus 鐩戞帶閰嶇疆

鏆撮湶浜?JVM 鐨勭洃鎺ф寚鏍囷紝鐜板湪鏉ラ厤缃笅 Prometheus锛岃鐩戞帶鏁版嵁琚噰闆嗗埌锛岄厤缃ず渚?

    - job_name: tomcat
      scrape_interval: 5s
      kubernetes_sd_configs:
      - role: endpoints
        namespaces:
          names:
          - default
      relabel_configs:
      - action: keep
        source_labels:
        - __meta_kubernetes_service_label_app
        regex: tomcat
      - action: keep
        source_labels:
        - __meta_kubernetes_endpoint_port_name
        regex: jmx-metrics

濡傛灉鏄畨瑁呬簡 prometheus-operator锛屼篃鍙互閫氳繃鍒涘缓 ServiceMonitor 鐨?CRD 瀵硅薄鏉ラ厤缃?Prometheus:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: tomcat
  namespace: default
  labels:
    app: tomcat
spec:
  endpoints:
  - port: jmx-metrics
    interval: 5s
  namespaceSelector:
    matchNames:
    - default
  selector:
    matchLabels:
      app: tomcat

娣诲姞 Grafana 鐩戞帶闈㈡澘

閲囬泦鍒颁簡鏁版嵁锛屼笅闈㈢湅涓嬪浣曞睍绀烘暟鎹紝濡傛灉鐔熸倝 Prometheus 鍜?Grafana锛屽彲鑷鏍规嵁鎸囨爣璁捐闇€瑕佺殑闈㈡澘锛岀ぞ鍖轰篃鏈夋彁渚涚幇鎴愮殑锛屼笉杩囬兘鏈変簺鑰佷簡锛岄儴鍒嗚鍥惧彲鑳藉睍绀轰笉鍑烘潵锛岀敤鐨勬瘮杈冨鐨勬槸 https://grafana.com/grafana/dashboards/8563 锛屽彲浠ョ洿鎺ュ鍏ワ紝闈㈡澘鏁堟灉鍥?

鎶€鏈浘鐗? src=

鍙傝€冭祫鏂?/h2>

以上是关于鎵嬫妸鎵嬫暀浣犱娇鐢?Prometheus 鐩戞帶 JVM的主要内容,如果未能解决你的问题,请参考以下文章

鎵嬫妸鎵嬫暀浣犲湪Windows鐜涓嬪崌绾

绉嬪悗涓€棰楃硸锛屾暀浣犱娇鐢╯ekiro jsrpc

[娣卞叆娴呭嚭LB]鎵嬫妸鎵嬪甫浣犲疄鐜颁竴涓礋杞藉潎琛″櫒

鎵挎帴cardboard澶栧寘锛寀nity3d澶栧寘锛堝寳浜姩杞€?璋锋瓕CARDBOARD鐪熷己澶э級