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

2024年 3月 11日 73.6k 0

在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

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论