本文介绍如何在 Linux 系统上下载、安装并配置 Promtail 客户端,以便收集日志并发送给 Loki。
1. 下载与安装
从 GitHub Releases 页面下载适合的 Promtail 版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| wget https://github.com/grafana/loki/releases/download/v2.7.3/promtail-linux-amd64.zip
mkdir -p /usr/local/promtail mv promtail-linux-amd64.zip /usr/local/promtail/
cd /usr/local/promtail/ unzip promtail-linux-amd64.zip
chmod +x promtail-linux-amd64
mv promtail-linux-amd64 promtail
|
2. 配置文件
创建并编辑配置文件 /usr/local/promtail/promtail-local-config.yaml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server: http_listen_port: 9080 grpc_listen_port: 0 grpc_server_max_recv_msg_size: 15728640 grpc_server_max_send_msg_size: 15728640
positions: filename: /usr/local/promtail/positions.yaml
clients: - url: http://192.168.66.178:3100/loki/api/v1/push
scrape_configs: - job_name: app_log static_configs: - targets: - localhost labels: job: varlogs_178 __path__: /var/log/*.log
|
3. 设置 Systemd 服务
创建服务文件 /usr/lib/systemd/system/promtail.service:
1 2 3 4 5 6 7 8 9 10 11 12 13
| [Unit] Description=Promtail service Documentation=https://grafana.com/docs/loki/latest/clients/promtail/ After=network.target
[Service] Type=simple User=root ExecStart=/usr/local/promtail/promtail -config.file=/usr/local/promtail/promtail-local-config.yaml Restart=on-failure
[Install] WantedBy=multi-user.target
|
4. 启动服务
1 2 3 4 5 6 7 8 9 10 11
| systemctl daemon-reload
systemctl start promtail
systemctl status promtail
systemctl enable promtail
|