设为首页 收藏本站
查看: 2491|回复: 0

[资源发布] Prometheus 监控 Nginx 流量 (三)

[复制链接]

尚未签到

发表于 2017-12-22 13:33:10 | 显示全部楼层 |阅读模式
介绍
  基于Openresty和Prometheus、Consul、Grafana设计的,实现了针对域名和Endpoint级别的流量统计,使用Consul做服务发现、KV存储,Grafana做性能图展示。
  最终展现图
DSC0000.png


主要实现流程
  POST Json 注册服务 -> Consul Service <- Prometheus -> 定时抓取 http指标接口 Nginx
  POST KV 提交Endpoint -> Consul KV Service <- Nginx 定时更新需要监控的Endpoint
  Grafana 读取 -> Prometheus

优点


  • 基本实现自动化集中配置,各种接口使用非常简单
  • 通过Prometheus提供了非常丰富的查询维度,例如(域名、Endpoint、状态码、协议类型、method),当然还可以非常简单地添加更多。
  • Grafana图表功能强大,非常直观地查看各个服务的状态和发现异常。
安装和使用说明
  本项目是基于Openresty开发,所以事先安装好Openresty,这个非常简单。
  安装Consul,这是基于golang开发的服务自动发现工具,详细查看官方文档。https://www.consul.io/
  安装Prometheus,这是一个时序数据库和监控工具,性能和存储十分可靠,把Prometheus配置发现服务使用Consul。官方文档:https://prometheus.io/docs/operating/configuration/#<consul_sd_config>
  安装Grafana。https://grafana.com/

安装 扩展程序
  克隆 lua-nginx-prometheus 仓库到Openresty服务器上。
  克隆依赖Prometheus nginx-lua-prometheus 仓库到服务器上。
  克隆依赖Consul lua-resty-consul 仓库到服务器上。
  把lua-nginx-prometheus仓库中的 counter.conf文件复制到Openresty目录下的nginx/conf/conf.d目录内。

编辑 counter.conf 文件
  

lua_package_path &quot;/Users/zl/Work/Counter/nginx-lua-prometheus/?.lua;;/Users/zl/Work/Counter/lua-resty-consul/lib/resty/?.lua;;/Users/zl/Work/Counter/lib/?.lua;;&quot;;  

  修改lua_package_path参数,把 lua-nginx-prometheus、nginx-lua-prometheus、lua-resty-consul三个目录位置指定,目录下一定是包含 ?.lua。
  

    consul_host = &quot;<Your consul host ip>&quot;  consul_port = <Your consul port>
  

  把consul的地址和端口替换上。
  

server {  listen 9145;
  allow 127.0.0.1;
  deny all;
  access_log off;
  location /metrics {
  content_by_lua 'prometheus:collect()';
  }
  
}
  

  添加allow 允许指定ip访问 指标接口。
  启动Openresty后,试试 http://<ip>:9145/metrics

配置 Prometheus 服务发现功能
  详细参考这个文档
  https://prometheus.io/docs/operating/configuration/#<consul_sd_config>
  完成后,通过Consul 的 http API进行注册服务。
  

curl -X PUT -d @test.json http://<ip>:<port>/v1/agent/service/register  

{  &quot;ID&quot;: <定义唯一的ID>,
  &quot;Name&quot;: &quot;对应prometheus consul_sd_config&quot;,
  &quot;Tags&quot;: [
  &quot;&quot;
  ],
  &quot;Address&quot;: <Openresty地址>,
  &quot;Port&quot;: 9145
  
}
  注销服务
  

curl http://<ip>:<port>/v1/agent/service/deregister/<ID>  

配置 Consul KV存储
  增加域名和对应的Endpoint
  

curl --request PUT --data @test.json http://<ip>:<port>/v1/kv/domain/<api.qq.com>/routers  

  数组
[  
&quot;/users/[0-9]+/followers/&quot;,
  
&quot;/users/[0-9]+/&quot;,
  
&quot;/users/[0-9]+/comments/&quot;,
  
&quot;/news&quot;
  
]

配置Grafana 到 Prometheus上读取数据
  详细文档参考 https://prometheus.io/docs/visualization/grafana/

创建图表
  常见查询语句
  

sum(irate(nginx_http_request_duration_seconds_count{host=&quot;api.qq.com&quot;}[5m])) by (status)  

  多台服务器合计每秒请求量,查询单个域名,group by 用状态码
  

sum(rate(nginx_http_request_duration_seconds_sum{host=&quot;api.qq.com&quot;,endpoint!=&quot;/ws&quot;}[1m])) / sum(rate(nginx_http_request_duration_seconds_count{host=&quot;api.qq.com&quot;,endpoint!=&quot;/ws&quot;}[1m]))  

  接口平均响应时间, 不包含 websocket接口
  histogram 直方图非常有用,可以详细了解一下。
  

topk(5, sum(rate(nginx_http_request_duration_seconds_sum{host=&quot;api.qq.com&quot;,endpoint!=&quot;/ws&quot;}[1h])) by (endpoint)/sum(rate(nginx_http_request_duration_seconds_count{host=&quot;api.qq.com&quot;,endpoint!=&quot;/ws&quot;}[1h])) by (endpoint))  

  5个响应时间最大的,不包含 websocket接口
  代码已经放到github上了
  https://github.com/vovolie/lua-nginx-prometheus
  有兴趣的可以看看啊。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表