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


grafana监控pg grafana监控日志_Promtail


Grafana+Loki+Promtail

  • 使用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

登录如图

grafana监控pg grafana监控日志_日志系统_02

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: 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 #每张表的时间范围28天

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 # 最大可查询历史日期 28天,这个时间必须是schema_config中的period的倍数,否则报错。

table_manager: # 配置保留多少天的数据,那么之前数据会被清除,Loki中默认保留所有数据
  retention_deletes_enabled: true
  retention_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-windows-amd64.exe --config.file=loki-local-config.yaml

3、安装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: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      job: viplogs
      __path__: F:\soft\grafana\testlogs\*.log
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      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.yaml

4、使用Grafana+loki+promtail查看日志

登录grafana后在Data sources -> Add data source选择loki配置好相应信息即可

grafana监控pg grafana监控日志_Promtail_03


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

grafana监控pg grafana监控日志_Granfana_04


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