Prometheus jmx_exporter
一、简介
地址:https://github.com/prometheus/jmx_exporter
JMX Exporter 利用 Java 的 JMX 机制来读取 JVM 运行时的一些监控数据,然后将其转换为 Prometheus 所认知的 metrics 格式,以便让 Prometheus 对其进行监控采集。
那么,JMX 又是什么呢?它的全称是:Java Management Extensions
。顾名思义,是管理 Java 的一种扩展框架,JMX Exporter 正是基于此框架来读取 JVM 的运行时状态的。
二、K8s中监控java进程
采用 javaagent 的形式运行 JMX-Exporter 的 jar 包,进程内读取 JVM 运行时状态数据,转换为 Prometheus metrics 格式,并暴露端口让 Prometheus 采集。
制作java镜像,导入jmx_exporter
Dockerfile
FROM openjdk:8u121-alpine LABEL maintainer="tunan" ENV JAVA_VERSION 8u121 ENV JAVA_OPTS="" RUN set -x \ && addgroup -g 1010 -S admin \ && adduser -S -D -u 1010 -s /sbin/nologin -G admin -g admin admin \ && mkdir -p /app && mkdir -p /data/logs/server-admin \ && chown -R admin.admin /app && chown -R admin.admin /data/logs/server-admin \ && ln -sf /dev/stdout /data/logs/server-admin/all_pb-server-admin.log \ && ln -sf /dev/stderr /data/logs/server-admin/err_pb-server-admin.log \ && rm -rf /var/lib/apt/lists/* WORKDIR /app USER admin COPY pb-server-admin-1.0-SNAPSHOT.jar /app COPY jmx_prometheus_javaagent-0.14.0.jar /app EXPOSE 8084 ENTRYPOINT java ${JAVA_OPTS} -jar pb-server-admin-1.0-SNAPSHOT.jar
jmx_exporter的配置文件
prometheus-jmx-config.yaml
--- ssl: false lowercaseOutputName: false lowercaseOutputLabelNames: false
创建
# 创建一个namespace # kubectl create ns kube-test # kubectl create cm jmx-config --from-file=prometheus-jmx-config.yaml -n kube-test configmap/jmx-config created # kubectl get cm -n kube-test NAME DATA AGE jmx-config 1 8s
启动一个监控项目
jmx-exporter.yaml
apiVersion: v1 kind: Service metadata: name: jmx-exporter namespace: kube-test labels: k8s-app: jmx-exporter spec: selector: k8s-app: jmx-exporter ports: - name: application port: 8084 protocol: TCP - name: jmx-metrics port: 9084 protocol: TCP type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: jmx-exporter namespace: kube-test labels: k8s-app: jmx-exporter spec: replicas: 1 selector: matchLabels: k8s-app: jmx-exporter template: metadata: labels: k8s-app: jmx-exporter spec: containers: - name: admin image: pb-server-admin:1.0.2 imagePullPolicy: IfNotPresent env: - name: JAVA_OPTS value: "-javaagent:/app/jmx_prometheus_javaagent-0.14.0.jar=9084:/config/prometheus-jmx-config.yaml" volumeMounts: - name: jmx-config mountPath: /config readOnly: true - name: tz-config mountPath: /etc/localtime readOnly: true nodeSelector: monitor: "true" volumes: - name: tz-config hostPath: path: /usr/share/zoneinfo/Asia/Shanghai - name: jmx-config configMap: name: jmx-config
-
通过9084这个端口来采集metrics
使用prometheus-operator,通过创建 ServiceMonitor 的 CRD 对象来配置 Prometheus
jmx-exporterMonitor.yaml
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: labels: k8s-app: jmx-exporter name: jmx-exporter namespace: monitoring spec: endpoints: - interval: 30s port: jmx-metrics scheme: http tlsConfig: insecureSkipVerify: false jobLabel: k8s-app namespaceSelector: matchNames: - kube-test selector: matchLabels: k8s-app: jmx-exporter
修改默认的clusterRole,使用Prometheus能采集其他namespace下的pod中的数据
vim prometheus-clusterRole.yaml
... resources: - nodes/metrics - namespaces - services - endpoints - pods verbs: - get - list - watch ...
# kubectl replace -f prometheus-clusterRole.yaml
创建
# kubectl create -f jmx-exporter.yaml service/jmx-exporter created deployment.apps/jmx-exporter created
查看Prometheus