使用Prometheus Stack监控SpringBoot应用
本文将详细介绍如何使用Prometheus Stack监控SpringBoot应用的metrics。假设你已经安装了Kubernetes集群,并使用Helm安装了Prometheus Stack全家桶。SpringBoot应用已经配置好,暴露了相应的metrics端点。
SpringBoot Feign指标暴露,参考链接:https://editor.csdn.net/md/?articleId=146506845
前置条件
- Kubernetes集群:确保你已经有一个运行中的Kubernetes集群。
- Prometheus Stack:使用Helm安装Prometheus Stack全家桶。
- SpringBoot应用:SpringBoot应用已经配置好,暴露了metrics端点,例如
/actuator/prometheus
。
步骤
1. 创建 springboot-monitor.yaml
文件
首先,我们需要创建一个 ServiceMonitor
资源文件,以便Prometheus能够发现并监控SpringBoot应用的metrics。
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: springboot-monitor # monitor名称,可自定义
namespace: monitoring # 必须与Prometheus Operator同namespace
spec:
selector:
matchLabels:
app: springboot-app # 必须匹配Service的标签,命令: kubectl get svc -----NAME 那一列值,需要监控哪个服务就填写哪个服务
namespaceSelector:
any: true # 设置为true可跨namespace监控
matchNames:
- default # 指定被监控Service所在的namespace, 命令: kubectl get svc -A
endpoints:
- port: http # 必须与Service中定义的端口名一致,这里是port名称,并非是port的值, kubectl describe svc xxx
path: /actuator/prometheus # SpringBoot中Prometheus暴露的端点信息
interval: 15s # 指定监控间隔
2. 应用 springboot-monitor.yaml
文件
将上述内容保存为 springboot-monitor.yaml
文件,然后使用 kubectl
命令将其应用到Kubernetes集群中:
kubectl apply -f springboot-monitor.yaml
3. 验证监控状态
查看Prometheus服务
首先,查看Prometheus服务的外部IP地址:
kubectl get svc -n monitoring
找到Prometheus服务的 externalIp
,然后使用 curl
命令验证监控目标:
curl http://{externalIp}:9090/api/v1/targets | jq '.data.activeTargets[] | select(.labels.service == "前面yaml中app对应的值")'
确保输出中包含你配置的SpringBoot应用的监控目标。
4. 在Grafana中查看指标
访问Grafana
打开Grafana的Web界面,进入到datasource explore中,metric 检索查看指标信息:
查看指标
在Grafana中,选择 Explore
选项卡,输入你感兴趣的指标名称,例如 jvm_memory_used_bytes
,即可看到SpringBoot应用暴露的指标数据。