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

✅ 一、前提条件

  1. 已安装 Loki 服务

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

  3. 具备 sudo 权限

二、下载 Promtail 二进制文件

# 替换为你想要的版本VERSION="3.5.1" # 创建目录sudo mkdir -p /opt/promtailcd /opt/promtail # 下载并解压curl -LO "https://github.com/grafana/loki/releases/download/v${VERSION}/promtail-linux-amd64.zip"unzip promtail-linux-amd64.zipmv promtail-linux-amd64 promtailchmod +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 serviceAfter=network.target [Service]Type=simpleExecStart=/opt/promtail/promtail -config.file=/etc/promtail/promtail.yaml >> /var/log/promtail.log 2>> /var/log/promtail-error.logRestart=on-failure [Install]WantedBy=multi-user.target

五、启动服务

sudo systemctl daemon-reexecsudo systemctl daemon-reloadsudo 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

posted @ 2025-07-20 11:40  yjbjingcha  阅读(13)  评论(0)    收藏  举报