轻量级日志系统Loki搭建与使用
参考
https://blog.csdn.net/wzpny/article/details/139833234
官方文档
https://grafana.com/docs/loki/latest/
- Loki简介
Loki是 Grafana Labs 团队最新的开源项目,是一个水平可扩展,高可用性,多租户的日志聚合系统。它的设计非常经济高效且易于操作,因为它不会为日志内容编制索引,而是为每个日志流编制一组标签。项目受 Prometheus 启发,官方的介绍就是: Like Prometheus, but for logs ,类似于 Prometheus 的日志系统。 - Loki快速上手
Loki 作为日志系统的后起之秀,设计上可以说非常优秀,设计的理念就是为了让日志聚合更简单,它被设计为非常经济高效且易于操作。它不索引日志的内容,而是为每个日志流设置一组标签。它主要由三部分组成。
与其他日志聚合系统相比, Loki 具有下面的一些特性:
- 不对日志进行全文索引。通过存储压缩非结构化日志和仅索引元数据, Loki 操作起来会更简单,更省成本。
- 通过使用与 Prometheus 相同的标签记录流对日志进行索引和分组,这使得日志的扩展和操作效率更高。
- 特别适合储存 Kubernetes Pod 日志 ; 诸如 Pod 标签之类的元数据会被自动删除和编入索引
安装Loki
简单介绍下 Loki :
Grafana Loki 是一个日志聚合工具,它是功能齐全的日志堆栈的核心。
Loki 是一个为有效保存日志数据而优化的数据存储。日志数据的高效索引将 Loki 与其他日志系统区分开来,
与其他日志系统不同, Loki 索引是根据标签构建的,原始日志消息未编入索引。
环境查看
# cat /etc/redhat-release
Rocky Linux release 9.3 (Blue Onyx)
# uname -a
Linux RockyEs02003082 5.14.0-362.18.1.el9_3.0.1.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Feb 11 13:49:23 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
下载Loki,下载地址
https://github.com/grafana/loki/releases/
因为是Rocky系统本次下载rpm包
安装
# rpm -ivh loki-3.2.0.x86_64.rpm
修改配置文件
# cat /etc/loki/config.yml
auth_enabled: false
server:
# http访问端口
http_listen_port: 3100
# rpc访问端口
grpc_listen_port: 9096
log_level: debug
grpc_server_max_concurrent_streams: 1000
common:
# 访问地址修改为本机ip地址
instance_addr: 192.168.3.82
path_prefix: /tmp/loki
storage:
filesystem:
# 记录块存储目录,默认chunks块上的日志数量后到期后将chunks数据打标签后存储
chunks_directory: /tmp/loki/chunks
# 规则配置目录
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
ingester_rf1:
enabled: false
# 查询规则
query_range:
# 结果缓存
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100
# 配置索引信息
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
# 索引前缀
prefix: index_
# 索引时长
period: 24h
pattern_ingester:
enabled: true
metric_aggregation:
enabled: true
loki_address: localhost:3100
ruler:
alertmanager_url: http://localhost:9093
frontend:
encoding: protobuf
# By default, Loki will send anonymous, but uniquely-identifiable usage and configuration
# analytics to Grafana Labs. These statistics are sent to https://stats.grafana.org/
#
# Statistics help us better understand how Loki is used, and they show us performance
# levels for most users. This helps us prioritize features and documentation.
# For more information on what's sent, look at
# https://github.com/grafana/loki/blob/main/pkg/analytics/stats.go
# Refer to the buildReport method to see what goes into a report.
#
# If you would like to disable reporting, uncomment the following lines:
#analytics:
# reporting_enabled: false
启动
# systemctl restart loki
# systemctl enable loki
# systemctl status loki
安装Promtail
# rpm -ivh promtail-3.2.0.x86_64.rpm
修改配置文件
]# cat /etc/promtail/config.yml
# This minimal config scrape only single log file.
# Primarily used in rpm/deb packaging where promtail service can be started during system init process.
# And too much scraping during init process can overload the complete system.
# https://github.com/grafana/loki/issues/11398
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
# 用于记录每次读取日志文件的索引行数
# 如promtail重启后从配置中恢复日志文件的读取位
filename: /tmp/positions.yaml
# 推送日志流到Loki
clients:
- url: http://192.168.3.82:3100/loki/api/v1/push
# 发现日志文件的位置并从中提取标签
scrape_configs:
# 任务名称
- job_name: system
# 目录配置
static_configs:
# 标签
- targets:
- localhost
# 子任务名称,通常以项目命名
labels:
job: varlogs
# 注意需要给日志赋予用户promtail对应权限
#NOTE: Need to be modified to scrape any additional logs of the system.
# 要读取日志文件的位置,允许使用调配符号
__path__: /var/log/messages
赋予权限
必须操作否则无法读取日志
# setfacl -m u:promtail:r /var/log/messages
启动
# systemctl start promtail
# systemctl enable promtail
# systemctl status promtail
安装granafa
#sudo yum install -y https://dl.grafana.com/oss/release/grafana-11.2.0-1.x86_64.rpm
启动
# systemctl daemon-reload
# systemctl enable grafana-server
# systemctl start grafana-server
访问
# http://ip:3000
默认用户名和密码admin admin
登录后修改密码
修改为中文
添加Lokis数据源
浏览数据源
- LogQL语法
选择器
对于查询表达式的标签部分,将放在 {} 中,多个标签表达式用逗号分隔:
{app="mysql",name="mysql-backup"}
支持的符号有:
= :完全相同。
!= :不平等。
=~ :正则表达式匹配。
!~ :不要正则表达式匹配。
过滤表达式
编写日志流选择器后,您可以通过编写搜索表达式进一步过滤结果。搜索表达式可以文本或正则表达式。 如
{job=“mysql”} |= “error”
{name=“kafka”} |~ “tsdb-ops.*io:2003”
{instance=~“kafka-[23]”,name=“kafka”} != kafka.server:type=ReplicaManager
支持多个过滤:
{job=“mysql”} |= “error” != “timeout”
目前支持的操作符
|= line 包含字符串。
!= line 不包含字符串。
|~ line 匹配正则表达式。
!~ line 与正则表达式不匹配。
- Loki收集Nginx日志
修改nginx配置文件增加以下配置
log_format json escape=json '{'
'"remote_addr": "$remote_addr", '
'"request_uri": "$request_uri", '
'"request_length": "$request_length", '
'"request_time": "$request_time", '
'"request_method": "$request_method", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_host": "$http_host", '
'"server_name": "$server_name", '
'"upstream": "$upstream_addr", '
'"upstream_response_time": "$upstream_response_time", '
'"upstream_status": "$upstream_status", '
#'"geoip_country_code": "$geoip2_data_country_code", '
#'"geoip_country_name": "$geoip2_data_country_name", '
#'"geoip_city_name": "$geoip2_data_city_name"'
'}';
设置日志输出为json
在sever段增加以下配置
# access_log logs/json_access.log json;
如果设置了https则访问http会强制跳转https所以需要设置在https的server段
nginx服务器安装Promtail并修改配置
# cat /etc/promtail/config.yml
# This minimal config scrape only single log file.
# Primarily used in rpm/deb packaging where promtail service can be started during system init process.
# And too much scraping during init process can overload the complete system.
# https://github.com/grafana/loki/issues/11398
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://192.168.3.82:3100/loki/api/v1/push
scrape_configs:
- job_name: nginx
static_configs:
- targets:
- localhost
labels:
job: nginxlogs
host: 192.168.3.218
#NOTE: Need to be modified to scrape any additional logs of the system.
__path__: /usr/local/nginx/logs/json_access.log
设置日志权限
# setfacl -R -m u:promtail:rx /var/log/nginx/
在 Grafana添加Dashboard
id为16101
查看Dashboard
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2019-09-20 Docker之使用Docker-compose搭建LNMP环境