步骤
1.下载jvm exporter jar包
wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.18.0/jmx_prometheus_javaagent-0.18.0.jar
2.创建Dockerfile
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.18.0/jmx_prometheus_javaagent-0.18.0.jar
FROM tomcat:jdk8-openjdk-slim
ADD prometheus-jmx-config.yaml /prometheus-jmx-config.yaml
COPY --from=jar /jmx_prometheus_javaagent-0.18.0.jar /jmx_prometheus_javaagent-0.18.0.jar
3.创建jvm export配置文件
cat prometheus-jmx-config.yaml
---
lowercaseOutputLabelNames: true
lowercaseOutputName: true
whitelistObjectNames: ["java.lang:type=OperatingSystem"]
blacklistObjectNames: []
rules:
- pattern: 'java.lang<type=OperatingSystem><>(committed_virtual_memory|free_physical_memory|free_swap_space|total_physical_memory|total_swap_space)_size:'
name: os_$1_bytes
labels: {}
type: GAUGE
attrNameSnakeCase: true
- pattern: 'java.lang<type=OperatingSystem><>((?!process_cpu_time)\w+):'
name: os_$1
labels: {}
type: GAUGE
attrNameSnakeCase: true
4.构建镜像
docker build -t jvm-exporter:1.0 .
5.创建deployment,svc
部署到监控的namespace,也可以部署到其他namespace,但是要注意有没有权限获取对应namespace中的资源。
cat jvm-exporter-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: jvm-exporter
spec:
replicas: 1
selector:
matchLabels:
app: jvm-exporter
name: jvm-exporter
template:
metadata:
labels:
app: jvm-exporter
name: jvm-exporter
spec:
containers:
- name: jvm-exporter
image: registry-hz.rubikstack.com/library/jvm-exporter:1.0
env:
- name: JAVA_OPTS
value: "-javaagent:/jmx_prometheus_javaagent-0.18.0.jar=8088:/prometheus-jmx-config.yaml"
---
apiVersion: v1
kind: Service
metadata:
name: jvm-exporter
labels:
app: jvm-exporter
name: jvm-exporter
spec:
type: ClusterIP
ports:
- port: 8080
protocol: TCP
name: http
- port: 8088
protocol: TCP
name: jmx-metrics
selector:
app: jvm-exporter
name: jvm-exporter
6.创建ServiceMonitor
根据实际环境中修改。
cat servicemonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: jvm-exporter
namespace: cattle-monitoring-system
labels:
app: jvm-exporter
name: jvm-exporter
spec:
endpoints:
- port: jmx-metrics
interval: 5s
path: /metrics
namespaceSelector:
matchNames:
- cattle-monitoring-system
selector:
matchLabels:
app: jvm-exporter
name: jvm-exporter
7.grafana导入模板
使用的模板id是8563。输入job name:jvm-exporter。
也可以用12856。