preface

For a SpringBoot project application, a SpringBoot application without JVM monitoring is like a runaway horse. It may run fine one day, but tomorrow it may run wild, and we as the host cannot control it, which is fatal to the application.

When the SpringBoot project is migrated to K8S, it is no longer applicable to monitor the resource status of the project, such as CPU, memory, disk and JVM related information, such as team memory, threads, class loading, etc., in the K8S environment as compared to the server directly deployed. Because the application is no longer a designated server, it can also be a cluster.

So what? One pod at a time?

At this time, it will be recalled that Grafana+Prometheus was used to monitor all objects in K8S when the entire system was built.

Grafana is an open source data visualization tool developed in Go for data monitoring and statistics, with alarm capabilities

Prometheus is an open source version of the open source monitoring system developed by SoundCloud. In 2016, the Google-sponsored Cloud Native Computing Foundation (CNCF) included Prometheus as its second major open source project (with a strong background).

The following figure is the resource monitoring of K8S node, POD and Service based on Grafana+Prometheus, through which we can realize the hardware resource monitoring of Springboot project.

Implement JSON file as follows:

K8s resource monitoring based on Grafana+Prometheus

Back to our topic, how to implement SpringBoot project on K8S JVM monitoring, for the general Java Java project, we generally use JDK own monitoring tools: JConsole and JVisualVM, or use the command line jstat, etc..

Grafana+Prometheus was used as the carrier to visualize the JVM status of SpingBoot in K8S. Grafana+Prometheus was used as the carrier to visualize the JVM status of SpingBoot in K8S.

That must be possible. That’s why i write .

The principle of

Spring-Boot-Actuator

SpringBoot has monitoring functions that can help monitor the internal operation of programs, such as monitoring, Bean loading, environment variables, log information, thread information, health check, audit, statistics and HTTP tracking. The Actuator can also be integrated with external application monitoring systems, such as Prometheus. You can choose to use HTTP endpoints or JMX to manage and monitor your application. The Actuator uses Micrometer to integrate with Prometheus, the external application monitoring system mentioned above. This makes it possible to integrate any application monitoring system with a very small configuration.

Check the official documentation of spring-boot-ACTUATOR in detail

Implementation Principle

Used in SpringBoot projectsspring-boot-actorIn order tohttpPrometheus stores JVM and other state data through configuration reading interface data, and finally provides the following information for PrometheusGrafanaIn the configurationPrometheusData source, design associated charts, read stored state data back to page rendering ICONS using Prometheus SQL,Implementation of springboot application JVM visual monitoring in K8S.

implementation

The environment that

soft version
java 1.8
springboot 2.1.0. RELEASE
Spring-Boot-Actuator 2.1.0. RELEASE
grafana-amd64 v5.0.4
prometheus v2.0.0
k8s 1.16.0

Springboot introduces dependencies

        <! -- monitor -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.1.0. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.1.0</version>
        </dependency>
Copy the code

Application configuration

# Actuator config
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
management.metrics.export.prometheus.enabled=true
management.endpoint.health.show-details=always
management.endpoints.web.exposure.exclude=env,beans
Copy the code

Viewing Indicator Data

Visit http://localhost:8080/actuator/prometheus localhost: 8080: the project access address

You can see the following output:

# HELP rabbitmq_published_total  
# TYPE rabbitmq_published_total counter
rabbitmq_published_total{application="center",name="rabbit", 0.0}# HELP tomcat_global_sent_bytes_total  
# TYPE tomcat_global_sent_bytes_total counter
tomcat_global_sent_bytes_total{application="center",name="http-nio-8080", 31.0}# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="center", 2.803367936 e9}# HELP tomcat_threads_current_threads  
# TYPE tomcat_threads_current_threads gauge
tomcat_threads_current_threads{application="center",name="http-nio-8084", 10.0}# HELP tomcat_sessions_active_current_sessions  
# TYPE tomcat_sessions_active_current_sessions gauge
tomcat_sessions_active_current_sessions{application="center", 0.0}# HELP rabbitmq_channels  
# TYPE rabbitmq_channels gauge
rabbitmq_channels{application="center",name="rabbit", 1.0}# HELP system_load_average_1m The sum of the number of runnable entities queued to available processors and the number of runnable entities running on the available processors averaged over a period of time
# TYPE system_load_average_1m gauge
system_load_average_1m{application="center", 1.53}# HELP tomcat_sessions_rejected_sessions_total  
# TYPE tomcat_sessions_rejected_sessions_total counter
tomcat_sessions_rejected_sessions_total{application="center", 0.0}# HELP tomcat_threads_busy_threads  
# TYPE tomcat_threads_busy_threads gauge
tomcat_threads_busy_threads{application="center",name="http-nio-8084", 1.0}# HELP tomcat_sessions_created_sessions_total  
# TYPE tomcat_sessions_created_sessions_total counter
tomcat_sessions_created_sessions_total{application="center", 0.0}# HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC
# TYPE jvm_gc_memory_promoted_bytes_total counter
jvm_gc_memory_promoted_bytes_total{application="center", 1.37307128 e8}# HELP jvm_buffer_count_buffers An estimate of the number of buffers in the pool
# TYPE jvm_buffer_count_buffers gauge
jvm_buffer_count_buffers{application="center",id="direct", 7.0 jvm_buffer_count_buffers} {application ="center",id="mapped", 0.0}Copy the code

Prometheus configuration

Adds a actuator reading source

  - job_name: 'center-actuator'
      metrics_path: /actuator/prometheus
      static_configs:
      - targets: ['192.168.1.240:8080']
Copy the code

Job_name: defines the name of a unique Prometheus task; metrics_path: specifies the access path; and the actuator fixes the targets: sets of service access ports

Visit Prometheus

Grafana configuration

Springboot Monitoring panel JSON

Effect:

conclusion

If you want to monitor the springboot application JVM on k8S, you can monitor the data in this way. If you want to monitor the SPRINGboot application JVM on K8S, you can monitor the data in this way. For now, Grafana+Prometheus is the preferred solution. Arthas is currently used in this case. Arthas is used to implement specific tuning for online applications.