[关闭]
@cdmonkey 2022-11-22T02:22:55.000000Z 字数 5188 阅读 344

Promtail

Loki


安装

创建用户:

  1. useradd promtail -s /sbin/nologin -M
  1. [root@myloki tools]# mkdir -pv /opt/promtail
  2. [root@myloki tools]# unzip promtail-linux-amd64.zip
  3. [root@myloki tools]# mv promtail-linux-amd64 /opt/promtail/
  1. [root@myloki ~]# vim /etc/systemd/system/promtail.service
  2. [Unit]
  3. Description=Promtail service
  4. After=network.target
  5. [Service]
  6. Type=simple
  7. User=promtail
  8. ExecStart=/opt/promtail/promtail-linux-amd64 -config.file /opt/promtail/promtail.yml
  9. Restart=on-failure
  10. LimitNOFILE=100000
  11. LimitNPROC=100000
  12. [Install]
  13. WantedBy=multi-user.target

配置

配置文件下载:

https://github.com/grafana/loki/tree/main/clients/cmd/promtail

  1. # 配置 Promtail 服务端
  2. [server: <server_config>]
  3. # 描述 Promtail 怎样连接至 Loki 的多个实例,向每个实例发送日志。
  4. # WARNING:若是其中一个远程 Loki 服务器未能回应或回应时出有可重试的错误,这将影响其他配置的远程 Loki 服务器发送日志。
  5. # 发送是于单线程上完成的!
  6. # 若是你想向多个远程 Loki 实例发送,一般建议并行运行多个 Promtail 客户端。
  7. clients:
  8. - [<client_config>]
  9. # 描述了怎样将读入的文件偏移量保存至硬盘上
  10. [positions: <position_config>]
  11. scrape_configs:
  12. - [<scrape_config>]
  13. # 配置被 watch 的目标如何 tailed
  14. [target_config: <target_config>]

server

配置 Promtail 作为一个 http 服务器时的行为。

  1. # 禁用 HTTP 及 GRPC 服务
  2. [disable: <boolean> | default = false]
  3. [http_listen_address: <string>] # HTTP 服务监听的主机/地址
  4. [grpc_listen_address: <string>] # gRPC 服务监听的主机/地址
  5. [http_listen_port: <int> | default = 80] # HTTP 服务监听端口,0 表示随机
  6. [grpc_listen_port: <int> | default = 9095] # gRPC 服务监听端口,0 表示随机
  7. # 注册指标处理器 (/metrics, etc.)
  8. [register_instrumentation: <boolean> | default = true]
  9. [graceful_shutdown_timeout: <duration> | default = 30s] # 优雅退出超时时间
  10. [http_server_read_timeout: <duration> | default = 30s] # HTTP 服务读出超时时间
  11. [http_server_write_timeout: <duration> | default = 30s] # HTTP 服务写入超时时间
  12. [http_server_idle_timeout: <duration> | default = 120s] # HTTP 服务空闲超时时间
  13. [grpc_server_max_recv_msg_size: <int> | default = 4194304] # 可接收之最大 gRPC 消息大小
  14. [grpc_server_max_send_msg_size: <int> | default = 4194304] # 可发送之最大 gRPC 消息大小
  15. # 对 gRPC 调用的并发流数量之限制 (0 = unlimited)
  16. [grpc_server_max_concurrent_streams: <int> | default = 100]
  17. # 只记录给定严重程度或以上之信息,可选值: debug, info, warn, error
  18. [log_level: <string> | default = "info"]
  19. # 所有 API 路由服务之基本路径 (e.g., /v1/)
  20. [http_path_prefix: <string>]
  21. # 目标管理器检测 Promtail 可读的标志,若是设置为 false 检查将被忽略
  22. [health_check_target: <bool> | default = true]

clients

配置 Promtail 怎样连接至 Loki 的实例。

  1. # 其实就是 Loki 监听之 URL,Loki 中表示为 http_listen_address、http_listen_port
  2. # 若是 Loki 于微服务模型下运行,这就是 Distributor 的 URL,需要包括 push API 路径
  3. # 就像: http://example.com:3100/loki/api/v1/push
  4. url: <string>
  5. # 缺省租户 ID,用于推送日志至 Loki
  6. # 若是省略或是为空,则假设 Loki 于单租户模型下运行,不发送 X-Scope-OrgID 头信息
  7. [tenant_id: <string>]
  8. # 发送一批日志前的最大等待时间,即使该批次日志数据未满
  9. [batchwait: <duration> | default = 1s]
  10. # 向 Loki 发送批处理之前要积累之最大批处理量(单位为字节)
  11. [batchsize: <int> | default = 1048576]
  12. # 若使用了 basic auth 认证,则需要配置用户名及密码
  13. basic_auth:
  14. [username: <string>]
  15. [password: <string>]
  16. [password_file: <filename>] # 包含 basic auth 认证之密码文件
  17. # 可选的 OAuth 2.0 配置
  18. # 不能同 basic_auth、authorization 同时使用
  19. oauth2:
  20. [client_id: <string>]
  21. [client_secret: <secret>]
  22. [client_secret_file: <filename>]
  23. scopes: # 令牌请求的可选范围
  24. [ - <string> ... ]
  25. token_url: <string>
  26. endpoint_params: # 附加至令牌 URL 的可选参数
  27. [ <string>: <string> ... ]
  28. [bearer_token: <secret>] # 发送给服务器的 Bearer token
  29. [bearer_token_file: <filename>] # 包含 Bearer token 的文件
  30. # 用来连接服务器的 HTTP 代理服务器
  31. [proxy_url: <string>]
  32. # 配置 TLS
  33. tls_config:
  34. [ca_file: <string>]
  35. [cert_file: <filename>]
  36. [key_file: <filename>]
  37. [server_name: <string>]
  38. [insecure_skip_verify: <boolean> | default = false]
  39. # 配置于请求失败时要怎样重试对 Loki 之请求
  40. # 默认回退周期为: 0.5s, 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s(4.267m)
  41. # 于日志丢失之前的总时间为 511.5s(8.5m)
  42. backoff_config:
  43. [min_period: <duration> | default = 500ms] # 重试之间之初始回退时间
  44. [max_period: <duration> | default = 5m] # 重试之间之最大回退时间
  45. [max_retries: <int> | default = 10] # 重试之最大次数
  46. # 配置所有发送至 Loki 的日志中的静态标签
  47. # 使用一个类似于 {"foo": "bar"} 的映射来配置一个 foo 标签,值为 bar
  48. # 这些也能从命令行中指定: -client.external-labels=k1=v1,k2=v2 (或是 --client.external-labels 依赖操作系统)
  49. # 由命令行提供的标签将应用于所有在 "clients" 部分的配置
  50. # 注意:若是标签键相同,配置文件中的定义将替代命令行中相关配置
  51. external_labels:
  52. [ <labelname>: <labelvalue> ... ]
  53. # 等待服务器响应一个请求之最长时间
  54. [timeout: <duration> | default = 10s]
  55. # 要包含于流滞后指标 promtail_stream_lag_seconds 中的用逗号分隔之标签列表
  56. # 默认值为 filename。始终包含 host 标签
  57. # 流滞后指标指示哪些流落后于写入 Loki
  58. # 注意使用太多标签,因为它会增加基数
  59. [stream_lag_labels: <string> | default = "filename"]

positions

配置了 Promtail 保存文件之位置,表示它已经读至文件什么程度。当 Promtail 重新启动时需要它,以允许它从中断处继续读入日志。

  1. # Positions 文件之路径
  2. [filename: <string> | default = "/var/log/positions.yaml"]
  3. # 更新 positions 文件之周期
  4. [sync_period: <duration> | default = 10s]
  5. # 是否忽略并覆盖被破坏的 positions 文件
  6. [ignore_invalid_yaml: <boolean> | default = false]

scrape_configs

配置了 Promtail 将怎样使用指定的发现方法从一系列目标中抓获日志。

  1. # 用于 Promtail UI 中识别该抓获配置的名称
  2. job_name: <string>
  3. # 配置如何对目标日志进行结构化
  4. [pipeline_stages: <pipeline_stages>]
  5. [journal: <journal_config>] # 配置如何从 jounal 中抓获日志
  6. [syslog: <syslog_config>] # 配置如何从 syslog 中抓获日志
  7. # 配置如何通过 Loki Push API 接收日志(例如从其他 Promtail 或是 Docker Logging Driver 中获得的数据)
  8. [loki_push_api: <loki_push_api_config>]
  9. # 配置如何从 Windows 事件日志中抓获日志
  10. [windows_events: <windows_events_config>]
  11. # 配置如何通过消费者组从 Kafka 获得日志
  12. [kafka: <kafka_config>]
  13. # 配置如何 relabel 目标,以确定是否应该对其进行处置
  14. relabel_configs:
  15. - [<relabel_config>]
  16. # 抓获日志静态目标配置
  17. static_configs:
  18. - [<static_config>]
  19. # 包含要抓获的目标文件
  20. file_sd_configs:
  21. - [<file_sd_configs>]
  22. # 配置了如何发现于同一主机上运行的 Kubernetes 服务
  23. kubernetes_sd_configs:
  24. - [<kubernetes_sd_config>]
  25. # 配置如何使用 Consul Catalog API 发现注册至 Consul 集群的服务
  26. consul_sd_configs:
  27. [ - <consul_sd_config> ... ]
  28. # 配置如何使用 Consul Agent API 发现同 Promtail 运行于同一主机上的 consul 代理注册的服务
  29. consulagent_sd_configs:
  30. [ - <consulagent_sd_config> ... ]

target_config

用于控制从目标读入文件之行为。就是怎样从目标处读入文件内容。

  1. # Period to resync directories being watched and files being tailed to discover
  2. # new ones or stop watching removed ones.
  3. sync_period: "10s"

参考内容:

https://jishuin.proginn.com/p/763bfbd5863b
https://cloud.tencent.com/developer/article/1824988
https://grafana.com/docs/loki/latest/clients/promtail/installation
https://grafana.com/docs/loki/latest/clients/promtail/configuration

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注