Promtail采集服务器本地日志存储到Loki

✅ 一、前提条件

  1. 已安装 Loki 服务

  2. 日志文件目录可访问(如 /var/log)

  3. 具备 sudo 权限

🧩 二、下载 Promtail 二进制文件

bash 复制代码
# 替换为你想要的版本
VERSION="3.5.1"

# 创建目录
sudo mkdir -p /opt/promtail
cd /opt/promtail

# 下载并解压
curl -LO "https://github.com/grafana/loki/releases/download/v${VERSION}/promtail-linux-amd64.zip"
unzip promtail-linux-amd64.zip
mv promtail-linux-amd64 promtail
chmod +x promtail

📦 三、配置 Promtail

创建配置文件 /etc/promtail/promtail.yaml ,示例内容如下:

bash 复制代码
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /opt/promtail/positions.yaml

clients:
  - url: http://10.55.2.40:31000/loki/api/v1/push

scrape_configs:
  - job_name: yarn_logs
    static_configs:
      - targets:
          - localhost
        labels:
          job: yarn_logs
          __path__: /data/yarn/logs/**/**/*.out
      - targets:
          - localhost
        labels:
          job: yarn_logs
          __path__: /data/yarn/logs/**/**/*.err
      - targets:
          - localhost
        labels:
          job: yarn_logs
          __path__: /data/yarn/logs/**/**/*.log
        

🛠 四、创建 systemd 服务

创建服务文件 /etc/systemd/system/promtail.service:

bash 复制代码
[Unit]
Description=Promtail service
After=network.target

[Service]
Type=simple
ExecStart=/opt/promtail/promtail -config.file=/etc/promtail/promtail.yaml >> /var/log/promtail.log 2>> /var/log/promtail-error.log
Restart=on-failure

[Install]
WantedBy=multi-user.target

🚀 五、启动服务

bash 复制代码
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable --now promtail

检查状态:

bash 复制代码
systemctl status promtail

📋 六、验证日志采集

  1. 访问 Loki,确认数据已经送达。

  2. 使用 Grafana 中的 Explore 功能查看日志(需要已连接 Loki 数据源)。

bash 复制代码
# 移动到日志采集的目录下
[root@uhadoop-1cef6n08gzqc-core1 ~]# cd  /data/yarn/logs/application_1748242101840_0048/container_1748242101840_0048_01_000002 
# 手动写入到sunwenbo.log 日志文件中
[root@uhadoop-1cef6n08gzqc-core1 container_1748242101840_0048_01_000002]# echo "sunwenbo test" >  sunwenbo.log 
相关推荐
jerryinwuhan8 小时前
《系统部署与运维》开篇 课程介绍
运维
我是谁??10 小时前
Ubuntu22.04更换清华源
linux·运维·服务器
圣殿骑士-Khtangc11 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
Shell运维手记11 小时前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
朴马丁11 小时前
国际与国产PLM在精细化工赛道的布局:2026年主要厂商技术特色
大数据·运维·人工智能·流程行业plm·化工新材料
这个DBA有点耶12 小时前
数据库一体机架构演进:从硬件堆叠到软硬深度耦合
服务器·网络·数据库·硬件架构·运维开发·database·数据库架构
Elastic 中国社区官方博客12 小时前
Elasticsearch:使用 AI Agent 来创建 workflow
大数据·运维·人工智能·elasticsearch·搜索引擎·自动化·全文检索
测试运维日常笔记13 小时前
Linux系统安装配置TigerVNC服务器完整指南
linux·运维·服务器
像风一样自由202013 小时前
加强版VScode结合remote-ssh远程连接服务器开发指南
服务器·vscode·ssh
喜欢的名字被抢了13 小时前
程序出问题怎么查,以及如何让它不掉线
java·运维·数据库