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

发布于:2025-06-05 ⋅ 阅读:(24) ⋅ 点赞:(0)

✅ 一、前提条件

  1. 已安装 Loki 服务

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

  3. 具备 sudo 权限

🧩 二、下载 Promtail 二进制文件

# 替换为你想要的版本
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 ,示例内容如下:

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:

[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

🚀 五、启动服务

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable --now promtail

检查状态:

systemctl status promtail

📋 六、验证日志采集

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

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

# 移动到日志采集的目录下
[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 


网站公告

今日签到

点亮在社区的每一天
去签到