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

2022-12-06 20:08:42 浏览数 (1)

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

Loki简单介绍

Loki:像 Prometheus,但用于日志。

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 的不同之处在于它专注于日志而不是指标,并通过推送而不是拉取来交付日志

(图片点击放大查看)

一、安装并配置Loki服务端

代码语言:javascript复制
#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

(图片点击放大查看)

(图片点击放大查看)

(图片点击放大查看)

代码语言:javascript复制
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

(图片点击放大查看)

(图片点击放大查看)

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

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

代码语言:javascript复制
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进行测试

(图片点击放大查看)

(图片点击放大查看)

(图片点击放大查看)

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

代码语言:javascript复制
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

(图片点击放大查看)

(图片点击放大查看)

三、Grafana上导入Loki的大屏

1、grafana的安装

granfan安装在Loki服务器上

代码语言:javascript复制
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

(图片点击放大查看)

(图片点击放大查看)

2、导入12559的dashboard ID

代码语言:javascript复制
https://grafana.com/grafana/dashboards/12559-grafana-loki-dashboard-for-nginx-service-mesh/

(图片点击放大查看)

(图片点击放大查看)

四、效果测试

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

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

0 人点赞