如何搭建 Loki开源日志系统?
介绍
Loki开源日志解决方案已经开源有一段时间了,对标EFK/ELK,由于其轻量的设计,备受欢迎
Loki相比EFK/ELK,它不对原始日志进行索引,只对日志的标签进行索引,而日志通过压缩进行存储,通常是文件系统存储,所以其操作成本更低,数量级效率更高
由于Loki的存储都是基于文件系统的,所以它的日志搜索时基于内容即日志行中的文本,所以它的查询支持LogQL,在搜索窗口中通过过滤标签的方式进行搜索和查询
Loki分两部分,Loki是日志引擎部分,Promtail是收集日志端,然后通过Grafana进行展示
Loki的官方文档很全
https://grafana.com/docs/loki/latest/installation
Loki·安装
从官方文档看,Loki支持源码安装、Docker、Helm、Local、Tanka
我选择local,就是编译好的二进制可执行文件
安装步骤:
下载:https://github.com/grafana/loki/releases/
curl -O -L "https://github.com/grafana/loki/releases/download/v2.2.1/loki-linux-amd64.zip"
通过unzip解压得到可执行文件
下载配置文件
wget https://raw.githubusercontent.com/grafana/loki/master/cmd/loki/loki-local-config.yaml
Loki·配置
Loki的配置文件是loki-local-config.yaml,支持YAML语法
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
ingester:
# wal:
# enabled: true
# dir: /tmp/wal
# recover: true
lifecycler:
address: 192.168.80.144
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 1h # Any chunk not receiving new logs in this time will be flushed
max_chunk_age: 1h # All chunks will be flushed when they hit this age, default is 1h
chunk_target_size: 10485760 # Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached first
chunk_retain_period: 30s # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)
max_transfer_retries: 0 # Chunk transfers disabled
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h #每张表的时间范围24小时
storage_config:
boltdb_shipper: #索引文件存储地址
active_index_directory: /tmp/loki/boltdb-shipper-active
cache_location: /tmp/loki/boltdb-shipper-cache
cache_ttl: 24h # Can be increased for faster performance over longer query periods, uses more disk space
shared_store: filesystem
filesystem:
directory: /tmp/loki/chunks #块存储地址
compactor:
working_directory: /tmp/loki/boltdb-shipper-compactor
shared_store: filesystem
limits_config:
reject_old_samples: true
reject_old_samples_max_age: 168h
ingestion_rate_mb: 32
ingestion_burst_size_mb: 64
chunk_store_config:
#最大可查询历史日期90天
# max_look_back_period: 2160h
# max_look_back_period: 0s
max_look_back_period: 168h
#表的保留期为90天
#table_manager:
# retention_deletes_enabled: true
# retention_period: 2160h
table_manager:
retention_deletes_enabled: true
retention_period: 168h
loki提供http和grpc两种接口,在配置文件中可以看到对应端口,分别是3100和9096,启动后,就可以通过http接口看到数据
配置完成后,就可以启动loki了,二进制可执行文件,通过 --config.file指定配置文件启动即可
promtail·安装
Promtail安装也很简单
下载可执行文件
curl -O -L https://github.com/grafana/loki/releases/download/v2.2.1/promtail-linux-amd64.zip
通过unzip解压得到可执行文件即可
同样下载配置文件
wget https://raw.githubusercontent.com/grafana/loki/main/clients/cmd/promtail/promtail-local-config.yaml
之后,就可以直接可执行文件指定配置文件启动了
# Promtail Server Config
server:
http_listen_port: 9080
grpc_listen_port: 0
# Positions
positions:
filename: /tmp/positions.yaml
# Loki服务器的地址
clients:
- url: http://192.168.80.144:3100/loki/api/v1/push
scrape_configs:
- job_name: bjj
static_configs:
- targets:
- 192.168.80.176targetsa
labels:
job: tomcat
host: 192.168.80.176
__path__: /data/tomcat-*/logs/catalina.out
前面三部分很简单,server部分定义监听端口,positions定义读取的文件偏移量存储位置,clients定义loki接口地址,最后一部分scrape_configs是重点部分
-
promtail通过scrape_configs部分配置收集日志的相关信息,以测试配置文件为例:
-
job_name 用来区分日志组
-
static_configs 收集日志的静态配置
-
targets 收集日志的节点,这个参数其实是在自动发现的时候使用的
-
labels 定义一个要收集的日志文件和一组可选的附加标签
-
job 标签名称,在grafana索引的时候用到的标签名称
-
path 定义日志收集的文件或路径,支持正则
配置文件修改完成后,就可以启动promtail了,和loki启动方法一样,通过 --config.file指定配置文件启动
promtail,类似于tail,它只监听新增日志,不会像filebeat一样,读取日志所有内容,这是和filebeat的一个区别
Grafana·展示
好了,promtail和Loki都配置好了,需要展示,6.0以上版本的grafana就支持loki的展示了
grafana配置很简单,登录后,在设置——数据源中,选择添加数据源,下来列表中直接选择Loki即可
接着配置数据源,只需要修改URL即可,其他可以根据自己的需要
因为我的loki是没有做认证的,所以这里不需要认证配置,直接填写loki的地址即可,完成后保存
保存完成后,在Explore中查看日志
这里支持LogQL语法,默认情况下,如果不添加规则,是没有日志展示的,在Log browser中添加query,根据在promtail中定义的label,比如我的promtail的job是nginx,我这里填写{job=“nginx”},然后run query
就可以看到nginx的日志
日志界面很清爽,保留了日志原有的样子,通过颜色区分日志级别,这里info是绿色,error是红色,可以很直观的看到日志的类别,也可以通过点击日志级别,只展示该级别的日志
总结
相对于EFK/ELK,专为日志而生的Loki,结合Grafana生态,对于中小企业,或者说只是作为日志系统而言,我更倾向于选择Loki代替EFK/ELK
优快云_码404:如何搭建 Loki开源日志系统?
https://www.code404.icu/1439.html