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

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
可观测监控 Prometheus 版,每月50GB免费额度
日志服务 SLS,月写入数据量 50GB 1个月
简介: 轻量级日志可视化平台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

相关文章
|
8月前
|
数据可视化 关系型数据库 MySQL
ELK实现nginx、mysql、http的日志可视化实验
通过本文的步骤,你可以成功配置ELK(Elasticsearch, Logstash, Kibana)来实现nginx、mysql和http日志的可视化。通过Kibana,你可以直观地查看和分析日志数据,从而更好地监控和管理系统。希望这些步骤能帮助你在实际项目中有效地利用ELK来处理日志数据。
646 90
|
9月前
|
存储 前端开发 数据可视化
Grafana Loki,轻量级日志系统
本文介绍了基于Grafana、Loki和Alloy构建的轻量级日志系统。Loki是一个由Grafana Labs开发的日志聚合系统,具备高可用性和多租户支持,专注于日志而非指标,通过标签索引而非内容索引实现高效存储。Alloy则是用于收集和转发日志至Loki的强大工具。文章详细描述了系统的架构、组件及其工作流程,并提供了快速搭建指南,包括准备步骤、部署命令及验证方法。此外,还展示了如何使用Grafana查看日志,以及一些基本的LogQL查询示例。最后,作者探讨了Loki架构的独特之处,提出了“巨型单体模块化”的概念,即一个应用既可单体部署也可分布式部署,整体协同实现全部功能。
3313 70
Grafana Loki,轻量级日志系统
|
11月前
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
991 3
|
应用服务中间件 nginx
nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
【8月更文挑战第27天】nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
931 6
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
运维 Kubernetes 监控
Loki+Promtail+Grafana监控K8s日志
综上,Loki+Promtail+Grafana 监控组合对于在 K8s 环境中优化日志管理至关重要,它不仅提供了强大且易于扩展的日志收集与汇总工具,还有可视化这些日志的能力。通过有效地使用这套工具,可以显著地提高对应用的运维监控能力和故障诊断效率。
1602 0
|
6月前
|
监控 容灾 算法
阿里云 SLS 多云日志接入最佳实践:链路、成本与高可用性优化
本文探讨了如何高效、经济且可靠地将海外应用与基础设施日志统一采集至阿里云日志服务(SLS),解决全球化业务扩展中的关键挑战。重点介绍了高性能日志采集Agent(iLogtail/LoongCollector)在海外场景的应用,推荐使用LoongCollector以获得更优的稳定性和网络容错能力。同时分析了多种网络接入方案,包括公网直连、全球加速优化、阿里云内网及专线/CEN/VPN接入等,并提供了成本优化策略和多目标发送配置指导,帮助企业构建稳定、低成本、高可用的全球日志系统。
785 54
|
11月前
|
监控 安全 Apache
什么是Apache日志?为什么Apache日志分析很重要?
Apache是全球广泛使用的Web服务器软件,支持超过30%的活跃网站。它通过接收和处理HTTP请求,与后端服务器通信,返回响应并记录日志,确保网页请求的快速准确处理。Apache日志分为访问日志和错误日志,对提升用户体验、保障安全及优化性能至关重要。EventLog Analyzer等工具可有效管理和分析这些日志,增强Web服务的安全性和可靠性。
334 9

热门文章

最新文章

推荐镜像

更多