Loki是受Prometheus启发由Grafana Labs团队开源的水平可扩展,高度可用的多租户日志聚合系统。 开发语言: Google Go。它的设计具有很高的成本效益,并且易于操作。使用标签来作为索引,而不是对全文进行检索,也就是说,你通过这些标签既可以查询日志的内容也可以查询到监控的数据签,极大地降低了日志索引的存储。系统架构十分简单,由以下3个部分组成
Loki 是主服务器,负责存储日志和处理查询 。
promtail 是代理,负责收集日志并将其发送给 loki 。
Grafana 用于 UI 展示。
只要在应用程序服务器上安装promtail来收集日志然后发送给Loki存储,就可以在Grafana UI界面通过添加Loki为数据源进行日志查询(如果Loki服务器性能不够,可以部署多个Loki进行存储及查询)。作为一个日志系统不关只有查询分析日志的能力,还能对日志进行监控和报警
loki文档地址:Loki Documentation | Grafana Labs
下载地址: Releases · grafana/loki · GitHub
Server:172.16.200.6 loki grafana
Client:172.16.200.7 promtail
loki&promtail 通过yml启动
[root@loki loki]# mkdir -p /usr/local/loki && mkdir -p /data/loki/{index,chunks}
[root@loki loki]# cd /usr/local/loki/
[root@loki loki]# wget https://github.com/grafana/loki/releases/download/v2.3.0/loki-linux-amd64.zip
[root@loki loki]# wget https://dl.grafana.com/oss/release/grafana-7.1.0-1.x86_64.rpm
[root@loki loki]# vim /usr/local/loki/loki.yaml
auth_enabled: false
server
http_listen_port: 3100
ingester:
lifecycler:
address: 172.16.200.6
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 5m
chunk_retain_period: 30s
schema_config:
configs:
- from: 2021-09-01
store: boltdb
object_store: filesystem
schema: v9
index:
prefix: index_
period: 168h #每张表的时间范围内7天
storage_config:
#标签索引存储地址
boltdb:
directory: /data/loki/index
#块文件存储地址
filesystem:
directory: /data/loki/chunks
# boltdb_shipper:
#active_index_directory: /home/data/loki
#cache_location: /home/data/loki/cache
#cache_ttl: 24h
#shared_store: s3
#aws:
#bucketnames: jn-log
# endpoint: oss-cn-hangzhou.aliyuncs.com
#region: oss-cn-hangzhou
#access_key_id: LTAI5tJrTDshuWHMDdQURu2r
#secret_access_key: LCl9123WlJvxrPZODdV5fn7z5pwRcH
limits_config:
#enforce_metric_name: false
reject_old_samples: true
reject_old_samples_max_age: 168h
ingestion_rate_mb: 20
#chunk_store_config:
#max_look_back_period: 720h
table_manager:
retention_deletes_enabled: true
retention_period: 672h
#table_manager:
# chunk_tables_provisioning:
# inactive_read_throughput: 0
# inactive_write_throughput: 0
# provisioned_read_throughput: 0
# provisioned_write_throughput: 0
# index_tables_provisioning:
# inactive_read_throughput: 0
# inactive_write_throughput: 0
# provisioned_read_throughput: 0
# provisioned_write_throughput: 0
# retention_deletes_enabled: false
# retention_period: 0
[root@loki loki]# vim /etc/systemd/system/service
loki.service
[Unit]
Description=loki
After=network.target
[Service]
ExecStart=/usr/local/loki/loki-linux-amd64 \
-config.file=/usr/local/loki/loki.yml &>> /usr/local/loki/loki-3100.log
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@loki loki]# systemctl daemon-reload
[root@loki loki]# systemctl restart loki grafana && systemctl enable loki grafana
[root@loki loki]# netstat -nutlp |grep loki
tcp6 0 0 :::3100 :::* LISTEN 6761/loki-linux-amd
tcp6 0 0 :::9095 :::* LISTEN 6761/loki-linux-amd
[root@loki loki]# ps -ef |grep loki
root 6761 1 0 Sep01 ? 00:00:28 /usr/local/loki/loki-linux-amd64 -config.file=/usr/local/loki/loki.yml &>> /usr/local/loki/loki-3100.log
root 8523 10196 0 11:06 pts/0 00:00:00 grep --color=auto loki
配置promtail
[root@promtail ~]# mkdir -p /usr/local/promtail/
[root@promtail ~]# wget "https://github.com/grafana/loki/releases/download/v2.3.0/promtail-linux-amd64.zip"
[root@promtail ~]# vim /usr/local/promtail/promtail.yaml
# Promtail Server Config
server:
http_listen_port: 9080
grpc_listen_port: 0
# Positions
positions:
filename: /tmp/positions.yaml
# Loki服务器的地址
clients:
- url: http://172.16.200.6:3100/loki/api/v1/push
scrape_configs:
- job_name: systemd #区分从其他日志组收集的日志
static_configs:
- targets:
- 172.16.200.7
labels:
job: systemd
host: 172.16.200.7
__path__: /var/log/messages #收集日志的路径
[root@promtail ~]# vim /etc/systemd/system/promtail.service
[Unit]
Description=promtail
After=network.target
[Service]
ExecStart=/usr/local/promtail/promtail-linux-amd64 \
-config.file=/usr/local/promtail/promtail.yaml &>> /usr/local/promtail/promtail-9080.log
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@promtail ~]# systemctl daemon-reload
[root@promtail ~]# systemctl start promtail && systemctl enable promtail
[root@promtail ~]# netstat -nutlp |grep promtail
tcp6 0 0 :::35539 :::* LISTEN 5941/promtail-linux
tcp6 0 0 :::9080 :::* LISTEN 5941/promtail-linux
访问grafana https://172.16.200.6:3000/
用户 admin
密码 admin
输入Loki的服务地址
查询方式/正则匹配/help有示例