鎵嬫妸鎵嬫暀浣犱娇鐢?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 鎻愪緵浜嗕袱绉嶇敤娉?
- 鍚姩鐙珛杩涚▼銆侸VM 鍚姩鏃舵寚瀹氬弬鏁帮紝鏆撮湶 JMX 鐨?RMI 鎺ュ彛锛孞MX-Exporter 璋冪敤 RMI 鑾峰彇 JVM 杩愯鏃剁姸鎬佹暟鎹紝杞崲涓?Prometheus metrics 鏍煎紡锛屽苟鏆撮湶绔彛璁?Prometheus 閲囬泦銆?/li>
- 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 锛屽彲浠ョ洿鎺ュ鍏ワ紝闈㈡澘鏁堟灉鍥?
鍙傝€冭祫鏂?/h2>
- JMX Exporter 椤圭洰鍦板潃: https://github.com/prometheus/jmx_exporter
- JVM 鐩戞帶闈㈡澘: https://grafana.com/grafana/dashboards/8563
以上是关于鎵嬫妸鎵嬫暀浣犱娇鐢?Prometheus 鐩戞帶 JVM的主要内容,如果未能解决你的问题,请参考以下文章
[娣卞叆娴呭嚭LB]鎵嬫妸鎵嬪甫浣犲疄鐜颁竴涓礋杞藉潎琛″櫒