Promtail 配置及优化

Promtail:

Promtail与Loki通信是通过HTTP API实现的,Promtail会使用Loki的/loki/api/v1/push接口将日志数据发送到Loki,每条日志数据包含日志内容和相关标签信息,Loki接收到日志后会根据标签进行存储和索引。

Tailing机制:Promtail使用Tailing机制读取新的日志条目,只读取新增的部分,而不是整个文件。这样减少了I/O操作,降低了系统负载。

Promtail将日志条目批量处理和发送,减少了与Loki通信次数,提高网络和CPU的利用率。

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

官方提供的配置信息及解释:

https://grafana.com/docs/loki/latest/send-data/promtail/configuration/

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#服务器块将Promtail的行为配置为HTTP服务器:

server:
  http_listen_port: 9080   #对外API请求的HTTP服务
  grpc_listen_port: 0   #每个组件产生一个用于内部请求服务

positions: #positions块配置Promtail保存文件的位置,指示它在文件中读取了多远。当Promtail重新启动时,它需要允许它从停止的地方继续。
  filename: /var/log/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push  #loki日志地址,即接口地址

##scrap_config块配置Promtail如何使用指定的发现方法从一系列目标中抓取日志。Promtail使用相同的Prometheus scrape_config。这意味着,如果你已经拥有一个Prometheus实例,配置将非常相似:

scrape_configs:
  - job_name: system  #namespace 名字
  static_configs:
    - targets:
      - localhost #node节点名字
  labels:
    job: varlogs  #自定义job名字
    __path__: /var/log/*.log  #实际日志地址

# 批处理和发送配置,客户端块配置Promtail如何连接到Loki实例:
client: 
  backoff_config:
    min_period: 500ms
    max_period: 5s
    max_retries: 10
  batch_size: 1048576 # 1MB  批处理发送文件的大小
  batch_wait: 1s    #发送一批数据的等待时间间隔,即使文件数据未满

 

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下载地址:

https://github.com/grafana/loki/releases/download/v2.8.6/promtail-linux-amd64.zip

启动:nohup ./promtail-linux-amd64 -config.file=/etc/promtail/config.yml &

或者 开机启动

[root@hw ~]# cat /usr/lib/systemd/system/promtail.service
[Unit]
Description=promtail
Documentation=https://github.com/grafana/loki/tree/master
After=network.target
 
[Service]
Type=simple
User=root
ExecStart=/home/tools/promtail/promtail-linux-amd64 -config.file=/home/tools/promtail/config/promtail-local-config.yaml
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

 

posted @ 2024-09-04 10:42  Mike_Jia  阅读(500)  评论(0)    收藏  举报