prometheus+grafana搭建 一、基础准备 二、prometheus搭建 三、grafana搭建 4、配置nginx反向代理

SpringBoot集成prometheus+Grafana监控:https://www.jianshu.com/p/f9d8c0f64936

Prometheus + Grafana(三)nginx 设置反向代理:https://grafana.com/tutorials/run-grafana-behind-a-proxy/#1

如何更改prometheus监控系统启动的默认端口号:https://blog.csdn.net/palet/article/details/82988100

二、prometheus搭建

1)上传prometheus-2.20.0.linux-amd64.tar.gz到服务器,例如/monitor,解压

2)cd /monitor/prometheus-2.20.0.linux-amd64

3)修改prometheus.yml,prometheus支持多种配置文件读取方式,本例采用file_sd方式,定期读取文件夹下的配置文件

scrape_configs:
  - job_name: 'prometheus'
    metrics_path: '/actuator/prometheus'     #springboot应用的prometheus数据接口地址
    file_sd_configs:
    - files:
      - /monitor/server_node.json    #配置文件路径地址
      refresh_interval: 10s          #配置文件刷新读取频率

4)新建/monitor/conf/server-node.json,内容如下,targets为后台应用的ip和端口(该端口是应用的prometheus数据端口,一般是manage port,不是server port)

[
  {
    "targets": [ "10.10.0.1:8001", “10.10.0.2:8001”],
    "labels": {
      "hostname": "prometheus_test"
    }
  }
]

5)启动prometheus

nohup ./prometheus --web.external-url=prometheus --config.file=prometheus.yml 2>&1 &

备注:web.external-url为prometheus的对外访问的子路精,用于nginx方向代理使用,添加这个配置会将prometheus对外访问的路径全部加上/prometheus子路径。

说明 原始路径 添加web.external-url后的访问路径
网址路径 http://127.0.0.1:9090/graph http://127.0.0.1:9090/prometheus/graph
静态资源路径 http://127.0.0.1:9090/static/xx.js http://127.0.0.1:9090/prometheus/static/xx.js
网页中配置的资源路径 http://127.0.0.1:9090/static/xx.js http://127.0.0.1:9090/prometheus/static/xx.js

三、grafana搭建

1)上传grafana-7.1.1.linux-amd64.tar.gz 到/monitor,解压三、grafana搭建

2)cd /wls/grafana-7.1.1.linux-amd64

3)修改配置文件,增加子路径地址   vi ./conf/defaults.ini,修改如下部分

root_url = %(protocol)s://%(domain)s/grafana
domain = xxx.rh.com
serve_from_sub_path = true

4)启动grafana

nohup ./bin/grafana-server web 2>&1 &

备注:grafana修改子路径和prometheus不一样,prometheus修改子路径后,所有访问的地址(页面、静态文件、页面中配置的静态文件地址)都会加上子路径

grafana修改子路径后 只有页面中配置的静态文件地址添加了子路径,页面和静态文件的访问地址还是在根路径

说明 原始路径 添加/grafana子路径后的访问路径
网址路径 http://127.0.0.1:3000/login http://127.0.0.1:3000/login
静态资源路径 http://127.0.0.1:3000/static/xx.js http://127.0.0.1:3000/static/xx.js
网页中配置的资源路径 http://127.0.0.1:3000/static/xx.js http://127.0.0.1:3000/grafana/static/xx.js

4、配置nginx反向代理

location /grafana/ {

        proxy_pass http://127.0.0.1:3000/;

    }

    location /prometheus {

        proxy_pass http://127.0.0.1:9090;

    }

访问地址:

prometheus:   http://域名/prometheus/graph

grafana:  http://域名/grafana/login

有个坑爹的事情 grafana登录后一直redirect到登录页面 f12发现是grafana的session写入不到cookie中,后来升级了浏览器竟然解决了。。。