一、簡 介
Loki是受Prometheus啟發由Grafana Labs團隊開源的水平可擴展,高度可用的多租戶日志聚合系統。 開發語言: Google Go。它的設計具有很高的成本效益,並且易於操作。使用標簽來作為索引,而不是對全文進行檢索,也就是說,你通過這些標簽既可以查詢日志的內容也可以查詢到監控的數據簽,極大地降低了日志索引的存儲。系統架構十分簡單,由以下3個部分組成 :
- Loki 是主服務器,負責存儲日志和處理查詢 。
- promtail 是代理,負責收集日志並將其發送給 loki 。
- Grafana 用於 UI 展示。
只要在應用程序服務器上安裝promtail來收集日志然后發送給Loki存儲,就可以在Grafana UI界面通過添加Loki為數據源進行日志查詢(如果Loki服務器性能不夠,可以部署多個Loki進行存儲及查詢)。作為一個日志系統不關只有查詢分析日志的能力,還能對日志進行監控和報警
二、系 統 架 構
-
promtail收集並將日志發送給loki的 Distributor 組件
-
Distributor會對接收到的日志流進行正確性校驗,並將驗證后的日志分批並行發送到Ingester
-
Ingester 接受日志流並構建數據塊,壓縮后存放到所連接的存儲后端
-
Querier 收到HTTP查詢請求,並將請求發送至Ingester 用以獲取內存數據 ,Ingester 收到請求后返回符合條件的數據 ;
如果 Ingester 沒有返回數據,Querier 會從后端存儲加載數據並遍歷去重執行查詢 ,通過HTTP返回查詢結果
三、與 ELK 比 較
- ELK雖然功能豐富,但規模復雜,資源占用高,操作苦難,很多功能往往用不上,有點殺雞用牛刀的感覺。
- 不對日志進行全文索引。通過存儲壓縮非結構化日志和僅索引元數據,Loki 操作起來會更簡單,更省成本。
- 通過使用與 Prometheus 相同的標簽記錄流對日志進行索引和分組,這使得日志的擴展和操作效率更高。
- 安裝部署簡單快速,且受 Grafana 原生支持。
四、安 裝 示 例
1、示例安裝環境:
服務器 | 系統 | IP |
loki主機 | Centos 7.6 | 10.0.0.171 |
promtail主機 | Centos 7.6 | 10.0.0.175 |
2、下載安裝軟件
curl -O -L "https://github.com/grafana/loki/releases/download/v1.5.0/loki-linux-amd64.zip" curl -O -L "https://github.com/grafana/loki/releases/download/v1.5.0/promtail-linux-amd64.zip" wget https://dl.grafana.com/oss/release/grafana-6.7.4-1.x86_64.rpm
3、自定義配置文件(loki.yaml和promtail.yaml),由自己創建:
loki.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.yaml:
# Promtail Server Config server: http_listen_port: 9080 grpc_listen_port: 0 # Positions positions: filename: /tmp/positions.yaml # Loki服務器的地址 clients: - url: http://127.0.0.1:3100/loki/api/v1/push scrape_configs: - job_name: linux static_configs: - targets: - localhost labels: job: messages host: localhost __path__: /var/log/messages*
4、安裝Grafana
#安裝依賴 yum install initscripts fontconfig yum install freetype yum install urw-fonts #安裝 rpm -ivh grafana-6.7.4-1.x86_64.rpm
5、啟動loki
systemctl start grafana-server.service
nohup ./loki-linux-amd64 -config.file=/etc/loki/loki.yaml &
6、啟動promtail
nohup ./promtail-linux-amd64 -config.file=/etc/loki/config.yaml &
7、重啟Grafana
systemctl restart grafana-server.service
8、從瀏覽器打開grafana:http:IP:3000
點擊”Greate a data source“
添加HTTP的參數,然后”Save & Test“.然后進入"Explore:
五、在被監控機(10.0.0.175)安裝promtail日志采集系統,並采集 Tomcat運行日志和Web訪問日志
修改配置文件config.yaml
# Promtail Server Config server: http_listen_port: 9080 grpc_listen_port: 0 # Positions positions: filename: /tmp/positions.yaml # Loki服務器的地址 clients: - url: http://10.0.0.141:3100/loki/api/v1/push scrape_configs: - job_name: WEB static_configs: - targets: - localhost labels: job: tomcat host: localhost __path__: /application/tomcat/logs/*.log,/application/tomcat/logs/catalina.out
啟動promtail
nohup ./promtail-linux-amd64 -config.file=/etc/loki/config.yaml &
接着在loki主機重啟grafana
systemctl start grafana-server.service
至此,LOKI日志聚合系統安裝和配置完成。
注:如有需要LOKI的軟件和配置文件的請留言。