搭建Loki、Promtail、Grafana輕量級日志系統(centos7)--簡稱PLG
需求
公司項目采用微服務的架構,服務很多,每個服務都有自己的日志,分別存放在不同的服務器上。當查找日志時需要分別登錄不同的服務器,有大量的請求的情況下,在日志文件中查找信息十分困難。想要搭建一個日志系統,ELK分布式日志系統對於中小型公司來說開發維護成本太高,經過調研,選擇Loki輕量級日志系統。
Loki簡介
Loki 是一個水平可擴展,高可用性,多租戶日志聚合系統,靈感來自 Prometheus ,其設計非常經濟高效,易於操作。它不索引日志的內容,而是為每個日志流設置一組標簽。
Loki與其他日志聚合系統差別:
不對日志進行全文本索引。通過存儲壓縮的,非結構化的日志以及僅索引元數據,Loki更加易於操作且運行成本更低。
使用與Prometheus相同的標簽對日志流進行索引和分組,從而使您能夠使用與Prometheus相同的標簽在指標和日志之間無縫切換。
特別適合存儲Kubernetes Pod日志。諸如Pod標簽之類的元數據會自動被抓取並建立索引。
在Grafana中原生支持(需要Grafana v6.0及以上)。
Loki的日志系統的組件:
Promtail是代理,負責收集日志並將其發送給Loki。
Loki是主服務器,負責存儲日志和處理查詢。
Grafana用於查詢和顯示日志。
搭建步驟
本文采用的搭建方式是分別下載各個組件並安裝。也可以參考官方的文檔進行搭建安裝。
Loki的GitHub地址:https://github.com/grafana/loki
配置文件官網地址:https://grafana.com/docs/loki/latest/installation/local/
Grafana下載官網:https://grafana.com/grafana/download
1.下載安裝啟動Grafana
官網提供了下圖中幾種方式,本文采用的是CentOS系統,yum安裝的方式。
#下載安裝grafana命令,下列命令執行成功后。在/usr/sbin文件夾下會有grafana-server執行文件
wget https://dl.grafana.com/oss/release/grafana-7.3.2-1.x86_64.rpm
sudo yum localinstall grafana-7.3.2-1.x86_64.rpm
#啟動grafana,grafana會占用服務器3000端口,記得保證3000端口不被占用
cd /usr/sbin
./grafana-server web
2.下載啟動Loki和Promtail
官方文檔地址:https://grafana.com/docs/loki/latest/installation/local/
因為采用本地安裝的方式,參考文檔(下圖箭頭指向的位置),分別下載執行文件和啟動的配置文件。
下載Promtail:https://github.com/grafana/loki/releases/download/v2.0.0/promtail-linux-amd64.zip
#下載壓縮文件
curl -O -L "https://github.com/grafana/loki/releases/download/v2.0.0/loki-linux-amd64.zip"
#解壓文件
unzip "loki-linux-amd64.zip"
#執行文件授權
chmod a+x "loki-linux-amd64"
#下載Loki和Promtail的配置文件
wget https://raw.githubusercontent.com/grafana/loki/master/cmd/loki/loki-local-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/main/clients/cmd/promtail/promtail-local-config.yaml
若下載失敗,使用下面已下載好的文件
loki-local-config.yaml
vim 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: 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-local-config.yaml
vim 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
啟動Loki,本文采用的Loki默認配置,服務端口為3100
#啟動Loki命令
后台啟動
nohup ./loki-linux-amd64 -config.file=loki-local-config.yaml > loki.log 2>&1 &
前台啟動
./loki-linux-amd64 -config.file=./loki-local-config.yaml
#查看啟動是否成功(查看3100端口的進程是否存在)
netstat -tunlp | grep 3100
#或者根據名稱查找進程(執行命令后有下邊的顯示,則啟動成功)
ps -ef | grep loki-linux-amd64
$ root 11037 22022 0 15:44 pts/0 00:00:55 ./loki-linux-amd64 -config.file=loki-local-config.yaml
啟動Promtail
前台啟動
./promtail-linux-amd64 -config.file=./promtail-local-config.yaml
后台啟動
#Promtail默認端口是9080,啟動完成后,可以采用上邊的方式查看進程是否啟動成功
nohup ./promtail-linux-amd64 -config.file=promtail-local-config.yaml > promtail.log 2>&1 &
配置服務
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
如何配置數據源
訪問web頁面:http://localhost:3000/ 進行登錄(賬號密碼都是admin),點擊下圖中的位置,找到Loki,配置數據源。
填寫數據源的訪問地址並保存。
日志查看效果如下圖。
配置日志路徑
varlog參考寫法
參考:https://www.cnblogs.com/sanduzxcvbnm/p/14234953.html
參考:https://blog.csdn.net/tcy1429920627/article/details/109679363