当前位置: 首页 > news >正文

openresty + prometheus + grafana 搭建。监控网络请求

1. 环境说明

centos 7

openresty 相当于 nginx + lua
prometheus 定时任务采集数据
grafana 数据大屏

2. 安装步骤

架构说明,我们这里采集的不是 node_exporter 的数据,而是 openresty 的数据
在这里插入图片描述

2.1 安装 配置 openresty

# 安装 yum 工具包
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty

将nginx 配置放到 openresty 目录下

cp /path-of-nginx/conf.d/* /usr/local/openresty/nginx/conf/conf.d/

修改nginx.conf 配置

添加如下内容

log_format main '{ '
        '"@timestamp": "$time_iso8601", '
        '"request_method": "$request_method", '
        '"request_uri": "$request_uri", '
        '"request_length": "$request_length", '
        '"server_protocol": "$server_protocol", '
        '"remote_addr": "$remote_addr", '
        '"referer": "$http_referer", '
        '"status": $status, '
        '"bytes": $body_bytes_sent, '
        '"agent": "$http_user_agent", '
        '"x_forwarded": "$http_x_forwarded_for", '
        '"host": "$http_host", '
        '"up_addr": "$upstream_addr",'
        '"up_host": "$upstream_http_host",'
        '"up_resp_time": "$upstream_response_time",'
        '"request_time": "$request_time",'
        '"req_id": "$http_req_id"'
        ' }';

 
    access_log  /var/log/nginx/access.log  main;
    include /usr/local/openresty/nginx/conf/conf.d/*.conf;

openresty 的常用命令

# 启动
openresty -c <configuration file>
# 快速停止
openresty -s stop
# 完整有序的停止
openresty -s quit
# 修改配置后重新加载生效
openresty -s reload
# 重新打开日志文件
openresty -s  reopen

2.2 配置nginx-lua-prometheus

在/usr/local/openresty/lualib

git clone https://gitee.com/mirrors_knyar/nginx-lua-prometheus.git

nginx.conf 添加如下内容,加载脚本

http {
	..........省略
	lua_shared_dict prometheus_metrics 10M;
    lua_package_path "/usr/local/openresty/lualib/nginx-lua-prometheus/?.lua;;";
    init_worker_by_lua_block {
        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_block {
        metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
        metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
     }
	..........省略
     server {
        listen       80;
        server_name  localhost;
        ......省略
        # 配置metrics路径,因为promethus需要定时调用nginx来获取信息的,所以需要提供一个metrics来获取信息,再nginx.conf中配置
		location /metrics {
            content_by_lua_block {
                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()
            }
		}
		......省略
	}
}

2.3 安装 prometheus

# 下载
wget https://github.com/prometheus/prometheus/releases/download/v2.38.0/prometheus-2.38.0.linux-amd64.tar.gz
# 解压
tar -xf prometheus-2.38.0.linux-amd64.tar.gz -C /usr/local/
# 重命名,设置软连接
cd /usr/local/ && mv prometheus-2.28.1.linux-amd64  prometheus
ln -s /usr/local/prometheus/prometheus   /usr/bin/prometheus

编辑service 文件

vim /usr/lib/systemd/system/prometheus.service

添加如下内容

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
WorkingDirectory=/usr/local/prometheus/
ExecStart=/usr/local/prometheus/prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -KILL $MAINPID
Type=simple
KillMode=control-group
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target

重载配置文件

systemctl daemon-reload
# 启动
 systemctl start prometheus
# 查看prometheus端口监听
netstat -lntp | grep prometheus
# 状态
systemctl status prometheus
# 停止
systemctl stop prometheus

在 /usr/local/prometheus/prometheus.yml 添加如下内容

alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
# 添加的内容部分
  - job_name: "cyl_nginx"
    metrics_path: /metrics
    static_configs:
    - targets: ["localhost:80"]

2.4 安装 grafana

vim /etc/yum.repos.d/grafana.repo

添加如下内容

[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt

安装, 启动

yum install -y grafana
systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server

配置 nginx
注意修改 server_name

server {
    listen       80;
    server_name  your-server-name;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS,DELETE;
        add_header 'Access-Control-Allow-Headers' 'userId,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        proxy_pass http://127.0.0.1:3000;
        rewrite ^/grafana/(.*) /$1 break;
        proxy_set_header   Host $host;
    }
}

导入 prometheus 数据源

在这里插入图片描述
输入 URL
在这里插入图片描述
保存即可

创建dashboard

打开 https://grafana.com/grafana/dashboards/10223-nginx-lua/
复制 id
在这里插入图片描述
在这里插入图片描述
粘贴复制的id,点击 load
在这里插入图片描述
大功完成

参考
Grafana setup and running
Prometheus【普罗米修斯】+Grafana部署企业级监控(一)
Prometheus+Grafana监控
Prometheus OVERVIEW

相关文章:

  • 复盘:统计学派(频率学派)跟贝叶斯学派(贝叶斯公式、朴素贝叶斯)的定义和区别,奥卡姆剃刀和最大似然跟谁更接近
  • Java数据类型与变量
  • od笔试记录
  • Code Review
  • Kubernetes部署服务通过Ingress访问报错413解决
  • 3如何搭建组件库的样式工程之button-scss
  • 飞书第三方ISV服务商应用开发及上架教程
  • JavaScript 运算符和表达式(二)
  • js arr.reduce() reduce方法应用
  • Day 56 Django 连接数据库 ORM
  • 深度学习中的激活函数有哪些?
  • Image through Atmospheric Turbulence笔记(一)
  • 遇到的一些奇怪的bug(非代码问题)与解决方法
  • 鸟哥私房菜linux就该这么学-学习记录
  • 猿创征文| Mybatis报错原因和解决方法:Invalid bound statement (not found): com.xxx.mapper.xxx
  • angular2 简述
  • centos安装java运行环境jdk+tomcat
  • mysql 5.6 原生Online DDL解析
  • ng6--错误信息小结(持续更新)
  • Nodejs和JavaWeb协助开发
  • Python_网络编程
  • Spring Boot MyBatis配置多种数据库
  • Vue学习第二天
  • 不上全站https的网站你们就等着被恶心死吧
  • 基于 Babel 的 npm 包最小化设置
  • 基于HAProxy的高性能缓存服务器nuster
  • 一、python与pycharm的安装
  • 用mpvue开发微信小程序
  • ​iOS安全加固方法及实现
  • ​人工智能书单(数学基础篇)
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (10)ATF MMU转换表
  • (WSI分类)WSI分类文献小综述 2024
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (推荐)叮当——中文语音对话机器人
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (一)WLAN定义和基本架构转
  • (转)3D模板阴影原理
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Micro Framework 4.2 beta 源码探析
  • .NET正则基础之——正则委托
  • :not(:first-child)和:not(:last-child)的用法
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • [20161101]rman备份与数据文件变化7.txt
  • [28期] lamp兄弟连28期学员手册,请大家务必看一下
  • [asp.net core]project.json(2)