轻量级日志可视化平台Grafana Loki接入nginx访问日志

简介: 轻量级日志可视化平台Grafana Loki接入nginx访问日志

轻量级日志可视化平台Grafana Loki接入nginx访问日志


Loki简单介绍


Loki:像 Prometheus,但用于日志。 



640.png

Loki 是受Prometheus启发的水平可扩展、高可用、多租户日志聚合系统。它的设计非常经济高效且易于操作。它不索引日志的内容,而是索引每个日志流的一组标签。

与其他日志聚合系统相比,Loki:


1、不对日志进行全文索引。通过存储压缩的非结构化日志和仅索引元数据,Loki 操作更简单,运行成本更低。


2、使用您已经在 Prometheus 中使用的相同标签对日志流进行索引和分组,使您能够使用您已经在 Prometheus 中使用的相同标签在指标和日志之间无缝切换。


3、特别适合存储Kubernetes Pod 日志。Pod 标签等元数据会自动抓取并编入索引。


4、在 Grafana 中有原生支持(需要 Grafana v6.0)。


基于 Loki 的日志堆栈由 3 个组件组成:


promtail是代理,负责收集日志并发送给 Loki。

loki是主服务器,负责存储日志和处理查询。


Grafana用于查询和显示日志。


Loki 就像 Prometheus,但对于日志而言:我们更喜欢基于多维标签的索引方法,并且想要一个单二进制、易于操作系统且没有依赖项的系统。Loki 与 Prometheus 的不同之处在于它专注于日志而不是指标,并通过推送而不是拉取来交付日志



640.jpg


一、安装并配置Loki服务端


#loki Linux二进制安装包下载地址
https://github.com/grafana/loki/releases/download/v2.7.0/loki-linux-amd64.zip
loki-linux-amd64.zip
unzip loki-linux-amd64.zip 
mv loki-linux-amd64 /usr/local/bin/loki
chmod a+x /usr/local/bin/loki
loki -version
#loki-config配置文件下载地址
https://raw.githubusercontent.com/grafana/loki/main/examples/getting-started/loki-config.yaml
mkdir /etc/loki/
cat loki-local-config.yaml


640.png



640.png

640.png

vim /etc/systemd/system/loki.service
cat /etc/systemd/system/loki.service
[Unit]
Description=Grafana Loki
Documentation=https://github.com/grafana/loki
[Service]
ExecStart=/usr/local/bin/loki -config.file=/etc/loki/loki-local-config.yaml
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start loki.service 
systemctl enable loki.service 
systemctl status loki.service 
firewall-cmd --permanent --zone=public --add-port=3100/tcp
firewall-cmd --reload


640.png


640.png


二、nginx服务器上安装Promtail并采集nginx日志


1、修改nginx的日志格式并重启nginx


log_format json_analytics escape=json '{'
                            '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
                            '"connection": "$connection", ' # connection serial number
                            '"connection_requests": "$connection_requests", ' # number of requests made in connection
                    '"pid": "$pid", ' # process pid
                    '"request_id": "$request_id", ' # the unique request id
                    '"request_length": "$request_length", ' # request length (including headers and body)
                    '"remote_addr": "$remote_addr", ' # client IP
                    '"remote_user": "$remote_user", ' # client HTTP username
                    '"remote_port": "$remote_port", ' # client port
                    '"time_local": "$time_local", '
                    '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
                    '"request": "$request", ' # full path no arguments if the request
                    '"request_uri": "$request_uri", ' # full path and arguments if the request
                    '"args": "$args", ' # args
                    '"status": "$status", ' # response status code
                    '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
                    '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
                    '"http_referer": "$http_referer", ' # HTTP referer
                    '"http_user_agent": "$http_user_agent", ' # user agent
                    '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
                    '"http_host": "$http_host", ' # the request Host: header
                    '"server_name": "$server_name", ' # the name of the vhost serving the request
                    '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
                    '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
                    '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
                    '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
                    '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
                    '"upstream_response_length": "$upstream_response_length", ' # upstream response length
                    '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
                    '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
                    '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
                    '"scheme": "$scheme", ' # http or https
                    '"request_method": "$request_method", ' # request method
                    '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
                    '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
                    '"gzip_ratio": "$gzip_ratio", '
                    '"http_cf_ray": "$http_cf_ray"'
                    '}';
       access_log /var/log/nginx/access.log json_analytics;


说明:这里安装nginx进行测试


640.png

640.png

640.png


2、日志采集器Promtail安装与配置


rpm -ivh promtail-2.7.0.x86_64.rpm
vi /etc/promtail/config.yml
server:
  http_listen_port: 9080
  grpc_listen_port: 0
positions:
  filename: /tmp/positions.yaml
clients:
  - url: http://192.168.31.230:3100/loki/api/v1/push
scrape_configs:
- job_name: nginx
  static_configs:
  - targets:
      - localhost
    labels:
      job: nginx_logs
      __path__: /var/log/nginx/access.log
systemctl enable promtail.service
systemctl restart promtail.service
systemctl status promtail.service


640.png



640.png


三、Grafana上导入Loki的大屏


1、grafana的安装


granfan安装在Loki服务器上


yum install grafana-9.2.4-1.x86_64.rpm 
systemctl enable grafana-server.service --now
firewall-cmd --permanent --zone=public --add-port=3000/tcp
firewall-cmd --reload


640.png


640.png

2、导入12559的dashboard ID


https://grafana.com/grafana/dashboards/12559-grafana-loki-dashboard-for-nginx-service-mesh/


640.png

640.png


640.png


640.png

四、效果测试


触发Nginx访问日志,效果展示如下


说明:GEOIP模块并未编译到nginx中,如果需要按改DashboardID中给的说明自行配置,不过也可以在grafana大屏中地图展示panel自行删除不展示

640.png

640.png

相关文章
|
7月前
|
存储 监控 算法
防止员工泄密软件中文件访问日志管理的 Go 语言 B + 树算法
B+树凭借高效范围查询与稳定插入删除性能,为防止员工泄密软件提供高响应、可追溯的日志管理方案,显著提升海量文件操作日志的存储与检索效率。
209 2
|
数据可视化 关系型数据库 MySQL
ELK实现nginx、mysql、http的日志可视化实验
通过本文的步骤,你可以成功配置ELK(Elasticsearch, Logstash, Kibana)来实现nginx、mysql和http日志的可视化。通过Kibana,你可以直观地查看和分析日志数据,从而更好地监控和管理系统。希望这些步骤能帮助你在实际项目中有效地利用ELK来处理日志数据。
929 90
|
存储 前端开发 数据可视化
Grafana Loki,轻量级日志系统
本文介绍了基于Grafana、Loki和Alloy构建的轻量级日志系统。Loki是一个由Grafana Labs开发的日志聚合系统,具备高可用性和多租户支持,专注于日志而非指标,通过标签索引而非内容索引实现高效存储。Alloy则是用于收集和转发日志至Loki的强大工具。文章详细描述了系统的架构、组件及其工作流程,并提供了快速搭建指南,包括准备步骤、部署命令及验证方法。此外,还展示了如何使用Grafana查看日志,以及一些基本的LogQL查询示例。最后,作者探讨了Loki架构的独特之处,提出了“巨型单体模块化”的概念,即一个应用既可单体部署也可分布式部署,整体协同实现全部功能。
5401 70
Grafana Loki,轻量级日志系统
|
存储 运维 监控
超越传统模型:从零开始构建高效的日志分析平台——基于Elasticsearch的实战指南
【10月更文挑战第8天】随着互联网应用和微服务架构的普及,系统产生的日志数据量日益增长。有效地收集、存储、检索和分析这些日志对于监控系统健康状态、快速定位问题以及优化性能至关重要。Elasticsearch 作为一种分布式的搜索和分析引擎,以其强大的全文检索能力和实时数据分析能力成为日志处理的理想选择。
1155 6
|
存储 消息中间件 网络协议
日志平台-ELK实操系列(一)
日志平台-ELK实操系列(一)
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
|
存储 运维 监控
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
中信银行信用卡中心每日新增日志数据 140 亿条(80TB),全量归档日志量超 40PB,早期基于 Elasticsearch 构建的日志云平台,面临存储成本高、实时写入性能差、文本检索慢以及日志分析能力不足等问题。因此使用 Apache Doris 替换 Elasticsearch,实现资源投入降低 50%、查询速度提升 2~4 倍,同时显著提高了运维效率。
1077 3
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
|
域名解析 应用服务中间件 网络安全
阿里云个人博客外网访问中断应急指南:从安全组到日志的七步排查法
1. 检查安全组配置:确认阿里云安全组已开放HTTP/HTTPS端口,添加规则允许目标端口(如80/443),授权对象设为`0.0.0.0/0`。 2. 本地防火墙设置:确保服务器防火墙未阻止外部流量,Windows启用入站规则,Linux检查iptables或临时关闭防火墙测试。 3. 验证Web服务状态:检查Apache/Nginx/IIS是否运行并监听所有IP,使用命令行工具确认监听状态。 4. 测试网络连通性:使用外部工具和内网工具测试服务器端口是否开放,排除本地可访问但外网不可的问题。 5. 排查DNS解析:确认域名A记录指向正确公网IP,使用`ping/nslookup`验证解析正
539 2
|
JSON API Go
基于责任链与策略模式的轻量级PHP日志库设计
项目日志乱成一团,bug 时好时坏,服务器问题难以复现?我写了个 PHP 日志系统,第一时间发现问题,避免跑路。实现了责任链模式+策略模式,让日志存储更灵活,支持多种输出方式。
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
1380 4

推荐镜像

更多