1. 程式人生 > 實用技巧 >5分鐘搭建輕量級日誌系統Loki

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"}