Prometheus 监控 Nginx
前言
Nginx官网有介绍各种监控方案,以前我们常用stub_status和Log日志来实现Nginx监控。本文主要介绍基于Prometheus的2种监控方案nginx-lua-prometheus和nginx-vts-exporter,在真实的生产环境中你可能需要研究和选择哪种方法才是适合你的,F5收购Nginx后的未来让我们一起拭目以待。
Prometheus 监控 Nginx
更新历史
2019年03月25日 - 初稿
阅读原文 - https://wsgzao.github.io/post...
扩展阅读
Monitoring NGINX - https://www.nginx.com/blog/mo...
Nginx监控
官网介绍的监控方案 - https://www.nginx.com/blog/mo...
Prometheus 集成的 HTTP exporter 方案 - https://prometheus.io/docs/in...
聊聊 Nginx 的监控 - https://zhuanlan.zhihu.com/p/...
使用rpmbuild制作Nginx的RPM包 - https://wsgzao.github.io/post...
Prometheus 监控 Nginx
nginx-lua-prometheus
Nginx 需要添加 Lua 扩展
https://github.com/knyar/ngin...
# 下载redis_exporter
https://github.com/knyar/nginx-lua-prometheus/releases
wget https://github.com/knyar/nginx-lua-prometheus/archive/0.20181120.tar.gz
tar xf 0.20181120.tar.gz
cd nginx-lua-prometheus-0.20181120
# 创建prometheus.lua目录
mkdir -p /etc/nginx/lua/
cp prometheus.lua /etc/nginx/lua/
# 编辑nginx配置文件修改,注意修改lua_package_path "/etc/nginx/lua/prometheus.lua";
vim /etc/nginx/nginx.conf
lua_shared_dict prometheus_metrics 10M;
lua_package_path "/etc/nginx/lua/prometheus.lua";
init_by_lua '
prometheus = require("prometheus").init("prometheus_metrics")
metric_requests = prometheus:counter(
"nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
metric_latency = prometheus:histogram(
"nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
metric_connections = prometheus:gauge(
"nginx_http_connections", "Number of HTTP connections", {"state"})
';
log_by_lua '
metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
';
# 创建nginx-lua-prometheus
vim /etc/nginx/sites-available/nginx-lua-prometheus
server {
listen 9145;
location /metrics {
content_by_lua '
metric_connections:set(ngx.var.connections_reading, {"reading"})
metric_connections:set(ngx.var.connections_waiting, {"waiting"})
metric_connections:set(ngx.var.connections_writing, {"writing"})
prometheus:collect()
';
}
}
# 创建软链接
cd /etc/nginx/sites-enabled/
ln -s ../sites-available/prometheus
# 测试Nginx语法并reload测试metrics
nginx -t
nginx -s reload
curl http://127.0.0.1:9145/metrics
# iptables rule for Prometheus Nginx
-A INPUT -s xxx -p tcp --dport 9145 -j ACCEPT
nginx-vts-exporter
https://github.com/hnlq715/ng...
对方正在输入中
Grafana
nginx-lua-prometheus
https://grafana.com/dashboard...
nginx-vts-exporter
https://grafana.com/dashboard...
参考文献
wsgzao
[链接]
推荐阅读
System Design 学习路径
2022年尾比较热门的话题莫过于Elon Musk收购推特后大刀阔斧的改革,加速下滑还是涅槃重生大家一起拭目以待,做时间的朋友。借这个机会我们也可以通过Alex Xu分享的《Twitter Architecture 2022 vs 2012》来一探究...
王奥OX阅读 436
为go应用添加prometheus监控指标
我们首先从一个最简单的 Go 应用程序开始,在端口 8080 的 /metrics 端点上暴露客户端库的默认注册表,暂时还没有跟踪任何其他自定义的监控指标。
一尾赞 2阅读 4.3k
把 Go 放到 Nginx C module 之中
最近一段时间,我在做一件有趣的事情,让一个 Nginx C module 通过 Go 代码来访问 gRPC 服务。不得不感慨 Go 真的很流行,让人无法拒绝。之前我做 wasm-nginx-module 时就试图把 tinygo 跑在 Nginx 里面,这次则...
spacewander赞 2阅读 2.1k评论 2
让你的 Runner 可视化,使用 Prometheus + Grafana 实现极狐GitLab Runner 可视化
极狐GitLab Runner 从 1.8.0 引入了对 Prometheus metrics 的原生支持。Runner 目前对外输出的信息有:
极狐GitLab赞 2阅读 427
prometheus指南:采集k8s的原理和高可用存储实践
k8s零基础入门运维课程k8s零基础入门运维课程,计算存储网络和常见的集群相关操作k8s纯源码解读教程(3个课程内容合成一个大课程)k8s底层原理和源码讲解之精华篇k8s底层原理和源码讲解之进阶篇k8s纯源码解读课程,...
ning1875阅读 2.5k评论 1
Prometheus的使用
在Prometheus的架构设计中,Prometheus Server 并不直接服务监控特定的目标,其主要任务负责数据的收集,存储并且对外提供数据查询支持。因此为了能够能够监控到某些东西,如主机的CPU使用率,我们需要使用到Expo...
代码的路赞 2阅读 230
Prometheus 性能调优-水平分片
简介之前笔者有连续 2 篇文章:Prometheus 性能调优 - 什么是高基数问题以及如何解决?如何精简 Prometheus 的指标和存储占用陆续介绍了一些 Prometheus 的性能调优技巧,包括高基数问题的解决以及精简 Prometheu...
东风微鸣云原生阅读 942
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。