如何借助Prometheus、Grafana、Elasticsearch监控Kubernetes集群微服务性能?
实现方案:Kubernetes环境下Spring Boot微服务的JVM/性能监控
一、改造Spring Boot微服务(核心准备)
仅靠Spring Boot Actuator无法直接输出Prometheus兼容的指标,需要搭配Micrometer完成格式转换与数据采集,这是K8s多服务监控的关键前提:
依赖配置(Maven示例)
在pom.xml中添加Actuator与Micrometer的Prometheus适配依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>配置文件(application.yml)
开启Prometheus端点,并给指标打上应用标签(用于K8s环境区分多服务):management: endpoints: web: exposure: include: prometheus,health # 按需添加其他端点 metrics: tags: application: ${spring.application.name} # 强制给所有指标打上服务名称标签 endpoint: prometheus: enabled: true
二、Kubernetes侧配置:让Prometheus自动发现服务
K8s环境下无需手动配置Prometheus静态目标,通过注解+ServiceMonitor实现服务自动发现:
1. 给微服务Deployment/Pod添加监控注解
在Deployment的Pod模板中添加Prometheus识别注解,告诉Prometheus该服务需要被采集:
apiVersion: apps/v1 kind: Deployment metadata: name: user-center-service spec: replicas: 3 template: metadata: annotations: prometheus.io/scrape: "true" prometheus.io/port: "8080" # Actuator所用端口,若单独配置管理端口则修改此处 prometheus.io/path: "/actuator/prometheus" # Prometheus端点路径 labels: app: spring-boot-service # 统一标签,用于后续ServiceMonitor批量匹配 spec: containers: - name: user-center-service image: your-registry/user-center:v1.0 ports: - containerPort: 8080
对应的Service也要保留匹配标签:
apiVersion: v1 kind: Service metadata: name: user-center-service spec: selector: app: spring-boot-service ports: - port: 8080 targetPort: 8080
2. 创建ServiceMonitor资源(适配Prometheus Operator)
如果使用Prometheus Operator部署Prometheus(K8s监控主流方案),创建ServiceMonitor批量匹配所有带app: spring-boot-service标签的服务:
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: spring-boot-services-monitor labels: release: prometheus # 需与你的Prometheus实例的release标签一致,否则无法被识别 spec: selector: matchLabels: app: spring-boot-service endpoints: - port: 8080 path: /actuator/prometheus interval: 15s # 指标采集间隔,按需调整
配置完成后,Prometheus会自动扫描集群内所有符合条件的服务,定期拉取监控数据。
三、Prometheus验证与指标查询
登录Prometheus UI,进入「Status -> Targets」,确认你的服务状态为UP。可用以下PromQL查询核心指标:
- 堆内存使用量:
jvm_memory_used_bytes{area="heap",application="user-center-service"} - 活跃线程数:
jvm_threads_live_threads{application="user-center-service"} - GC次数:
jvm_gc_collection_seconds_count{application="user-center-service"}
四、Grafana可视化配置
直接复用官方成熟的Spring Boot/JVM仪表盘,无需从零构建:
- 在Grafana中添加Prometheus作为数据源;
- 导入官方仪表盘ID:
12856(适配Spring Boot 2.x/3.x + Micrometer)或4701(经典JVM监控仪表盘); - 导入后通过
application标签过滤,即可查看单个或所有微服务的JVM、线程、GC等指标的可视化图表。
五、Elasticsearch的角色适配
你提到的Elasticsearch可承担两个角色:
- 日志存储与分析:用Filebeat采集容器日志,发送至ES,在Kibana中做日志排查,同时通过
application标签与Prometheus指标关联,实现日志-指标联动; - 指标持久化(可选):通过Prometheus的Remote Write功能,将指标同步至ES,用Kibana做长期指标分析(但Grafana+Prometheus已满足大部分实时监控需求,此场景多用于归档)。
六、K8s环境注意事项
- 端口安全:将微服务Service设为
ClusterIP类型,仅允许Prometheus在集群内部访问Actuator端点,避免外部暴露; - 权限控制:若使用Prometheus Operator,需确保ServiceMonitor所在Namespace与Prometheus实例的权限范围匹配,避免出现服务发现失败;
- 自定义指标:如需采集业务指标,直接用Micrometer API埋点(如
Counter、Gauge),指标会自动注入Prometheus端点。
内容的提问来源于stack exchange,提问作者user3069309
相关产品推荐
相关产品推荐

