基于docker 搭建Prometheus+Grafana
【监控利器Prometheus】——Prometheus+Grafana监控SpringBoot项目JVM信息
prometheus下载地址:https://prometheus.io/download
grafana下载地址:https://grafana.com/grafana/download
node_exporter下载地址:https://github.com/prometheus/node_exporter/releases
window_node_exporter下载地址:https://github.com/prometheus-community/windows_exporter/releases
docker pull prom/node-exporter
docker pull prom/prometheus
docker pull grafana/grafana
node-exporter用于采集node的运行指标,包括node的cpu、load、filesystem、meminfo、network等基础监控指标,类似于zabbix监控系统的的zabbix-agent
node-export由Prometheus官方提供、维护,属于监控指标收集类UNIX内核操作系统的必备的exporter。
docker run --name node-exporter -d -p 9100:9100 \
-v /proc:/host/proc:ro \
-v /sys:/host/sys:ro \
-v /:/rootfs:ro \
--net=mynet \
prom/node-exporter
# 或者
docker run --name node-exporter -d -p 9100:9100 \
-v "/proc:/host/proc:ro" \
-v "/sys:/host/sys:ro" \
-v "/:/rootfs:ro" \
--net=mynet \
prom/node-exporter
root@sony-HP-Notebook:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2fec22b3fb7 prom/node-exporter "/bin/node_exporter" 7 seconds ago Up 3 seconds 0.0.0.0:9100->9100/tcp, :::9100->9100/tcp node-exporter
访问:http://192.168.0.44:9100/metrics
cd /usr/local
mkdir prometheus
vim prometheus.yml
内容如下:
global:
scrape_interval: 60s
evaluation_interval: 60s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['192.168.0.44:9090']
labels:
instance: prometheus
- job_name: linux
static_configs:
- targets: ['192.168.0.44:9100']
labels:
instance: localhost
注意:修改IP地址,这里的192.168.0.44就是本机地址
docker run --name prometheus -d \
-p 9090:9090 \
-v /usr/local/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
--net=mynet \
prom/prometheus
访问:http://192.168.0.44:9090/targets,如下显示状态up
mkdir /usr/local/grafana
# 配置权限因为grafana用户会在这个目录写入文件,直接设置777
chmod 777 -R /usr/local/grafana
启动grafana
docker run --name grafana -d \
-p 3000:3000 \
-v /usr/local/grafana:/var/lib/grafana \
--net=mynet \
grafana/grafana
需要等待一段时间
访问:http://192.168.0.44:3000/,默认的用户名和密码都是admin
https://grafana.com/grafana/dashboards/?search=JVM+%28Actuator%29&utm_source=grafana_search
8919
用来监控物理机信息4701
健康java程序13694
https://grafana.com/grafana/dashboards/13694-jvm-actuator/
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>io.micrometergroupId>
<artifactId>micrometer-registry-prometheusartifactId>
<version>1.9.3version>
dependency>
这里 management.endpoints.web.exposure.include=* 配置为开启 Actuator 服务,因为Spring Boot Actuator 会自动配置一个 URL 为 /actuator/Prometheus 的 HTTP 服务来供 Prometheus 抓取数据,不过默认该服务是关闭的,该配置将打开所有的 Actuator 服务。management.metrics.tags.application 配置会将该工程应用名称添加到计量器注册表的 tag 中去,方便后边 Prometheus 根据应用名称来区分不同的服务。
@SpringBootApplication
public class Springboot2PrometheusApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2PrometheusApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName){
return registry -> registry.config().commonTags("application", applicationName);
}
}
# 配置为开启Actuator服务
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
prometheus:
enabled: true
metrics:
enabled: true
metrics:
export:
prometheus:
enabled: true
# 将工程应用名称添加到计量器注册表的 tag 中
tags:
application: ${spring.application.name}
访问:http://127.0.0.1:8205/actuator/prometheus
修改宿主机prometheus配置文件prometheus.yml
scrape_configs:
- job_name: "springboot_jvm"
# 多久采集一次数据
scrape_interval: 5s
# 采集时的超时时间
scrape_timeout: 5s
# 采集的路径
metrics_path: '/actuator/prometheus'
# 采集Springboot服务的地址
static_configs:
- targets: ['192.168.0.35:8205']
主要是在prometheus.yml默认配置中加一个job,job_name为 “springboot_jvm”,
如果一个应用有多个实例:
修改完配置文件,重启Prometheus
docker restart prometheus
然后在Prometheus的targets中(http://192.168.0.44:9090/targets)就可以看到刚才配置的Node Exporter服务了:
13694
Jvm模板
10280
SpringBoot模板spring-boot-2-1-statistics
确保程序代码已经接入了actuator和prometheus