1. 首页

使用prometheus监控nginx

前言

由于nginx的快速和高效,生产中的大部分应用都会选择使用nginx来做反向代理,这时对于nginx的监控就显得非常有必要,需要能够直观的显示出nginx实时的流量,访问量,响应时间,http状态码等指标。

prometheus具有由度量名称和键/值对标识的时间序列数据的多维数据模型,能够很好的对nginx的这些指标进行收集,并配合grafana进行图像展示。

收集nginx指标的较多采用的有nginx-vts模块,prometheus-lua两种方式进行采集,本文采用nginx-vts模块方式进行数据收集。

nginx-vts模块

1、添加nginx模块

nginx -V #configure arguments中可以查看到当前nginx所包含的模块

cd /root/

git clone https://github.com/vozlt/nginx-module-vts #获取vts源码

wget https://nginx.org/download/nginx-1.14.2.tar.gz #获取nginx源码

tar -zxvf https://nginx.org/download/nginx-1.14.2.tar.gz

cd nginx-1.14.2

./configure –add-module=/root/nginx-module-vts –prefix=/etc/nginx –sbin-path=/usr/sbin/nginx –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock –http-client-body-temp-path=/var/cache/nginx/client_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/nginx/scgi_temp –user=nginx –group=nginx –with-http_ssl_module –with-http_realip_module –with-http_addition_module –with-http_sub_module –with-http_dav_module –with-http_flv_module –with-http_mp4_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_random_index_module –with-http_secure_link_module –with-http_stub_status_module –with-http_auth_request_module –with-mail –with-mail_ssl_module –with-file-aio –with-ipv6 –with-cc-opt=’-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector –param=ssp-buffer-size=4 -m64 -mtune=generic’

–add-module=/root/nginx-module-vts,并将现有参数全部加上

make && make install #编译好的新nginx文件,可以直接用来替换掉旧的nginx文件,重启nginx服务进行升级

service nginx restart

2、nginx.conf加入以下配置,通过8088端口展示出数据


> http { vhost_traffic_status_zone; server { listen 8088; location /status { vhost_traffic_status_display; vhost_traffic_status_display_format html; } }}复制代码 >

重新加载nginx配置后,访问 IP:8088/status 可以看到如下页面,能够获取到请求时间,后端响应时间,http code等大部分需要的指标了。

使用prometheus监控nginx

3、指标的数据类型转换

要将获取到的数据接入prometheus还需要将数据转为metrics类型的数据,vts模块中直接提供了/status/format/prometheus 接口,访问 IP:8088/status/format/prometheus 即可看到转换后的数据。

部署nginx-vts-exporter

git clone https://github.com/hnlq715/nginx-vts-exporter

chmod +x nginx-vts-exporter

nginx-vts-exporter -nginx.scrape_uri=http://localhost:8088/status/format/json

启动后默认端口为 :9913/metrics

4、通过VTS获取其它指标

通过以上部署只能拿到默认的指标,生产中可能还会需要监控uri的请求量,监控IP访问情况(同一个IP出现大量访问时可能被攻击),获取不同agent请求量用于分析等,通过vts模块的vhost_traffic_status_filter_by_set_key功能可以自定义需要获取的指标。此处的指标需要加到对应的server配置中

server {

listen 80;

server_name nginx.test.com;

vhost_traffic_status_filter_by_set_key $uri uri::$server_name; #每个uri访问量

vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name; #不同国家/区域请求量

vhost_traffic_status_filter_by_set_key $filter_user_agent agent::$server_name; #获取用户所使用的agent

vhost_traffic_status_filter_by_set_key $status $server_name; #http code统计

vhost_traffic_status_filter_by_set_key $upstream_addr upstream::backend; #后端转发统计

vhost_traffic_status_filter_by_set_key $remote_port client::ports::$server_name; #请求端口统计

vhost_traffic_status_filter_by_set_key $remote_addr client::addr::$server_name; #请求IP统计

location ~ ^/storage/(.+)/.*$ {

set $volume $1;

vhost_traffic_status_filter_by_set_key $volume storage::$server_name; #请求路径统计

}

……

}

使用prometheus监控nginx

prometheus部署及相应配置

1、部署prometheus

prometheus server采用go语言编写,下载后解压可直接运行


> tar xvfz prometheus-2.7.2.linux-amd64.tar.gzcd prometheus-2.7.2.linux-amd64复制代码 >

/usr/local/prometheus-2.7.2.linux-amd64/prometheus –storage.tsdb.path=/data/prometheus –web.enable-lifecycle >> /data/prometheus/prometheus.log 2>&1 &

–storage.tsdb.path参数指定数据存储路径

–web.enable-lifecycle参数允许热加载,设置该参数后可以通过 curl XPOST localhost:9090/-/reload 快速加载配置

2、nginx-vts-exporte接入

修改prometheus.yml配置文件,将nginx-vts-exporter对应端口接入prometheus,采用文件发现的方式

- job_name: ‘Nginx_vts_exporter’

file_sd_configs:

- files: [’./file_sd_configs/file_sd_configs_nginx.json’]

refresh_interval: 30s

编辑./file_sd_configs/file_sd_configs_nginx.json,nginx数量比较多的情况下,可以利用自定义labels对Nginx进行分组,便于后续图像展示

[

{

“targets”: [ “10.21.141.25:9913” ],

“labels”: {

“group”: “ops-test”

}

},

{

“targets”: [ “10.21.141.26:9913” ],

“labels”: {

“group”: “ops-test”

}

}

]

grafana接入prometheus

在grafana数据源设置中将部署好的prometheus加入,就可以对获取到的数据使用图像展示了

导入nginx-vts-exporter的示例图

https://grafana.com/dashboards/2949

根据监控需求进行标签分组,增删图像。最终效果如下:

使用prometheus监控nginx

作者:MobTech开发者
链接:https://juejin.im/post/6844903845705809933

看完两件小事

如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:

  1. 关注我们的 GitHub 博客,让我们成为长期关系
  2. 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
  3. 关注公众号 「画漫画的程序员」,公众号后台回复「资源」 免费领取我精心整理的前端进阶资源教程

JS中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。欢迎热爱技术的你一起加入交流与学习,JS中文网的使命是帮助开发者用代码改变世界

本文著作权归作者所有,如若转载,请注明出处

转载请注明:文章转载自「 Js中文网 · 前端进阶资源教程 」https://www.javascriptc.com

标题:使用prometheus监控nginx

链接:https://www.javascriptc.com/4223.html

« 可视化IDE的探索之路
我们该如何从 0 到 1 搭建性能检测系统»
Flutter 中文教程资源

相关推荐

QR code