大連 做網(wǎng)站廣西壯族自治區(qū)
一、簡介
日志監(jiān)控告警系統(tǒng),較為主流的是ELK(Elasticsearch 、 Logstash和Kibana核心套件構(gòu)成)
,雖然優(yōu)點是功能豐富,允許復(fù)雜的操作。但是,這些方案往往規(guī)模復(fù)雜,資源占用高,操作苦難。很多功能往往用不上,大多數(shù)查詢只關(guān)注一定時間范圍和一些簡單的參數(shù)(如host、service等),使用這些解決方案就有點殺雞用牛刀的感覺了。
所以尋找更輕量級的日志監(jiān)控系統(tǒng),所以也就有了EFK(elasticsearch+kibana+filebeat),
搭建參考:EFK輕量級日志系統(tǒng)搭建
另外一種是Promtail + Loki + Grafana
核心組件構(gòu)成,簡稱PLG
,這種是更輕量級的,配置要求也不高,功能簡單,但是目的明確,就是日志采集。
Loki的架構(gòu)也非常簡單,使用了和prometheus一樣的標(biāo)簽來作為索引,Loki 是受Prometheus啟發(fā)的水平可擴展、高可用、多租戶日志聚合系統(tǒng)。
組件:
promtail: 日志收集的代理,安裝部署在需要收集和分析日志的服務(wù)器,promtail會將日志發(fā)給Loki服務(wù)。
Loki: 主服務(wù)器,負(fù)責(zé)存儲日志和處理查詢。
Grafana:提供web管理界面,數(shù)據(jù)展示功能。
二、服務(wù)器安裝及配置
1.基礎(chǔ)安裝啟動
安裝方式大概有這么幾種,二進制安裝,yum安裝、docker安裝、helm安裝。
因為是服務(wù)器端,我這里用docker安裝,簡單方便。
官方網(wǎng)站也提供了docker-compose文件供下載。
參考地址
#下載docker-compose文件
wget https://raw.githubusercontent.com/grafana/loki/v2.5.0/production/docker-compose.yaml -O docker-compose.yaml
下載的docker-compose文件
如下,也是最簡單基本的。
version: "3"networks:loki:services:loki:image: grafana/loki:2.5.0ports:- "3100:3100"command: -config.file=/etc/loki/local-config.yamlnetworks:- lokipromtail:image: grafana/promtail:2.5.0volumes:- /var/log:/var/logcommand: -config.file=/etc/promtail/config.ymlnetworks:- lokigrafana:image: grafana/grafana:latestports:- "3000:3000"networks:- loki
這時也可以正常啟動即可,默認(rèn)日志為/var/log
的位置。
docker-compose -f docker-compose.yaml up
這時,基本的安裝就完成了,但是有時候需要更改配置文件,如后續(xù)nginx反向代理訪問,這時這個配置文件滿足不了需求,所以需要自己改動grafana。還有就是docker-compose down掉,數(shù)據(jù)也是沒了,需要重新添加。
2、升級配置啟動
更改也很簡單,無非就是啟動后,把docker的配置文件夾拷貝出來本地,然后掛載上去即可。
以下就是我自己改的docker-compose文件
,可以參考:
本人機器3000端口被占用,所以改用3030端口映射,按需設(shè)置。還有本人數(shù)據(jù)文件夾/var/lib/grafana/
掛載后,賬號進不去,所以先注釋掉了,后續(xù)在慢慢測試。
version: "3"networks:loki:services:loki:image: grafana/loki:2.5.0restart: unless-stoppedports:- "3100:3100"volumes:- ./config/loki/:/etc/loki/command: -config.file=/etc/loki/local-config.yamlnetworks:- lokipromtail:image: grafana/promtail:2.5.0volumes:- /var/log:/var/log- ./config/promtail/:/etc/promtail/command: -config.file=/etc/promtail/config.ymlnetworks:- lokigrafana:image: grafana/grafana-enterprise:8.5.2-ubunturestart: unless-stoppedports:- "3030:3000"volumes:- ./config/grafana/:/etc/grafana/
# - ./grafana-data:/var/lib/grafana/
#這行是數(shù)據(jù)掛載在本地,需要將數(shù)據(jù)拷貝出來networks:- loki
最后文件結(jié)構(gòu)如下:除了grafana配置文件夾比較多文件,其他都只有一個。grafana我整個文件夾復(fù)制出來就行了。
├── config
│ ├── grafana
│ │ ├── grafana.ini
│ │ ├── ldap.toml
│ │ └── provisioning
│ │ ├── access-control
│ │ ├── dashboards
│ │ ├── datasources
│ │ ├── notifiers
│ │ └── plugins
│ ├── loki
│ │ └── local-config.yaml
│ └── promtail
│ └── config.yml
├── docker-compose.yaml
└── grafana-data├── alerting [error opening dir]├── csv [error opening dir]├── grafana.db├── plugins└── png [error opening dir]
loki的local-config.yaml
參考配置文件:
auth_enabled: falseserver:http_listen_port: 3100common:path_prefix: /lokistorage:filesystem:chunks_directory: /loki/chunksrules_directory: /loki/rulesreplication_factor: 1ring:kvstore:store: inmemoryschema_config:configs:- from: 2020-10-24store: boltdb-shipperobject_store: filesystemschema: v11index:prefix: index_period: 24hruler:alertmanager_url: http://localhost:9093# By default, Loki will send anonymous, but uniquely-identifiable usage and configuration
# analytics to Grafana Labs. These statistics are sent to https://stats.grafana.org/
#
# Statistics help us better understand how Loki is used, and they show us performance
# levels for most users. This helps us prioritize features and documentation.
# For more information on what's sent, look at
# https://github.com/grafana/loki/blob/main/pkg/usagestats/stats.go
# Refer to the buildReport method to see what goes into a report.
#
# If you would like to disable reporting, uncomment the following lines:
#analytics:
# reporting_enabled: false
promtail的config.yml
配置文件:
server:http_listen_port: 9080grpc_listen_port: 0positions:filename: /tmp/positions.yamlclients:- url: http://loki:3100/loki/api/v1/pushscrape_configs:
- job_name: systemstatic_configs:- targets:- localhostlabels:job: varlogs__path__: /var/log/*log
grafana的配置文件太長,就不復(fù)制了,自己拷貝出來即可。
拷貝完配置文件夾后,更改完后,直接啟動即可。
sudo docker-compose up -d
瀏覽器輸入ip端口即可訪問,我這里配置為ip:3030端口訪問。
3.grafana的反向代理nginx配置,域名訪問
grafana配置完后,默認(rèn)是IP:端口的方式訪問,如果需要域名訪問,則需要用到nginx方向代理。
官方文檔配置參考:官網(wǎng)地址
需要更改的配置文件為grafana.ini
,去掉下面三個前面的注釋。
- domain:你的域名,我這里為grafana.ywbj.cc
- root_url:訪問連接,官方文檔是后面加了grafana/,默認(rèn)是沒有的,也可以不加,區(qū)別是,不加直接訪問網(wǎng)址即可,加了就需要訪問域名/grafana才能訪問,如:grafana.ywbj.cc/grafana/ 才能訪問。我這里配置就沒有加。
- serve_from_sub_path:去掉注釋,將false改成true即可。
[server]
domain = grafana.ywbj.cc
root_url = %(protocol)s://%(domain)s:%(http_port)s/
serve_from_sub_path = true
grafana配置完成,下面就是配置nginx了。官方nginx配置文件:
注:如果上面root_url 后面加了grafana/,則location / 改為location /grafana/
。
# this is required to proxy Grafana Live WebSocket connections.
map $http_upgrade $connection_upgrade {default upgrade;'' close;
}server {listen 80;server_name grafana.initcapp.com;root /usr/share/nginx/html;index index.html index.htm;location / {proxy_set_header Host $http_host;proxy_pass http://localhost:3000/;}# Proxy Grafana Live WebSocket connections.location /api/live {rewrite ^/(.*) /$1 break;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;proxy_set_header Host $http_host;proxy_pass http://localhost:3000/;}
}
上面配置好了,然后啟動,雖然可以訪問,也可以添加資源,但是在實時查看日志流,會出現(xiàn)錯誤。
Query error Live tailing was stopped due to following error: undefined,
如下圖:
原因是websocket出錯,即配置文件中l(wèi)ocation /api/live 這個區(qū)域。
根據(jù)另一篇官方文檔:https://grafana.com/docs/grafana/latest/live/configure-grafana-live/
修改nginx配置文件,最后nginx配置文件為:
map $http_upgrade $connection_upgrade {default upgrade;'' close;
}server {listen 80;server_name grafana.ywbj.cc;#root /usr/share/nginx/html;#index index.html index.htm;error_log /var/log/nginx/nginx.localhost.error.log warn;location / {proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;proxy_set_header Host $http_host;proxy_pass http://localhost:3030/;#默認(rèn)為3000端口,我配置為3030,所以是3030.}
這樣啟動后,功能一切正常了。
三、客戶端安裝及配置
loki客戶端
,及需要收集日志的另一臺機器,只需要安裝promtail
,也可以docker啟動安裝,也可以二進制安裝。
由于本人收集日志有其他目錄,如果用docker,需要配置掛載很多路徑到docker,所以我在客戶端直接用二進制啟動即可,配置文件比較容易。
二進制官方下載地址:https://github.com/grafana/loki/releases
找到自己合適的版本promtail,我這里是這個。
下載后,解壓即可,得到一個可執(zhí)行文件promtail-linux-amd64。
root@web:/loki# ls
config.yaml promtail-linux-amd64
還需要創(chuàng)建一個config.yaml
文件,用于啟動promtail
。
sudo vim config.yaml
如果有多路徑及目錄,則添加多個job_name即可。
注意:clients中的url為你服務(wù)器的地址。
server:http_listen_port: 9080grpc_listen_port: 0positions:filename: /tmp/positions.yamlclients:- url: http://192.168.152.100:3100/loki/api/v1/push#服務(wù)器端loki的IP地址scrape_configs:
- job_name: webstatic_configs:- targets:- localhostlabels:job: web-varlogs__path__: /var/log/*log- job_name: webstatic_configs:- targets:- localhostlabels:job: nginx-logs__path__: /home/ubuntu/laradock/logs/nginx/*log- job_name: webstatic_configs:- targets:- localhostlabels:job: api-logs__path__: /home/ubuntu/api/storage/logs/*log
創(chuàng)建完配置文件,啟動即可。
./promtail-linux-amd64 -config.file=./config.yaml
后臺啟動,并輸入日志。
#輸入日志到指定位置
nohup ./promtail-linux-amd64 -config.file=./config.yaml > /var/log/promtail.log 2>&1 &
#不需要日志輸入,直接為空。
nohup ./promtail-linux-amd64 -config.file=./config.yaml > /dev/null 2>&1 &
啟動后,用ps可查看進程是否啟動
root@web:/loki# ps -ef|grep promtail
root 1466400 1450430 7 10:41 pts/0 00:00:00 ./promtail-linux-amd64 -config.file=./config.yaml
root 1466415 1450430 0 10:41 pts/0 00:00:00 grep --color=auto promtail
四、grafana使用添加loki
在瀏覽器輸入域名訪問,默認(rèn)賬號密碼為admin/admin
然后在data sources
,點擊添加add data source
,選擇loki
即可。
然后添加服務(wù)器loki的地址即可,地址為IP:3100,loki的默認(rèn)端口為3100。
添加完成,沒有報錯,說明添加成功。
在explore
,在 log browser
中選擇,即可查看剛才添加的日志,這里以job分類。
選擇一個,show logs
即可查看,右上角為實時流查看日志。
這里loki、promtail、Grafana日志系統(tǒng)就配置完了。