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 
相关推荐
网硕互联的小客服15 分钟前
如何利用Elastic Stack(ELK)进行安全日志分析
linux·服务器·网络·安全
数字芯片实验室26 分钟前
寄存器模型生成:从手工到自动化
运维·自动化
Yungoal31 分钟前
php & apache构建 Web 服务器
服务器·php·apache
冰橙子id1 小时前
linux——磁盘和文件系统管理
linux·运维·服务器
咕噜企业签名分发-淼淼1 小时前
应用app的服务器如何增加高并发
运维·服务器
b***25111 小时前
18650锂电池组点焊机:高效组装锂电池的关键工具|比斯特自动化
运维·自动化
leagsoft_10031 小时前
联软NSPM自动化策略管理 助力上交所加速国产化替代提升运维效率
运维·网络·自动化
Mikhail_G2 小时前
Python应用函数调用(二)
大数据·运维·开发语言·python·数据分析
来鸟 鸣间3 小时前
vscode 连接远程服务器
服务器·ide·vscode
伤不起bb3 小时前
Kafka 消息队列
linux·运维·分布式·kafka