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

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

相关实践学习
通过可观测可视化Grafana版进行数据可视化展示与分析
使用可观测可视化Grafana版进行数据可视化展示与分析。
相关文章
|
9天前
|
存储 运维 监控
超越传统模型:从零开始构建高效的日志分析平台——基于Elasticsearch的实战指南
【10月更文挑战第8天】随着互联网应用和微服务架构的普及,系统产生的日志数据量日益增长。有效地收集、存储、检索和分析这些日志对于监控系统健康状态、快速定位问题以及优化性能至关重要。Elasticsearch 作为一种分布式的搜索和分析引擎,以其强大的全文检索能力和实时数据分析能力成为日志处理的理想选择。
44 6
|
1月前
|
存储 消息中间件 网络协议
日志平台-ELK实操系列(一)
日志平台-ELK实操系列(一)
|
2月前
|
存储 监控 Serverless
阿里泛日志设计与实践问题之Grafana Loki在日志查询方案中存在哪些设计限制,如何解决
阿里泛日志设计与实践问题之Grafana Loki在日志查询方案中存在哪些设计限制,如何解决
|
2月前
|
应用服务中间件 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 如何处理?
167 6
|
2月前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
2月前
|
存储 监控 应用服务中间件
查看nginx日志文件
器性能和提高网站可用性。掌握日志文件的路径、查看方法和基本分析技能对于任何服务器管理员来说都是必备技能。
99 1
|
2月前
|
存储 Ubuntu 应用服务中间件
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
27 4
|
1月前
|
运维 Kubernetes 监控
Loki+Promtail+Grafana监控K8s日志
综上,Loki+Promtail+Grafana 监控组合对于在 K8s 环境中优化日志管理至关重要,它不仅提供了强大且易于扩展的日志收集与汇总工具,还有可视化这些日志的能力。通过有效地使用这套工具,可以显著地提高对应用的运维监控能力和故障诊断效率。
190 0
|
2月前
|
监控 Java Serverless
美团 Flink 大作业部署问题之想在Serverless平台上实时查看Spring Boot应用的日志要怎么操作
美团 Flink 大作业部署问题之想在Serverless平台上实时查看Spring Boot应用的日志要怎么操作
|
2月前
|
Prometheus 监控 Cloud Native
在Linux中,如何使用Grafana和Prometheus进行网络监控和可视化?
在Linux中,如何使用Grafana和Prometheus进行网络监控和可视化?