参考官方文档:
Loki: https://grafana.com/docs/loki/latest/
Grafana Loki V2 Dashboard for Nginx https://grafana.com/grafana/dashboards/12559?pg=dashboards&plcmt=featured-main
先看下最终效果截图:
官网有说明。
简单理解,
Loki是个日志监控系统,可用来分析日志,并提供给Grafana进行图形化展示。
Promtail是个日志转发的代理,用于给Loki提供日志数据的。
wget https://raw.githubusercontent.com/grafana/loki/v2.1.0/cmd/loki/loki-local-config.yaml -O loki-config.yaml
docker run -v $(pwd):/mnt/config -p 3100:3100 grafana/loki:2.1.0 -config.file=/mnt/config/loki-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/v2.1.0/cmd/promtail/promtail-docker-config.yaml -O promtail-config.yaml
docker run -v $(pwd):/mnt/config -v /usr/local/nginx/logs:/usr/local/nginx/logs grafana/promtail:2.1.0 -config.file=/mnt/config/promtail-config.yaml
以上脚本来自官网,容器启动后,先停止,修改配置文件。
log_format json_analytics '{'
'"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 logs/json_access.log json_analytics;
server:
http_listen_port: 0
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://172.17.0.4:3100/loki/api/v1/push # Loki日志推送地址
scrape_configs:
- job_name: system
#pipeline_stages:
#- replace:
# expression: '(?:[0-9]{1,3}\.){3}([0-9]{1,3})'
# replace: '***'
static_configs:
- targets:
- localhost # 根据官方文档,这里只能是localhost
labels:
job: nginx_access_log
agent: promtail
__path__: /usr/local/nginx/logs/json_access.log # 需要上传的日志地址。
root@daicytest01:/usr/local/nginx/logs# docker container restart 49ae3da9bd4d
49ae3da9bd4d
root@daicytest01:/usr/local/nginx/logs# docker container restart d3c292252aa4
d3c292252aa4
配置方式跟上一篇(Grafana搭建及vts监控)的配置Prometheus数据源相同
模版ID:12559
配置模版方式同上一篇配置vts-nginx模版。
总请求量
状态码 2xx,3xx,5xx请求量
流量情况
请求明细
百分之95请求响应时间
最大请求IP
最大请求路径
.....
Country面板无数据,是因为Grafana没有地图面板插件,且Nginx默认没有开启国家城市过滤。
下一篇为大家添加该插件并展示地图信息
查看资源消耗。
loki在内存消耗上还不小,每15秒钟,占用内存22.5%,大概440M左右内存消耗
promtail在内存消耗,大概每10秒一次,占用内存2.7%,大概40-50M内存消耗。
promtail占用资源还好,但loki部署时,需要考虑不要和应用服务部署在一台电脑。