Loki 支持多种部署方式:二进制、Docker、Docker Compose、Helm(K8s 环境),二进制部署最轻便。
本次部署环境:
CentOS 7.x / 8.x(Ubuntu 类似,命令略有差异)
Loki v3.6.10(最新稳定版,适配高版本GLIBC)
Promtail 采集日志
Grafana 可视化。
注:1. 若系统为CentOS 7(默认GLIBC版本低于2.32),部署v3.6.10会出现GLIBC_2.32/2.34 not found报错,需先升级系统GLIBC版本-建议不升级换低版本的
关闭防火墙(或开放 3100、9090、3000 端口)
mkdir -p /app/soft/{bin,conf,data}
cd /app/soft/bin
下载 Loki 二进制包
Loki 官方(https://github.com/grafana/loki/releases),下载对应系统的二进制包(我的是linux-amd64 版本);
下载 Loki v3.6.10 二进制包
wget https://github.com/grafana/loki/releases/download/v3.6.10/loki-linux-amd64.zip
unzip loki-linux-amd64.zip
chmod +x loki-linux-amd64
mv loki-linux-amd64 loki
创建 Loki 配置文件,采用本地文件存储(适合单节点部署),配置日志保留时间、存储路径等核心参数,所有路径统一改为/app/soft:
进入配置目录,创建配置文件
cd /app/soft/conf
vim loki-config.yaml
配置文件内容(直接复制粘贴,按需修改,所有路径适配/app/soft)
server:
http_listen_port: 3100 # Loki 服务端口
grpc_listen_port: 9096 # gRPC 通信端口
log_level: info # 日志级别(info/debug/error)
common:
instance_addr: 127.0.0.1
path_prefix: /app/soft/data # 存储目录(对应之前创建的data目录)
storage:
filesystem:
chunks_directory: /app/soft/data/chunks # 日志块存储路径
rules_directory: /app/soft/data/rules # 规则存储路径
replication_factor: 1 # 副本数(单节点设为1)
ring:
kvstore:
store: inmemory # 单节点使用内存存储ring信息
schema_config:
configs:
- from: 2025-04-08
store: tsdb # 索引格式(推荐tsdb)
object_store: filesystem # 日志块存储方式(本地文件)
schema: v13
index:
prefix: index_
period: 24h # 索引分片周期(每24小时一个分片)
compactor:
working_directory: /app/soft/data/compactor # 压缩器工作目录
compaction_interval: 10m # 压缩间隔
retention_enabled: true # 启用日志保留策略
retention_delete_delay: 2h
retention_delete_worker_count: 150
delete_request_store: filesystem # 新增配置:解决retention启用后报错,与object_store保持一致
limits_config:
retention_period: 72h # 日志保留时间(3天)
ruler:
alertmanager_url: http://localhost:9093 # 告警地址(暂不配置,可留空)
frontend:
encoding: protobuf
创建 systemd 服务文件,实现 Loki 开机自启,便于管理,ExecStart路径改为/app/soft/bin:
vim /etc/systemd/system/loki.service
[Unit]
Description=Loki Log Aggregation System
After=network.target
[Service]
User=root
Group=root
ExecStart=/app/soft/bin/loki -config.file=/app/soft/conf/loki-config.yaml
Restart=always # 异常自动重启
LimitNOFILE=65536 # 提升文件描述符限制
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start loki
systemctl enable loki
systemctl status loki
curl 服务器IP:3100/ready,返回ready即表示 正常运行;
Promtail 部署在需要采集日志的节点上(本次与 Loki 部署在同一节点,测试用),负责采集节点日志并发送到 Loki,全程适配/app/soft目录。
进入 Loki 二进制目录(已统一为/app/soft/bin)
cd /app/soft/bin
下载 Promtail v3.6.10 二进制包(与 Loki 版本一致)
wget https://github.com/grafana/loki/releases/download/v3.6.10/promtail-linux-amd64.zip
#解压
unzip promtail-linux-amd64.zip
# 赋予执行权限并改名
chmod +x promtail-linux-amd64
mv promtail-linux-amd64 promtail
创建 Promtail 配置文件,设置日志采集路径、标签、Loki 服务端地址
cd /app/soft/conf
vim promtail-config.yaml
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /app/soft/data/positions.yaml # 记录日志采集位置(避免重复采集)
clients:
- url: http://127.0.0.1:3100/loki/api/v1/push # Loki 服务端地址(本地部署,直接用127.0.0.1)
scrape_configs:
# 采集系统日志(/var/log 目录下的日志)
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: system_log # 日志标签(自定义)
env: test# 环境标签(自定义)
node: localhost # 节点标签(自定义)
file_sd_configs:
- files:
- /app/soft/conf/system-logs.yaml # 系统日志路径配置(适配/app/soft)
# 采集 Loki 自身日志
- job_name: loki
static_configs:
- targets:
- localhost
labels:
job: loki_log
env: test
node: localhost
file_sd_configs:
- files:
- /app/soft/conf/loki-logs.yaml
创建两个日志路径配置文件,分别指定系统日志和 Loki 日志的采集路径,配置文件存储在/app/soft/conf:
vim /app/soft/conf/system-logs.yaml
#创建系统日志路径配置(存储在/app/soft/conf)
vim /app/soft/conf/system-logs.yaml
# 内容如下(采集 /var/log 下的核心系统日志)
- targets:
- localhost
labels:
__path__: /var/log/*.log# 采集所有 .log 结尾的日志
__path__: /var/log/messages # 系统消息日志
__path__: /var/log/secure # 安全日志
# 创建 Loki 日志路径配置(存储在/app/soft/conf)
vim /app/soft/conf/loki-logs.yaml
# 内容如下(采集 Loki 自身日志)
- targets:
- localhost
labels:
__path__: /var/log/loki.log # Loki 日志路径(后续配置)
创建 systemd 服务文件(全程适配你的部署目录/app/soft)
vim /etc/systemd/system/promtail.service
服务文件内容(固定使用/app/soft路径)
[Unit]
Description=Promtail Log Collector
After=network.target loki.service # 依赖 Loki 服务,Loki 启动后再启动 Promtail
[Service]
User=root
Group=root
ExecStart=/app/soft/bin/promtail -config.file=/app/soft/conf/promtail-config.yaml
Restart=always
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
重新加载 systemd 配置,启动 Promtail
systemctl daemon-reload
systemctl start promtail
systemctl enable promtail
systemctl status promtail
启动成功后,Promtail 会开始采集指定路径的日志,并发送到 Loki 服务端。
采用部署p8s那会的
加入loki

