在k8s环境中我们有很多的java应用,需要监控其性能我们可以通过集成jmx的客户端,然后通过promethues的自动发现来实现监控数据的获取。
promethues的部署he相关配置可参考:promethues部署
jmx_prometheus_javaagent-0.17.2.jar(版本自定义,可以直接打包到镜像里)
使用configmap挂载jmx-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-jmx-config
namespace: monitor
data:
prometheus-jmx-config.yaml: |
lowercaseOutputLabelNames: true
lowercaseOutputName: true
whitelistObjectNames: ["java.lang:type=OperatingSystem"]
blacklistObjectNames: []
rules:
- pattern: 'java.lang<>(committed_virtual_memory|free_physical_memory|free_swap_space|total_physical_memory|total_swap_space)_size:'
name: os_$1_bytes
type: GAUGE
attrNameSnakeCase: true
- pattern: 'java.lang<>((?!process_cpu_time)\w+):'
name: os_$1
type: GAUGE
attrNameSnakeCase: true
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
namespace: monitor
spec:
replicas: 2
selector:
matchLabels:
app: tomcat
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: tomcat:jdk8-openjdk-slim
env:
- name: JAVA_OPTS
value: "-javaagent:/jmx_prometheus_javaagent-0.17.2.jar=8088:/jmx/jmx-config.yaml" **# (添加的启动命令参数)**
resources:
requests:
cpu: 300m
memory: 512Mi
limits:
cpu: 500m
memory: 1024Mi
volumeMounts:
- mountPath: "/jmx_prometheus_javaagent-0.17.2.jar" **#(将客户端挂载到容器)**
name: jmx-prometheus-javaagent
- mountPath: "/jmx"
name: prometheus-jmx-config **#(通过configmap挂载配置文件)**
volumes:
- name: jmx-prometheus-javaagent
hostPath:
path: /data/k8s_yaml/jvm/jmx_prometheus_javaagent-0.17.2.jar **# (使用的是挂在本地路径)**
- configMap:
name: prometheus-jmx-config
name: prometheus-jmx-config
nodeSelector:
kubernetes.io/hostname: 10.1.40.121
apiVersion: v1
kind: Service
metadata:
name: tomcat
namespace: monitor
labels:
app: tomcat
annotations:
prometheus.io/port: "8088" **#(jmx监听的端口)**
prometheus.io/jvm: "true"
spec:
type: NodePort
selector:
app: tomcat
ports:
- name: http
port: 8080
protocol: TCP
- name: jmx-metrics
port: 8088
protocol: TCP
jvm面板 10519参考