Loki安装配置

文档参考
https://blog.51cto.com/u_16213620/7782631
github官网地址
https://github.com/grafana/loki?tab=readme-ov-file
各种安装方式,我们选择locally方式
https://grafana.com/docs/loki/latest/setup/install/
选择安装的版本
https://github.com/grafana/loki/releases
https://grafana.com/docs/loki/latest/configuration/examples/ # loki.yaml配置示例 https://grafana.com/docs/loki/latest/operations/storage/table-manager/ # Loki日志保留时间设置
https://grafana.com/docs/loki/latest/clients/promtail/ # promtail.yaml

grafana模版
https://grafana.com/grafana/dashboards/13639-logs-app/

测试环境
在日志服务器 上创建目录 /usr/local/loki 用于安装日志服务
阿里ECS缺失高版本类库,我们选择v2.8.8版本

下载文件
进入目录

mkdir /usr/local/monitlog && cd /usr/local/monitlog

下载loki

curl -O -L "https://github.com/grafana/loki/releases/download/v2.8.8/loki-linux-amd64.zip"

解压

解压之后只有一个二进制文件loki-linux-amd64

unzip "loki-linux-amd64.zip"

sudo yum install unzip

授权

chmod a+x "loki-linux-amd64"
如果没有unzip
sudo yum install unzip

下载promtail

curl -O -L "https://github.com/grafana/loki/releases/download/v2.8.8/promtail-linux-amd64.zip"

解压

解压之后只有一个二进制文件promtail-linux-amd64

unzip "promtail-linux-amd64.zip"

授权

chmod a+x "promtail-linux-amd64"
chmod -R u+rw /bbc

下载配置文件
wget https://raw.githubusercontent.com/grafana/loki/v2.8.8/cmd/loki/loki-local-config.yaml

wget https://raw.githubusercontent.com/grafana/loki/v2.8.8/clients/cmd/promtail/promtail-local-config.yaml
loki修改配置
loki-local-config.yaml 添加 数据保留时间、写入速度等优化
auth_enabled: false

server:
http_listen_port: 3100
grpc_listen_port: 19096
grpc_server_max_recv_msg_size: 18388608
grpc_server_max_send_msg_size: 18388608

common:
instance_addr: 127.0.0.1
path_prefix: /bbc/loki
storage:
filesystem:
chunks_directory: /bbc/loki/chunks
rules_directory: /bbc/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory

query_range:
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100

schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h

ruler:
alertmanager_url: http://localhost:9093
enable_api: true
enable_alertmanager_v2: true
enable_alertmanager_discovery: false
alertmanager_refresh_interval: 3m0s
alertmanager_url: http://8.219.198.22:8893
storage:
type: local
local:
directory: /usr/local/monitlog/loki-rules

compactor:
retention_enabled: true
compaction_interval: 60m
retention_delete_delay: 5m
retention_delete_worker_count: 150

limits_config:
retention_period: 20d
ingestion_rate_mb: 100
ingestion_burst_size_mb: 200
per_stream_rate_limit: 30MB
per_stream_rate_limit_burst: 60MB
reject_old_samples: true
reject_old_samples_max_age: 48h
失效设置
https://blog.csdn.net/chechenshi/article/details/129300229
Loki配置优化
注意loki版本,高版本配置不支持,会导致无法启动。
https://grafana.com/docs/loki/v3.0.x/configure/
https://grafana.com/docs/loki/v2.8.x/configuration/#storage_config

limits_config:
ingestion_rate_mb: 100 # 每分钟允许的写入速率(MB)
ingestion_burst_size_mb: 200 # 允许的写入突发大小(MB)
max_label_names_per_series: 50 # 每个系列允许的最大标签数
max_label_name_length: 1024 # 标签名的最大长度
max_label_value_length: 2048 # 标签值的最大长度
reject_old_samples: true # 拒绝旧样本
reject_old_samples_max_age: 72h # 72小时之前的样本被拒绝
max_entries_limit_per_query: 5000 # 每个查询的最大条目数限制

promtail修改配置文件
https://grafana.com/docs/loki/latest/send-data/promtail/configuration/#pipeline_stages
promtail-local-config.yaml
server:
http_listen_port: 19080
grpc_listen_port: 0

grpc最大接收消息值,默认4M 改为41943040

grpc_server_max_recv_msg_size: 41943040
grpc_server_max_send_msg_size: 41943040

clients:

scrape_configs:

  • job_name: cktest_id
    pipeline_stages:

    • timestamp:
      source: timestamp
      format: RFC3339Nano

      format: "2006-01-02T15:04:05Z07:00"

      location: Asia/Shanghai

    static_configs:

    • targets:
      • localhost
        labels:
        job: service-logs-id
        path: /bbc/tmp/app_log/id/*.log
  • job_name: cktest_th
    static_configs:

    • targets:
      • localhost
        labels:
        job: service-logs-th
        path: /bbc/tmp/app_log/th/*.log
  • job_name: app_log_pf
    static_configs:

    • targets:
      • localhost
        labels:
        job: service-logs-pf
        path: /bbc/tmp/app_log/pf/*.log
  • job_name: app_log_my
    static_configs:

    • targets:
      • localhost
        labels:
        job: service-logs-my
        path: /bbc/tmp/app_log/my/*.log

启动服务

启动loki

nohup ./loki-linux-amd64 -config.file=loki-local-config.yaml > loki_server.log 2>&1 &
调试时用
./loki-linux-amd64 -config.file=loki-local-config.yaml

启动promtail

nohup ./promtail-linux-amd64 -config.file=promtail-local-config.yaml > promtail_server.log 2>&1 &

调试时用
./promtail-linux-amd64 -config.file=promtail-local-config.yaml

查看进程

ps -ef | grep loki
ps -ef | grep promtail

关闭进程

kill -9 进程ID

ps -ef |grep loki |awk '{print $2}'|xargs kill -9

ps -ef |grep promtail |awk '{print $2}'|xargs kill -9

搭建验证
curl -X GET http://localhost:3100/metrics

grafana使用

查询语法参考
Loki查询语法
https://www.feiyiblog.com/2021/10/11/Loki的LogQL查询语法/

https://www.51cto.com/article/712649.html
案例: {filename="/bbc/log_haibeilizhi_id/business-service.log"} |= "查询条件"

Loki介绍
Loki 是一个由 Grafana Labs 开源的水平可扩展、高可用性、多租户的日志聚合系统。它受到 Prometheus 的启发,使用标签(labels)对日志进行特征标记,然后进行归集统计。Loki 的设计理念是经济高效且易于操作,它不会为日志内容建立全文索引,而是对日志数据进行压缩存储,并且只对日志数据的元数据(如时间戳、labels 等)建立索引 75。
部署 Loki 有几种方式,包括使用 Docker、Kubernetes 以及 Helm Chart 等。以下是一些部署 Loki 的基本步骤:

  1. 使用 Docker 部署 Loki: 使用 Docker 部署 Loki 是一种简便的方式。你可以编写一个 docker-compose.yml 文件来定义 Loki 服务,并通过 docker-compose up 命令启动 Loki 75。
  2. 使用 Kubernetes 部署 Loki: Loki 官方推荐的最佳实践为采用 DaemonSet 部署 Promtail 的方式,Promtail 是 Loki 的代理,负责收集日志并将其发送给 Loki。你可以创建相应的 Kubernetes 配置文件,定义 Loki 和 Promtail 的部署和服务 75。
  3. 使用 Helm Chart 部署 Loki: Helm Chart 是 Kubernetes 的包管理工具,可以用来部署 Loki。首先需要添加 Loki 的 Helm 仓库,然后使用 helm install 命令安装 Loki。安装时可以指定一个 values 文件来覆盖默认配置 77。
  4. 配置文件: 无论是哪种部署方式,都需要配置 Loki 的配置文件(如 loki.yaml),在该文件中设置 Loki 的各种参数,例如 HTTP 监听端口、存储配置、日志的索引和存储策略等 78。
  5. 安装 Promtail: Promtail 通常与 Loki 配合使用,负责收集和发送日志数据到 Loki。Promtail 也需要配置文件(如 promtail.yaml),在该文件中定义日志收集的路径和标签等信息 78。
  6. 在 Grafana 中配置 Loki 数据源: 部署完 Loki 后,你可以在 Grafana 中添加 Loki 作为数据源,这样就可以通过 Grafana 探索和可视化 Loki 中的日志数据 78。
  7. 日志清理: Loki 还提供了日志清理的功能,你可以设置日志的保留策略,例如基于时间或日志大小进行清理 75。
  8. 微服务模式部署: 当日志规模非常大时,Loki 支持微服务模式部署,将 Loki 的组件实例化为不同的进程,每个进程都被调用并指定其目标。这种部署模式提供了更高的灵活性和可扩展性,但设置和维护也更复杂
posted @   kunchengs  阅读(236)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示