介绍:Loki 由以下3个部分组成:
loki是主服务器,负责存储日志和处理查询。 promtail是代理,负责收集日志并将其发送给 loki 。 Grafana用于 UI 展示。
一、promtail 部署 下载地址:https://github.com/grafana/loki/releases/download/v2.5.0/promtail-linux-amd64.zip
1、解压,下载官方配置模板并修改
mkdir /data/promtail
unzip promtail-linux-amd64.zip
wget https://raw.githubusercontent.com/grafana/loki/main/clients/cmd/promtail/promtail-local-config.yaml
vim promtail-local-config.yaml
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://192.156.71.125:3100/loki/api/v1/push ## 此处lokiserver修改为服务器端地址
scrape_configs:
- job_name: application # job名称,自定义
static_configs:
- targets: # 如测试环境多个应用多个路径,从此行开始复制修改对应的标签
- localhost
labels:
job: tomcat # 定义检索的名称
project: tjhlwjg # 项目名称自定义
host: 192.156.71.125 # 建议修改为本机ip,方便过滤
__path__: /data/tomcat_tjjg/logs/catalina.out # tomcat日志路径
2、启动promtail
cd /data/promtail
nohup ./promtail-linux-amd64 --config.file=promtail.yaml &
二、loki 部署 下载地址:https://github.com/grafana/loki/releases/download/v2.5.0/loki-linux-amd64.zip
1、解压,下载官方配置模板并修改
mkdir /data/loki
unzip loki-linux-amd64
wget https://raw.githubusercontent.com/grafana/loki/master/cmd/loki/loki-local-config.yaml
vim loki-local-config.yaml # 这里不需要alertmanager,注释掉
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
common:
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
instance_addr: 127.0.0.1
kvstore:
store: inmemory
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h
# ruler:
# alertmanager_url: http://localhost:9093
# 下面的配置为新增的,不配置日志太大会报错
limits_config:
enforce_metric_name: false
reject_old_samples: true
reject_old_samples_max_age: 168h
ingestion_rate_mb: 30 #修改每用户摄入速率限制,即每秒样本量,默认值为4M
ingestion_burst_size_mb: 15 #修改每用户摄入速率限制,即每秒样本量,默认值为6M
grfana面板出图有问题 加的如下配置
limits_config:
reject_old_samples: true
reject_old_samples_max_age: 168h
retention_period: 360h
max_query_series: 100000
max_query_parallelism: 2
2、启动loki
cd /data/loki
nohup ./loki-linux-amd64 --config.file=loki.yaml &
三、配置grafana
1、添加loki数据源
2、输入loki服务器的ip和端口(3100)、其他默认,然后Save&Test
3、在Explore中选择loki,可以根据自定义的标签进行过滤
4、loki的日志页面如下
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",'
'"geoip_country_code": "$geoip_country_code"'
'}';
--with-http_geoip_module 这个模块需要单独编译 tengjin自带