以前在《Prometheus监控实践:Kubernetes集群监控》一本中总结了咱们目前基于Prometheus对Kubernetes集群的监控,除了监控Kubernetes集群自己的关键指标以外,也对部署在Kubernetes集群上应用的状态作了监控。 对于Kubernetes集群上Pod, DaemonSet, Deployment, Job, CronJob等各类资源对象,咱们经过kube-state-metrics做为Prometheus的exporter完成了对这些Kubernetes资源对象的监控。而为了使监控深刻到应用的内部,就须要应用自身暴露做为Exporter暴露监控指标,这就和应用的开发语言和技术框架紧密相关了。html
咱们目前部署在Kubernetes上的微服务主要是由Go和Java两种语言开发的,本篇将总结一下目前咱们使用Prometheus对Java应用的监控实践。java
Prometheus提供了一个client_java项目能够很方便的将JVM和自定义的指标暴露出来。目前咱们主要用到了这个项目中的以下几个库:git
simpleclient_hotspot
simpleclient_spring_web
simpleclient_spring_boot
simpleclient_httpserver
使用Java能够开发如下两种服务:github
第一种是使用spring-boot-starter-web
开发的HTTP Restful API,这类服务要集成Prometheus十分简单,只须要在项目中依赖管理如引入simpleclient_hotspot
和simpleclient_spring_boot
。web
1 2 |
compile 'io.prometheus:simpleclient_spring_boot:0.1.0' compile 'io.prometheus:simpleclient_hotspot:0.1.0' |
simpleclient_spring_boot
中的io.prometheus.client.spring.boot.PrometheusEndpointConfiguration
会将prometheus exporter注册为Spring Boot Actuator的enpoint。启动这个配置只需在spring boot项目的Appication类上加上@EnablePrometheusEndpoint
的注解,例如:spring
1 2 3 4 5 6 7 8 9 |
@EnablePrometheusEndpoint @EnableSpringBootMetricsCollector @SpringBootApplication public class BootApplication { @PostConstruct public void init() { DefaultExports.initialize(); } ...... |
固然spring boot的配置文件中须要配置如何暴露这个endpoint:api
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
management: port: 8088 security: enabled: false health: defaults: enabled: false endpoints: enabled: false prometheus: enabled: true path: /prometheus health: enabled: true path: /health |
此时http://:8088/prometheus这个端点就是这个Java应用暴露的监控指标。tomcat
第二种是使用最基本的spring-boot-starter
和其余starter开发的rpc服务(thrift,gRPC等),自己不包含嵌入的tomcat,而Prometheus的exporter须要以HTTP暴露监控指标。须要在项目中依赖管理如引入simpleclient_hotspot
和simpleclient_httpserver
。app
1 2 |
compile 'io.prometheus:simpleclient_httpserver:0.1.0' compile 'io.prometheus:simpleclient_hotspot:0.1.0' |
simpleclient_httpserver
库包含一个简单的httpserver,使用其完成监控指标的暴露:框架
1 2 |
DefaultExports.initialize(); new io.prometheus.client.exporter.HTTPServer.HTTPServer(8088); |
http://:8088这个端点就是这个Java应用暴露的监控指标。
当Java应用中集成了Prometheus JVM Client后,就能够以HTTP的形式暴露监控指标。 若是Java应用以Pod的形式部署在Kubernetes集群上,为了使Kubernetes集群中的Prometheus能够发现Pod暴露的HTTP监控端点,还须要在Kubernetes manifest文件中加入下面的annotation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
--- kind: Deployment apiVersion: apps/v1beta2 metadata: labels: ...... name: xxx-svc namespace: xx spec: ...... selector: matchLabels: app: xxx-svc template: metadata: labels: app: xxx-svc annotations: prometheus.io/scrape: 'true' prometheus.io/path: /prometheus prometheus.io/port: '8088' spec: containers: ...... |
这样Prometheus中配置的job kubernetes-pods
就能够自动发现服务暴露的监控端点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
- job_name: 'kubernetes-pods' kubernetes_sd_configs: - role: pod relabel_configs: - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape] action: keep regex: true - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path] action: replace target_label: __metrics_path__ regex: (.+) - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port] action: replace regex: ([^:]+)(?::\d+)?;(\d+) replacement: $1:$2 target_label: __address__ - action: labelmap regex: __meta_kubernetes_pod_label_(.+) - source_labels: [__meta_kubernetes_namespace] action: replace target_label: kubernetes_namespace - source_labels: [__meta_kubernetes_pod_name] action: replace target_label: kubernetes_pod_name |
关于Java应用中JVM的监控的Dashboard,目前尚未在网上找到太好的,下图是目前咱们本身画的一个:
收集到了监控数据后,告警规则根据须要配置就能够了。