本文使用的 Loki 和 Promtail 版本為 1.6.1,Grafana 版本為 7.2.0;部署在 Linux 服務器
Loki 負責日志的存儲和查詢;Promtail 負責日志的采集並推送給 Loki;Grafana 負責日志展示
一、Loki
Loki 下載地址:https://github.com/grafana/loki/releases,下載 loki-linux-amd64.zip
在 Loki 的源碼里找到對應版本的配置文件:/cmd/loki/loki-local-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
max_transfer_retries: 0
schema_config:
configs:
- from: 2018-04-15
store: boltdb
object_store: filesystem
schema: v11
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: 0s
table_manager:
retention_deletes_enabled: false
retention_period: 0s
根據具體情況決定是否修改上述配置,然后執行以下命令:
unzip loki-linux-amd64.zip
nohup ./loki-linux-amd64 -config.file=loki-local-config.yaml >/dev/null 2>&1 &
二、Promtail
Promtail 下載地址:https://github.com/grafana/loki/releases,下載 promtail-linux-amd64.zip
在 Loki 的源碼里找到對應版本的配置文件:/cmd/promtail/promtail-local-config.yaml,文件內容為:
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://localhost:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*log
根據具體情況決定是否修改上述配置或者添加 job,然后執行以下命令:
unzip promtail-linux-amd64.zip
nohup ./promtail-linux-amd64 -config.file=promtail-local-config.yaml >/dev/null 2>&1 &
三、Grafana
3.1. 下載並啟動
如果之前沒有安裝過 Grafana,需要先下載安裝,下載地址:https://grafana.com/grafana/download
執行以下命令:
tar -xzvf grafana-7.2.0.linux-amd64.tar.gz
cd grafana-7.2.0
nohup ./bin/grafana-server >/dev/null 2>&1 &
如果不是在 grafana 所在目錄執行運行命令需要添加參數,如:-homepath /xx/grafana-7.2.0
其中 xx 為自定義目錄
默認端口為 3000,用戶名密碼均為 admin
3.2. 配置
- 登錄 Grafana,在 Configuration > Data Sources 點擊 “Add data source” 按鈕,選中 Loki
- URL 填入 http://localhost:3100,並保存
- 在 Explore 中選中上面添加的 Loki 數據源 既可以看到日志信息
