Promtail是Loki的客户端代理,它负责在Linux服务器上收集日志。通过在每个Linux服务器上安装和配置Promtail,我们可以将业务系统的日志数据发送到Loki进行集中存储和分析。
图片
1. 前置依赖
- Loki 集群,如果不知如何按照,请参考:10分钟在K8s中部署轻量级日志系统Loki
- Linux 服务器可以访问Loki集群
2. 下载安装 promtail
wget https://github.com/grafana/loki/releases/download/v2.7.3/promtail-linux-amd64.zip
3. 解压并迁移文件至/usr/local/promtail文件夹下
unzip promtail-linux-amd64.zip
mkdir /usr/local/promtail
mv promtail-linux-amd64 /usr/local/promtail
4. 创建并修改配置文件
vi config.yaml
server:
http_listen_port: 9080
grpc_listen_port: 0
#读取位置保存文件
positions:
filename: /opt/promtail/positions.yaml # This location needs to be writeable by Promtail.
## 指定推送Loki地址
clients:
- url: http://172.18.1.47:31300/loki/api/v1/push
scrape_configs:
- job_name: system
pipeline_stages:
#日志换行,匹配开头
- multiline:
firstline: ^\d{1,2}:\d{2}:\d{2}.\d{1,3}
max_lines: 128
max_wait_time: 3s
#静态配置
static_configs:
- targets:
- localhost
#定义静态标签
labels:
job: smartpark-agent # A `job` label is fairly standard in prometheus and useful for linking metrics and logs.
node_name: 172.17.1.55 # A `host` label will help identify logs from this machine vs others
app: smartpark-agent
__path__: /data/logs/*log # The path matching uses a third party library: https://github.com/bmatcuk/doublestar
3.编译自启动脚本
cat > /usr/lib/systemd/system/promtail.service <
4.启动命令
#启动
systemctl start promtail
#查看状态
systemctl status promtail
#设置开机启动
systemctl enable promtail
图片
5. 查看验证
登录Grafana在Explore 中选择标签 app = smartpark-agent, 可以查询到相关日志,如下图
图片