5分鍾搭建輕量級日志系統Loki


Loki 是一個水平可擴展,高可用性,多租戶日志聚合系統,靈感來自 Prometheus ,其設計非常經濟高效,易於操作。它不索引日志的內容,而是為每個日志流設置一組標簽。

與其他日志聚合系統相比,Loki:

  • 不對日志進行全文本索引。通過存儲壓縮的,非結構化的日志以及僅索引元數據,Loki更加易於操作且運行成本更低。
  • 使用與Prometheus相同的標簽對日志流進行索引和分組,從而使您能夠使用與Prometheus相同的標簽在指標和日志之間無縫切換。
  • 特別適合存儲Kubernetes Pod日志。諸如Pod標簽之類的元數據會自動被抓取並建立索引。
  • 在Grafana中原生支持(需要Grafana v6.0及以上)。

基於Loki的日志記錄堆棧包含3個組件:

  • promtail是代理,負責收集日志並將其發送給Loki。
  • loki是主服務器,負責存儲日志和處理查詢。
  • Grafana用於查詢和顯示日志。

開始

大部分文章都是基於 k8s 、docker-compose去安裝的,這里我們用二進制安裝

Loki

類似 elasticsearch

安裝
curl -O -L "https://github.com/grafana/loki/releases/download/v1.5.0/loki-linux-amd64.zip"
unzip loki-linux-amd64.zip
chmod a+x loki-linux-amd64
./loki-linux-amd64
配置文件 config.yaml
auth_enabled: false

server:
  http_listen_port: 3100

ingester:
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 5m
  chunk_retain_period: 30s

schema_config:
  configs:
    - from: 2018-04-15
      store: boltdb
      object_store: filesystem
      schema: v9
      index:
        prefix: index_
        period: 168h

storage_config:
  boltdb:
    directory: /tmp/loki/index

  filesystem:
    directory: /tmp/loki/chunks

limits_config:
  enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h

chunk_store_config:
  max_look_back_period: 0

table_manager:
  chunk_tables_provisioning:
    inactive_read_throughput: 0
    inactive_write_throughput: 0
    provisioned_read_throughput: 0
    provisioned_write_throughput: 0
  index_tables_provisioning:
    inactive_read_throughput: 0
    inactive_write_throughput: 0
    provisioned_read_throughput: 0
    provisioned_write_throughput: 0
  retention_deletes_enabled: false
  retention_period: 0

Promtail

比如你要收集Nginx的錯誤日志,那就要在Nginx那台服務器部署 Promtail,類似 fluentd

安裝
curl -O -l "https://github.com/grafana/loki/releases/download/v1.5.0/promtail-linux-amd64.zip"
unzip promtail-linux-amd64.zip
chmod a+x promtail-linux-amd64
./promtail-linux-amd64
配置文件 config.yaml
# Promtail Server Config
server:
  http_listen_port: 9080
  grpc_listen_port: 0

# Positions
positions:
  filename: /tmp/positions.yaml

# Loki服務器的地址
clients:
  - url: http://172.18.11.161:3100/loki/api/v1/push

scrape_configs:
  - job_name: nginx
    static_configs:
      - targets:
          - localhost
        labels:
          job: nginx-error
          host: localhost
          __path__: /usr/local/nginx/logs/error.log

Grafana

打開Grafana,添加數據源,選

使用

打開 Grafana,點擊 Explore ,

Log labels 輸入 {job="nginx-error"}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM