介绍

基于Openresty和Prometheus、Consul、Grafana设计的,实现了针对域名和Endpoint级别的流量统计,使用Consul做服务发现、KV存储,Grafana做性能图展示。

最终展现图

主要实现流程

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 "/Users/zl/Work/Counter/nginx-lua-prometheus/?.lua;;/Users/zl/Work/Counter/lua-resty-consul/lib/resty/?.lua;;/Users/zl/Work/Counter/lib/?.lua;;";

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

    consul_host = "<Your consul host ip>"
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
{
"ID": <定义唯一的ID>,
"Name": "对应prometheus consul_sd_config",
"Tags": [
""
],
"Address": <Openresty地址>,
"Port": 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

数组

[
"/users/[0-9]+/followers/",
"/users/[0-9]+/",
"/users/[0-9]+/comments/",
"/news"
]

配置Grafana 到 Prometheus上读取数据

详细文档参考 https://prometheus.io/docs/visualization/grafana/

创建图表

常见查询语句

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

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

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

接口平均响应时间, 不包含 websocket接口

histogram 直方图非常有用,可以详细了解一下。

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

5个响应时间最大的,不包含 websocket接口


代码已经放到github上了

https://github.com/vovolie/lua-nginx-prometheus

有兴趣的可以看看啊。

Prometheus 监控 Nginx 流量 (三)的更多相关文章

  1. 05 . Prometheus监控Nginx

    List CentOS7.3 prometheus-2.2.1.linux-amd64.tar.gz nginx-module-vts 节点名 IP 软件版本 硬件 网络 说明 Prometheus ...

  2. Prometheus监控Nginx

    转载自:https://www.cnblogs.com/you-men/p/13173245.html CentOS7.3 prometheus-2.2.1.linux-amd64.tar.gz ng ...

  3. 用prometheus监控Nginx

    GitHub上官方地址:https://github.com/knyar/nginx-lua-prometheus 告警规则地址:https://awesome-prometheus-alerts.g ...

  4. Prometheus(三):Prometheus监控交换机(snmp)

    默认已安装Prometheus服务,服务地址:192.168.56.200 一.获取交换机snmp信息 snmp服务IP(交换机IP):172.20.2.83 snmp community:dfete ...

  5. 基于k8s集群部署prometheus监控ingress nginx

    目录 基于k8s集群部署prometheus监控ingress nginx 1.背景和环境概述 2.修改prometheus配置 3.检查是否生效 4.配置grafana图形 基于k8s集群部署pro ...

  6. Prometheus监控神器-Alertmanager篇(1)

    本章节主要涵盖了Alertmanager的工作机制与配置文件的比较详细的知识内容,由浅入深的给大家讲解. 警报一直是整个监控系统中的重要组成部分,Prometheus监控系统中,采集与警报是分离的.警 ...

  7. Prometheus监控学习笔记之360基于Prometheus的在线服务监控实践

    0x00 初衷 最近参与的几个项目,无一例外对监控都有极强的要求,需要对项目中各组件进行详细监控,如服务端API的请求次数.响应时间.到达率.接口错误率.分布式存储中的集群IOPS.节点在线情况.偏移 ...

  8. Kubernetes容器集群管理环境 - Prometheus监控篇

    一.Prometheus介绍之前已经详细介绍了Kubernetes集群部署篇,今天这里重点说下Kubernetes监控方案-Prometheus+Grafana.Prometheus(普罗米修斯)是一 ...

  9. Kubernetes 系列(五):Prometheus监控框架简介

    由于容器化和微服务的大力发展,Kubernetes基本已经统一了容器管理方案,当我们使用Kubernetes来进行容器化管理的时候,全面监控Kubernetes也就成了我们第一个需要探索的问题.我们需 ...

随机推荐

  1. CNN tricks

    Must Know Tips/Tricks in Deep Neural Networks (by Xiu-Shen Wei) http://lamda.nju.edu.cn/weixs/projec ...

  2. BZOJ3998 TJOI2015弦论(后缀数组+二分答案)

    先看t=1的情况.显然得求出SA(因为我不会SAM).我们一位位地确定答案.设填到了第len位,二分这一位填什么之后,在已经确定的答案所在的范围(SA上的某段区间)内二分,找到最后一个小于当前串的后缀 ...

  3. MVC 多submit

    直接上代码 3种情况: 第一种,由于form已经可以支持多个了.所以我们分成2个form来提交,submit会根据自己所在的form来提交当前表单的内容 @using (Html.BeginForm( ...

  4. bzoj 2460 [BeiJing2011]元素 (线性基)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2460 题意: 给你一堆矿石,矿石有a,b两种性质,取任意个矿石,满足取得的这些矿石a性质异或 ...

  5. MT【29】介绍向量的外积及应用举例

    我们在学校教材里学到的数量积(内积)其实还有一个孪生兄弟向量积(外积),这个对参加自主招生以及竞赛的学生来讲是需要掌握的,这里稍作介绍: 原理: 例题: 应用:

  6. 洛谷 P1407 [国家集训队]稳定婚姻 解题报告

    P1407 [国家集训队]稳定婚姻 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的 ...

  7. 【原创】py3+requests+json+xlwt,爬取拉勾招聘信息

    在拉勾搜索职位时,通过谷歌F12抓取请求信息 发现请求是一个post请求,参数为: 返回的是json数据 有了上面的基础,我们就可以构造请求了 然后对获取到的响应反序列化,这样就获取到了json格式的 ...

  8. luogu4181 [USACO18JAN]Rental Service (贪心)

    我们要出租的话,一定是出租产奶量最少的牛 那我们就看出租多少头牛(其他的卖奶)的时候答案最大就可以了. (注意N有可能小于R) #include<bits/stdc++.h> #defin ...

  9. Centos7下安装python3

    1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline- ...

  10. 【模板】spfa

    代码如下 #include <bits/stdc++.h> using namespace std; const int maxv=1e4+10; const int maxe=5e5+1 ...