1. 下載二進制文件
官方下載地址:https://github.com/grafana/loki/releases
下載如圖所示的這倆文件,Grafana采用yum方式安裝
cd /usr/local/src
wget https://github.com/grafana/loki/releases/download/v2.1.0/loki-linux-amd64.zip
wget https://github.com/grafana/loki/releases/download/v2.1.0/promtail-linux-amd64.zip
unzip loki-linux-amd64.zip
unzip promtail-linux-amd64.zip
chmod a+x loki-linux-amd64
chmod a+x promtail-linux-amd64
wget https://dl.grafana.com/oss/release/grafana-7.3.6-1.x86_64.rpm
sudo yum -y install grafana-7.3.6-1.x86_64.rpm
systemctl start grafana-server.service
systemctl stop grafana-server.service
systemctl status grafana-server.service
2. 下載配置文件
關於這個配置文件,應該具體分情況,不同的部署方式采用的配置文件會有一些不同之處
loki官方文檔地址:https://grafana.com/docs/loki/latest/configuration/examples/
不過這里參考官方github上的地址:https://github.com/grafana/loki/tree/master/cmd/loki
loki-local-config.yaml
auth_enabled: false
server:
http_listen_port: 3100 # 端口
ingester:
wal:
enabled: true
dir: /tmp/wal
recover: true
lifecycler:
address: 127.0.0.1 # 地址
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 1h # Any chunk not receiving new logs in this time will be flushed
max_chunk_age: 1h # All chunks will be flushed when they hit this age, default is 1h
chunk_target_size: 1048576 # Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached first
chunk_retain_period: 30s # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)
max_transfer_retries: 0 # Chunk transfers disabled
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h
storage_config:
boltdb_shipper:
active_index_directory: /tmp/loki/boltdb-shipper-active
cache_location: /tmp/loki/boltdb-shipper-cache
cache_ttl: 24h # Can be increased for faster performance over longer query periods, uses more disk space
shared_store: filesystem
filesystem:
directory: /tmp/loki/chunks
compactor:
working_directory: /tmp/loki/boltdb-shipper-compactor
shared_store: filesystem
limits_config:
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
ruler:
storage:
type: local
local:
directory: /tmp/loki/rules
rule_path: /tmp/loki/rules-temp
alertmanager_url: http://localhost:9093 # alertmanager報警地址
ring:
kvstore:
store: inmemory
enable_api: true
promtail官方github上的地址:https://github.com/grafana/loki/blob/master/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 # 推送日志的loki地址
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*log
3. 運行
使用這個loki-local-config.yaml文件時還需要修改一下,去掉里面的這幾行,
wal:
enabled: true
dir: /tmp/wal
recover: true
不然啟動會報錯:
failed parsing config: ./loki-local-config.yaml: yaml: unmarshal errors:
line 7: field wal not found in type ingester.Config
修改loki數據存儲路徑:
# vim loki-local-config.yaml
:%s+/tmp/+/opt/loki/tmp/+
# vim promtail-local-config.yaml
:%s+/tmp/+/opt/loki/tmp/+
./loki-linux-amd64 -config.file=./loki-local-config.yaml
./promtail-linux-amd64 -config.file=./promtail-local-config.yaml
后台啟動,輸出日志到其他文件中:
nohup ./loki-linux-amd64 -config.file=./loki-local-config.yaml > /opt/logs/loki-3100.log 2>&1 &
訪問:# curl http://localhost:3100/metrics
如需debug日志,使用以下啟動命令:
nohup ./loki-linux-amd64 --log.level=debug -config.file=./loki-local-config.yaml > /opt/logs/loki-3100.log 2>&1 &
# nohup ./promtail-linux-amd64 -config.file=promtail-local-config.yaml > /opt/logs/promtail-9080.log 2>&1 &
正常啟動后的信息顯示:
打開瀏覽器訪問grafana界面,配置數據源
查看loki運行日志,promtail給loki推送過來日志的話日志顯示如下:
4. 進一步完善
注:這一步只是寫一下教程,並沒實際操作
參考這篇文章的部分內容,制作成service服務並運行:https://www.cnblogs.com/sanduzxcvbnm/p/13094099.html
vim /usr/lib/systemd/system/loki.service
[Unit]
Description=loki
Documentation=https://github.com/grafana/loki/tree/master
After=network.target
[Service]
Type=simple
User=loki
ExecStart=/usr/local/src/loki-linux-amd64 -config.file=/usr/local/src/loki-local-config.yaml &>> /opt/logs/loki-3100.log # 具體路徑可以根據實際情況修改
Restart=on-failure
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl start loki
# systemctl status loki
# systemctl enable loki
vim /usr/lib/systemd/system/promtail.service
[Unit]
Description=promtail
Documentation=https://github.com/grafana/loki/tree/master
After=network.target
[Service]
Type=simple
User=promtail
ExecStart=/usr/local/src/promtail-linux-amd64 -config.file=/usr/local/src/promtail-local-config.yaml &>> /opt/logs/promtail-9080.log # 具體路徑可以根據實際情況修改
Restart=on-failure
[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl start promtail
# systemctl status promtail
# systemctl enable promtail
驗證:
curl "http://127.0.0.1:3100/api/prom/label"
curl localhost:3100/loki/api/v1/labels