In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to use Prometheus to monitor JVM, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Overview
When your Java business is containerized with K8S, what if you monitor it? The Prometheus community has developed JMX Exporter to export the monitoring metrics of JVM so that Prometheus can be used to collect monitoring data. Here's how to use Prometheus and JMX Exporter to monitor the JVM of your Java application.
What is JMX Exporter?
JMX Exporter uses the JMX mechanism of Java to read some monitoring data of JVM runtime, and then converts it into metrics format recognized by Prometheus, so that Prometheus can monitor and collect it.
So, what is JMX? Its full name is: Java Management Extensions. As the name implies, it is an extension framework for managing Java, based on which JMX Exporter reads the run-time state of JVM.
How do I use JMX Exporter to expose JVM monitoring metrics?
The following describes how to expose the JVM monitoring metrics of Java applications through JMX Exporter.
Two uses of JMX Exporter
JMX-Exporter provides two uses:
Start a stand-alone process. When JVM starts, it specifies parameters, exposes JMX's RMI interface, JMX-Exporter calls RMI to obtain JVM runtime status data, converts it to Prometheus metrics format, and exposes the port for Prometheus to collect.
JVM in-process startup (in-process). When JVM starts, it specifies the parameters, runs the jar package of JMX-Exporter in the form of javaagent, reads the JVM runtime status data in the process, converts it to Prometheus metrics format, and exposes the port for Prometheus to collect.
The first method is not recommended, on the one hand, the configuration is complex, on the other hand, it requires a separate process, and the monitoring of the process itself has become a new problem, so this article focuses on the second usage of how to use JMX Exporter to expose JVM monitoring indicators in the K8S environment.
Package image
Using the second usage, you need to specify the jar package file and configuration file for JMX Exporter when you start JVM. The jar package is a binary file, so it is difficult to mount the configuration file through configmap. We hardly need to modify the configuration file, so it is recommended to package the jar package and configuration file of JMX Exporter directly into the business container image.
First, prepare a directory to create the image, and put it into the JMX Exporter configuration file prometheus-jmx-config.yaml:
-ssl: falselowercaseOutputName: falselowercaseOutputLabelNames: false
Note: please refer to the official documentation for more configuration items.
Then prepare the jar package file. You can find the latest jar package download address on the Github page of jmx_exporter and download it to the current directory:
Wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.13.0/jmx_prometheus_javaagent-0.13.0.jar
Then prepare Dockerfile. Here's an example of tomcat:
FROM tomcat:jdk8-openjdk-slimADD prometheus-jmx-config.yaml / prometheus-jmx-config.yamlADD jmx_prometheus_javaagent-0.13.0.jar / jmx_prometheus_javaagent-0.13.0.jar
Finally, compile the image:
Docker build. -t ccr.ccs.tencentyun.com/imroc/tomcat:jdk8
Got it! If you want to make it easier, you can take advantage of docker multi-phase build, omitting the step of manually downloading the jar package, Dockerfile example:
FROM ubuntu:16.04 as jarWORKDIR / RUN apt-get update-yRUN DEBIAN_FRONTEND=noninteractive apt-get install-y wgetRUN wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.13.0/jmx_prometheus_javaagent-0.13.0.jarFROM tomcat:jdk8-openjdk-slimADD prometheus-jmx-config.yaml / prometheus-jmx-config.yamlCOPY-- from=jar / jmx_prometheus_javaagent-0.13.0.jar / jmx_prometheus_javaagent-0.13.0.jar deploy Java application
With the packaged image, the next step is to see how to deploy the application to K8S. The key point is how to modify the JVM startup parameters to load JMX Exporter at startup. When JVM starts, it reads the JAVA_OPTS environment variable as an additional startup parameter, so we can add this environment variable to the application when we deploy. Example:
ApiVersion: apps/v1kind: Deploymentmetadata: name: tomcatspec: replicas: 1 selector: matchLabels: app: tomcat template: metadata: labels: app: tomcatspec: 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: v1kind: Servicemetadata: name: tomcat labels: app: tomcatspec: type: ClusterIP ports:-port: 8080 protocol: TCP name: http-port: 8088 protocol: TCP name: jmx-metrics selector: app: tomcat
Startup parameter format:-javaagent:=:.
Here, port 8088 is used to expose the monitoring metrics of JVM, which can be changed by yourself.
Add Prometheus Monitoring configuration
Now that the monitoring metrics of JVM are exposed, configure Prometheus so that the monitoring data can be collected. Configuration example:
-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
If you have prometheus-operator installed, you can also configure Prometheus by creating a CRD object for ServiceMonitor:
ApiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata: name: tomcat namespace: default labels: app: tomcatspec: endpoints:-port: jmx-metrics interval: 5s namespaceSelector: matchNames:-default selector: matchLabels: app: tomcat add Grafana Monitoring Panel
After the data has been collected, let's take a look at how to display the data. If you are familiar with Prometheus and Grafana, you can design your own panels according to the metrics. The community also provides ready-made panels, but they are all a little old, and some views may not be displayed. You can import them directly and display them in some views, so you can import them directly.
The above is how to use Prometheus to monitor JVM. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.