springboot Actuator指标监控(基于k8s 和prometheus 实现)
1.简介
每一个微服务在部署以后,我们都需要对其进行监控、追踪、审计、控制等。 Spring Boot就提供了Actuator场景,使得我们的应用快速引用即可获得生产级别的应用监控、审计等功能。
Spring Boot Actuator是一个用于监控和管理Spring Boot应用程序的工具。它提供了一组RESTful端点,可以用于获取应用程序的健康状况、性能指标、日志信息等。
优点:
只需添加相应的依赖,即可快速启用和使用,可以满足大部分应用程序的监控和管理需求。
缺点:
1、Actuator的端点暴露了应用程序的内部信息,如果不加以保护,可能会导致安全风险。
2、Actuator的一些端点可能会对应用程序的性能产生一定的影响,尤其是在高并发场景下。因此,在使用Actuator时,需要根据实际情况进行性能测试和优化。
Actuator 文档:https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html
2.环境说明
k8s:v1.23.17 负责springboot程序的运行
prometheus:2.32.1 负责拉取数据并存储
springboot Actuator:3.0.2 负责提供prometheus支持的数据
3.Springboot 配置Actuator
配置依赖:引⼊actuator提供指标
org.springframework.boot
spring-boot-starter-actuator
io.github.mweirauch
micrometer-jvm-extras
0.2.0
io.micrometer
micrometer-registry-prometheus
修改配置文件暴露指标:
management.server.port=8080
spring.application.name=spring-prometheus
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.metrics.tags.application=${spring.application.name}
server.port=8080
引入jar包并修改配置文件后,springboot程序就可以对外提供各种指标数据了
启动服务验证:http://localhost:8080/actuator
常用端点:
| ID | 描述 |
|---|---|
auditevents |
暴露当前应用程序的审核事件信息。需要一个AuditEventRepository组件。 |
beans |
显示应用程序中所有Spring Bean的完整列表。 |
caches |
暴露可用的缓存。 |
conditions |
显示自动配置的所有条件信息,包括匹配或不匹配的原因。 |
configprops |
显示所有@ConfigurationProperties。 |
env |
暴露Spring的属性ConfigurableEnvironment |
flyway |
显示已应用的所有Flyway数据库迁移。 需要一个或多个Flyway组件。 |
health |
显示应用程序运行状况信息。 |
httptrace |
显示HTTP跟踪信息(默认情况下,最近100个HTTP请求-响应)。需要一个HttpTraceRepository组件。 |
info |
显示应用程序信息。 |
integrationgraph |
显示Spring integrationgraph 。需要依赖spring-integration-core。 |
loggers |
显示和修改应用程序中日志的配置。 |
liquibase |
显示已应用的所有Liquibase数据库迁移。需要一个或多个Liquibase组件。 |
metrics |
显示当前应用程序的“指标”信息。 |
mappings |
显示所有@RequestMapping路径列表。 |
scheduledtasks |
显示应用程序中的计划任务。 |
sessions |
允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序。 |
shutdown |
使应用程序正常关闭。默认禁用。 |
startup |
显示由ApplicationStartup收集的启动步骤数据。需要使用SpringApplication进行配置BufferingApplicationStartup。 |
threaddump |
执行线程转储。 |
heapdump |
返回hprof堆转储文件。 |
jolokia |
通过HTTP暴露JMX bean(需要引入Jolokia,不适用于WebFlux)。需要引入依赖jolokia-core。 |
logfile |
返回日志文件的内容(如果已设置logging.file.name或logging.file.path属性)。支持使用HTTPRange标头来检索部分日志文件的内容。 |
prometheus |
以Prometheus服务器可以抓取的格式公开指标。需要依赖micrometer-registry-prometheus。 |
4.部署程序到K8S 集群
Deployment 配置:
[root@k8s-demo001 sc]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: expose-prometheus-demo
labels:
app: expose-prometheus-demo
spec:
replicas: 3
selector:
matchLabels:
app: expose-prometheus-demo
template:
metadata:
labels:
app: expose-prometheus-demo
spec:
containers:
- name: expose-prometheus-demo
image: 192.168.1.10:8011/demo/prometheus-springboot:0.0.1
imagePullPolicy: Always
ports:
- containerPort: 8080
service配置:
[root@k8s-demo001 sc]# cat service.yaml
apiVersion: v1
kind: Service
metadata:
name: expose-prometheus-demo-service
labels:
app: expose-prometheus-demo
release: prometheus
spec:
selector:
app: expose-prometheus-demo
ports:
- protocol: TCP
name: http-traffic
port: 8080
targetPort: 8080
5.prometheus 部署在k8s内
servicemonitor 配置
[root@k8s-demo001 sc]# cat servicemonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: expose-prometheus-demo-service-monitor
labels:
app: expose-prometheus-demo
release: prometheus
spec:
selector:
matchLabels:
app: expose-prometheus-demo # 跟 svc 的 lables 保持一致
endpoints:
- port: http-traffic # 这个 port 对应 Service.spec.ports.name
path: "/actuator/prometheus"
servicemonitor 配置完成之后,观察prometheus web页面就可以看到自动发现了pod

grafana 上配置相关的dashboard 就可以将采集的指标进行出图展示

6.prometheus 部署在k8s外
在service中加入如下信息
annotations:
prometheus.io/port: "8082" #端口配自己服务的端口
prometheus.io/spring: "true"
prometheus.io/path: "actuator/prometheus"
配置prometheus
- job_name: 'spring metrics'
kubernetes_sd_configs:
- role: endpoints
api_server: "https://172.16.252.105:6443"
tls_config:
insecure_skip_verify: true
bearer_token_file: k8s.token
relabel_configs:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_spring]
action: keep
regex: true
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: ([^:]+)(?::d+)?;(d+)
replacement: $1:$2
- action: labelmap
regex: __meta_kubernetes_service_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: spring_namespace
- source_labels: [__meta_kubernetes_service_name]
action: replace
target_label: spring_name
在k8s集群外部署prometheus监控,需要保证可以和k8s pod通信,否则自动发现服务后会一直报错down