使用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等大部分需要的指标了。


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部署及相应配置

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

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


转载于:https://juejin.im/post/5cde1c436fb9a037d1393d18

一、prometheus简介 Prometheus是一个开源的系统监控和告警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF维护管理的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控prometheus支持多种exporter采集数据,还支持通过pushgateway进行数据上报,Prometheus再性能上可支撑上万台规模的集群。 二、prometheus架构图 三、prometheus组件介绍 1.Prometheus Server: 用于收集和存储时间序列数据。 2.Client Library: 客户端库,检测应用程序代码,当Prometheus抓取实例的HTTP端点时,客户端库会将所有跟踪的metrics指标的当前状态发送到prometheus server端。 3.Exporters: prometheus支持多种exporter,通过exporter可以采集metrics数据,然后发送到prometheus server端 4.Alertmanager: 从 Prometheus server 端接收到 alerts 后,会进行去重,分组,并路由到相应的接收方,发出报警,常见的接收方式有:电子邮件,微信,钉钉, slack等。 5.Grafana:监控仪表盘 6.pushgateway: 各个目标主机可上报数据到pushgatewy,然后prometheus server统一从pushgateway拉取数据。 四、课程亮点 五、效果图展示 六、讲师简介 先超(lucky):高级运维工程师、资深DevOps工程师,在互联网上市公司拥有多年一线运维经验,主导过亿级pv项目的架构设计和运维工作 主要研究方向: 1.云计算方向:容器 (kubernetes、docker),虚拟化(kvm、Vmware vSphere),微服务(istio),PaaS(openshift),IaaS(openstack)等2.系统/运维方向:linux系统下的常用组件(nginx,tomcat,elasticsearch,zookeeper,kafka等),DevOps(Jenkins+gitlab+sonarqube+nexus+k8s),CI/CD,监控(zabbix、prometheus、falcon)等 七、课程大纲
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值