1、背景
在分布式系统中,众多服务分散部署在数十台甚至是上百台不同的服务器上,要想快速方便的实现查找、分析和归档等功能,使用Linux命令等传统的方式查询到想要的日志就费时费力,更不要说对日志进行分析与归纳。如果说有一个统一管理的日志平台,那么对于我们快速查询日志和日志分析,那么就会有意想不到的收获,而且能够快速提高查询自己和Bug的效率
2、Loki日志平台官网
https://grafana.com/docs/loki/latest/logql/log_queries/
可以从官网查询到一些学习资料
3、Loki日志平台简介
Loki 是 Grafana Labs 团队最新的开源项目,是一个水平可扩展,高可用性,多租户的日志聚合系统。它的设计非常经济高效且易于操作,因为它不会为日志内容编制索引,而是为每个日志流编制一组标签,可以根据标签搜索日志,高效化管理日志;
4、 Loki组件
搭建Loki平台需要安装三个组件,Loki、promtail和Grafana,组件介绍如下:
(1)Loki 是主服务器,负责存储日志和处理查询 。
(2)promtail 是代理,负责收集日志并将其发送给 loki(只收集日志,不在本地进行运算)
(3)Grafana 用于 UI 展示,只要在被监控服务器上安装promtail来收集日志然后发送给Loki存储,就可以在Grafana UI界面通过添加Loki为数据源进行日志查询;
备注:在程序开发中,你也可以不使用Grafana提供的日志UI界面,因为Loki提供了一系列接口,你可以自己使用接口,实现自己的日志界面
5、Loki安装
(1)Loki安装方式
Loki提供了几种安装方式,安装包安装或者docker安装等,安装包可以从下面链接下载: github.com/grafana/lok…
(2)安装Loki
配置如下:
auth_enabled: false
server:
http_listen_port: 3100
ingester:
lifecycler:
address: 127.0.0.1
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 5m
chunk_retain_period: 30s
max_transfer_retries: 0
schema_config:
configs:
- from: 2022-08-06
store: boltdb
object_store: filesystem
schema: v11
index:
prefix: index_
period: 672h
storage_config:
boltdb:
directory: /tmp/loki/index
filesystem:
directory: /tmp/loki/chunks
limits_config:
enforce_metric_name: false
reject_old_samples: true
chunk_store_config:
max_look_back_period: 24h
table_manager:
retention_deletes_enabled: true
retention_period: 24h
启动Loki使用以下命令:
.\loki-windows-amd64.exe --config.file=loki-local-config.yaml
(3)promtail安装:
配置如下:
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://localhost:3100/loki/api/v1/push
scrape_configs:
- job_name: system1
pipeline_stages:
- match:
selector: '{job="om"} |= "c.c.u.o.s.impl.ServerHealthServiceImpl"'
stages:
- regex:
expression: .*ServerHealthServiceImpl(?P<output>.*)
- output:
source: output
static_configs:
- targets:
- localhost1
labels:
job: om
env: dev1
__path__: /home/smarthome/logs/om/om.log
- job_name: system
pipeline_stages:
static_configs:
- targets:
- localhost
labels:
job: syslog
env: dev
__path__: /home/smarthome/logs/*.log
- job_name: system
pipeline_stages:
static_configs:
- targets:
- localhost
labels:
job: apache
env: dev
__path__: /home/smarthome/logs/devicecenter
- job_name: system
pipeline_stages:
- regex:
expression: "^(?P<ip>\\S+) (?P<identd>\\S+) (?P<user>\\S+) \\[(?P<timestamp>[\\w:/]+\\s[+\\-]\\d{4})\\] \"(?P<action>\\S+)\\s?(?P<path>\\S+)?\\s?(?P<protocol>\\S+)?\" (?P<status_code>\\d{3}|-) (?P<size>\\d+|-)\\s?\"?(?P<referer>[^\"]*)\"?\\s?\"?(?P<useragent>[^\"]*)?\"?$"
- labels:
ip:
action:
status_code:
static_configs:
- targets:
- localhost
labels:
job: apache
env: dev
__path__: /home/smarthome/logs/devicecenter
启动promtail使用以下命令:
.\promtail-windows-amd64.exe --config.file=promtail-local-config.yaml
(4)grafana安装
grafana安装链接可以从以下链接下载:
https://grafana.com/grafana/download?platform=windows
备注:或者可以直接使用dokcer安装或者docker-compose安装,这个看个人习惯
6、界面操作
(1)安装完成之后,你可以测试一下是否安装完成,可以访问9080,测试一下promtail是否成功启动,成功会显示以下界面:
(2)访问grafana界面,默认端口是3000,进入以下界面:
默认账号密码是admin,admin;
(3)测试是否连接上loki: 点击左边菜单栏中的Configuration,进入Data sources,搜索Loki,后进入:
在url中输入loki的地址和端口,点击save&Test,成功的话,会显示:
错误的话,检查你的日志文件是否存在等等;
(4)点击左边菜单栏,进入explore,就可以查询日志了:
总结
Loki提供了一系列强大的功能,并没有根据大部分的日志平台一样,采用全文索引,而且根据标签索引,所以合理的使用标签很重要