使用Loki promtail 收集Linux原生部署应用日志

系统 Linux
Promtail是Loki的客户端代理,它负责在Linux服务器上收集日志。通过在每个Linux服务器上安装和配置Promtail,我们可以将业务系统的日志数据发送到Loki进行集中存储和分析。

在Kubernetes集群中部署Loki后,默认只能收集Kubernetes集群中的系统日志。然而,对于在Linux服务器上原生部署的业务系统,无法直接进行日志收集。这就导致了在日常业务交互中需要查询这些系统的日志时存在困难。为了解决这个问题,我们计划在Linux服务器上安装Promtail来收集日志,并将其推送到Loki,以实现统一的日志分析和检索。

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 <<EOF
[Unit]
Descriptinotallow=promtail
Documentatinotallow=https://github.com/grafana/loki/tree/master
After=network.target
 
[Service]
Type=simple
User=root
ExecStart=/usr/local/promtail/promtail-linux-amd64 -config.file=/usr/local/promtail/config.yaml
Restart=on-failure
 
[Install]
WantedBy=multi-user.target
EOF

4.启动命令

#启动
systemctl start promtail
#查看状态
systemctl status promtail
#设置开机启动
systemctl enable promtail

图片图片

5. 查看验证

登录Grafana在Explore 中选择标签 app = smartpark-agent, 可以查询到相关日志,如下图

图片图片

责任编辑:武晓燕 来源: 架构成长指南
相关推荐

2022-12-29 08:00:26

Loki网络设备

2022-06-28 08:40:16

LokiPromtail日志报警

2021-05-18 07:30:36

开发Spring Boot日志

2023-08-31 07:46:54

KubernetesLoki

2024-02-04 00:00:00

Loki性能查询

2023-12-25 11:18:12

OpenTeleme应用日志Loki

2022-06-27 07:33:19

微服务Loki

2021-07-20 08:32:16

Kubernetes日志平台

2021-07-19 09:18:07

KubernetesELK Stackk8s

2022-05-11 10:58:11

MetricKitiOS13系统崩溃诊断

2022-06-12 21:28:26

Fluentd开源

2018-03-09 09:15:16

Linuxsyslog-ng日志

2021-06-02 06:02:50

Loki 源码分析日志

2015-10-19 09:47:39

history企业日志Linux

2015-10-27 15:54:29

message日志分析Linux

2022-01-10 09:35:50

日志语言解析器

2023-01-04 08:21:02

Loki配置日志

2021-09-13 08:20:13

Loki日志系统

2022-04-14 08:00:00

Cypress测试开发

2024-02-01 09:48:17

点赞
收藏

51CTO技术栈公众号