1.下载jar包
[root@k8s-matser01 jmx]# wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.13.0/jmx_prometheus_javaagent-0.13.0.jar
[root@k8s-matser01 jmx]# cat prometheus-jmx-config.yaml
---
ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false
2.编辑基础镜像
[root@k8s-matser01 jmx]# cat Dockerfile
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
3.生成镜像
[root@k8s-matser01 jmx]# docker build -t tomcat:jdk8 .
4.编译yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
spec:
replicas: 3
selector:
matchLabels:
app: tomcat
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: tomcat:jdk8
env:
- name: JAVA_OPTS
value: "-javaagent:/jmx_prometheus_javaagent-0.13.0.jar=9090:/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: 9090
protocol: TCP
name: jmx-metrics
selector:
app: tomcat
4.监控增加jvm监控规则
- job_name: jvm
scrape_interval: 5s
kubernetes_sd_configs:
- role: endpoints
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