【日志系统】Loki日志监控 - 入门初体验

使用Grafana+Loki+Promtail入门级部署分布式日志系统(windows环境)

生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德分享学习心得,欢迎指正,大家一起学习成长!

【日志系统】Loki日志监控 - 入门初体验

文章插图
目录
  • 使用Grafana+Loki+Promtail入门级部署分布式日志系统(windows环境)
    • 简介
    • 1、安装grafana
    • 2、安装loki
    • 3、安装promtail
    • 4、使用Grafana+loki+promtail查看日志
简介由于需求对日志进行监控 , 但是日志又很大 , 不能直接通过流的方式直接将text读取出来 , 可以使用grafana+loki+promtail搭建一个轻量级的日志系统,部署也简单方便 。grafana提供可视化展示日志,然而loki是存储日志和处理查询,Promtail相当于loki的代理,收集日志发给loki 。
1、安装grafana
  • 下载地址:https://grafana.com/grafana/download?platform=windows
  • 下载grafana-9.0.6.windows-amd64.zip安装包即可,并解压到F:\softwarea\grafana\grafana-9.0.6
  • 进入 bin 目录,双击grafana-server.exe启动
  • 启动成功之后,http://localhost:3000
  • 初始登录账户:admin/admin
登录如图
【日志系统】Loki日志监控 - 入门初体验

文章插图
2、安装loki下载地址:https://github.com/grafana/loki/releases下载loki-windows-amd64.exe.zip安装包,并进行解压到F:\soft\grafana\logmanager,解压得到loki-windows-amd64.exe在F:\soft\grafana\logmanager目录下添加loki-local-config.yaml文件,内容如下
auth_enabled: falseserver:http_listen_port: 3100ingester:lifecycler:address: 127.0.0.1ring:kvstore:store: inmemoryreplication_factor: 1final_sleep: 0schunk_idle_period: 5mchunk_retain_period: 30smax_transfer_retries: 0schema_config:configs:- from: 2022-08-06store: boltdbobject_store: filesystemschema: v11index:prefix: index_period: 672h #每张表的时间范围28天storage_config:boltdb:directory: /tmp/loki/index # 索引文件存储地址filesystem:directory: /tmp/loki/chunks # 块存储地址limits_config:enforce_metric_name: falsereject_old_samples: truechunk_store_config:max_look_back_period: 24h # 最大可查询历史日期 28天,这个时间必须是schema_config中的period的倍数,否则报错 。table_manager: # 配置保留多少天的数据,那么之前数据会被清除 , Loki中默认保留所有数据retention_deletes_enabled: trueretention_period: 24h打开cmd定位到exe目录,执行命令:.\loki-windows-amd64.exe --config.file=loki-local-config.yaml,loki服务启动成功配置通信端口号:
server:http_listen_port: 3200 # http访问端口grpc_listen_port: 9096 # 通信端口启动:
【【日志系统】Loki日志监控 - 入门初体验】 .\loki-windows-amd64.exe --config.file=loki-local-config.yaml3、安装promtail下载地址:https://github.com/grafana/loki/releases下载promtail-windows-amd64.exe.zip安装包,并解压到F:\soft\grafana\promtail目录,得到promtail-windows-amd64.exe在F:\soft\grafana\promtail目录下添加promtail-local-config.yaml文件,内容如下
server:http_listen_port: 9080grpc_listen_port: 0positions:filename: /tmp/positions.yamlclients:- url: http://localhost:3100/loki/api/v1/pushscrape_configs:- job_name: systemstatic_configs:- targets:- localhostlabels:job: viplogs__path__: F:\soft\grafana\testlogs\*.log- job_name: systemstatic_configs:- targets:- localhostlabels:job: viplogs__path__: F:\soft\grafana\testlogs\*.log打开cmd定位到exe目录,执行命令: .\promtail-windows-amd64.exe --config.file=promtail-local-config.yaml,loki服务启动成功 。
.\promtail-windows-amd64.exe --config.file=promtail-local-config.yaml4、使用Grafana+loki+promtail查看日志登录grafana后在Data sources -> Add data source选择loki配置好相应信息即可
【日志系统】Loki日志监控 - 入门初体验

文章插图
输入labels标签,右上角可以设置查看时间,然后查询就可以看到,并且支持定时查询日志,笔者暂时没有开启收集日志,并且是使用loki+logback收集的日志(后期会提供相应的篇章介绍),因此截图中没有日志 , 但能看到标签 。
【日志系统】Loki日志监控 - 入门初体验

文章插图
这样就能够查看日志了 , loki是比较轻量级的日志监控系统 , 通过logback可以定制自己打印日志,还能够自己实现可视化,对于一些工业项目中,可以将日志显示在后台上,方便实施人员查看,这个以后会有相关文章来介绍,这个只是简单的介绍 。

推荐阅读