柚子快報(bào)激活碼778899分享:日志收集 云原生日志Loki
柚子快報(bào)激活碼778899分享:日志收集 云原生日志Loki
1. Loki簡介
1.1 Loki介紹
Loki是 Grafana Labs 團(tuán)隊(duì)最新的開源項(xiàng)目,是一個(gè)水平可擴(kuò)展,高可用性,多租戶的日志聚合系統(tǒng)。它的設(shè)計(jì)非常經(jīng)濟(jì)高效且易于操作,因?yàn)樗粫?huì)為日志內(nèi)容編制索引,而是為每個(gè)日志流編制一組標(biāo)簽。項(xiàng)目受 Prometheus 啟發(fā),官方的介紹就是:Like Prometheus, but for logs,類似于 Prometheus 的日志系統(tǒng)。
1.2 Loki 的主要特點(diǎn)
水平可擴(kuò)展性:Loki 能夠通過增加更多的節(jié)點(diǎn)來擴(kuò)展其處理能力,以應(yīng)對不斷增長的數(shù)據(jù)量。 高可用性:設(shè)計(jì)上保證了系統(tǒng)的穩(wěn)定性和可靠性,即使在部分組件發(fā)生故障時(shí)也能繼續(xù)運(yùn)行。 多租戶支持:Loki 支持在同一個(gè)實(shí)例中為不同的用戶提供隔離的日志數(shù)據(jù)存儲(chǔ)和查詢服務(wù)。 經(jīng)濟(jì)高效:與傳統(tǒng)的日志系統(tǒng)相比,Loki 不會(huì)對日志內(nèi)容進(jìn)行全文索引,而是通過為每個(gè)日志流創(chuàng)建一組標(biāo)簽來組織數(shù)據(jù),這大大減少了存儲(chǔ)需求和提高了查詢效率。 易于操作:Loki 的設(shè)計(jì)理念是簡單易用,使得用戶可以快速上手并有效地管理日志數(shù)據(jù)。
2. Loki部署
主機(jī)名作用系統(tǒng)IPV4master采集端CentOS 7.9192.168.110.21/24node-01被采集端CentOS 7.9192.168.110.22/24node-02被采集端CentOS 7.9192.168.110.23/24
2.1 Loki主要組件
Loki 作為日志聚合系統(tǒng)的設(shè)計(jì)理念確實(shí)非常先進(jìn)和實(shí)用,它通過以下三個(gè)主要組件來實(shí)現(xiàn)高效的日志管理:
Promtail
Promtail 是 Loki 的日志收集器,它運(yùn)行在每個(gè)主機(jī)上,負(fù)責(zé)從本地文件中收集日志數(shù)據(jù)。 它使用類似于 Prometheus 的服務(wù)發(fā)現(xiàn)機(jī)制來動(dòng)態(tài)地發(fā)現(xiàn)日志文件。 Promtail 將日志作為流發(fā)送到 Loki 服務(wù)器,這些流由一組標(biāo)簽定義,而不是通過全文索引。 Loki
Loki 本身是一個(gè)高效的日志存儲(chǔ)系統(tǒng),它接收來自 Promtail 的日志數(shù)據(jù),并將其存儲(chǔ)在后端數(shù)據(jù)庫中,如 BoltDB 或 Cassandra。 Loki 提供了一個(gè) RESTful API,允許用戶查詢和聚合日志數(shù)據(jù)。 它的設(shè)計(jì)允許快速查詢和聚合,因?yàn)椴樵兪腔跇?biāo)簽的,而不是基于文本的全文搜索。 Grafana
Grafana 是一個(gè)流行的開源數(shù)據(jù)可視化和監(jiān)控平臺(tái),它與 Loki 緊密集成。 用戶可以通過 Grafana 來創(chuàng)建儀表板,實(shí)時(shí)監(jiān)控和分析 Loki 中的日志數(shù)據(jù)。 Grafana 提供了豐富的圖表和可視化選項(xiàng),幫助用戶更直觀地理解日志數(shù)據(jù)。
2.2 Loki主要特性
Loki 與其他日志聚合系統(tǒng)相比確實(shí)具有一些獨(dú)特的特性,這些特性使得 Loki 成為處理大規(guī)模日志數(shù)據(jù)的理想選擇,特別是在云原生和容器化環(huán)境中。它的簡單性、效率和成本效益使其在日志聚合領(lǐng)域中獨(dú)樹一幟。
不進(jìn)行全文索引:
Loki 不對日志內(nèi)容進(jìn)行全文索引,而是存儲(chǔ)壓縮的非結(jié)構(gòu)化日志數(shù)據(jù),并僅索引元數(shù)據(jù)。這種方法簡化了操作,減少了存儲(chǔ)和計(jì)算資源的需求,從而降低了成本。 使用標(biāo)簽記錄流:
Loki 采用了與 Prometheus 類似的標(biāo)簽系統(tǒng),通過標(biāo)簽來索引和分組日志流。這種方法提高了日志數(shù)據(jù)的擴(kuò)展性和操作效率,使得用戶可以快速地查詢和聚合具有特定標(biāo)簽的日志。 適合存儲(chǔ) Kubernetes Pod 日志:
Loki 特別適合于存儲(chǔ)和處理 Kubernetes 環(huán)境中的 Pod 日志。在 Kubernetes 中,Pod 是短暫的,而 Loki 的設(shè)計(jì)允許它高效地處理這些短暫生命周期的日志源。 Kubernetes Pod 的標(biāo)簽等元數(shù)據(jù)可以自動(dòng)被 Loki 索引,這使得用戶能夠利用這些標(biāo)簽來查詢特定 Pod 或命名空間的日志。 水平可擴(kuò)展性:
Loki 設(shè)計(jì)為易于水平擴(kuò)展,可以通過增加更多的節(jié)點(diǎn)來處理更多的日志數(shù)據(jù),這使得它能夠適應(yīng)不斷增長的數(shù)據(jù)量。 多租戶支持:
Loki 支持多租戶部署,允許多個(gè)團(tuán)隊(duì)或項(xiàng)目在同一個(gè) Loki 實(shí)例上獨(dú)立地存儲(chǔ)和查詢?nèi)罩荆粫?huì)相互干擾。 與 Grafana 的集成:
Loki 與 Grafana 的緊密集成提供了強(qiáng)大的可視化和監(jiān)控能力,使得用戶可以輕松地創(chuàng)建儀表板和警報(bào)。 成本效益:
由于 Loki 的設(shè)計(jì)減少了對存儲(chǔ)和計(jì)算資源的需求,它提供了一種成本效益更高的日志聚合解決方案,特別是在處理大規(guī)模日志數(shù)據(jù)時(shí)。 各日志收集組件簡單對比
名稱安裝的組件優(yōu)點(diǎn)ELK/EFKElasticsearch, Logstash支持自定義grok正則解析復(fù)雜日志內(nèi)容- Dashboard支持豐富的可視化展示Kibana, Filebeat, Kafka/RedisLokiGrafana, Loki, Promtail占用資源小- Grafana原生支持- 查詢速度快
相關(guān)資料
官方地址:Grafana Loki OSS | Log aggregation system 文檔地址:Configure the Loki data source | Grafana documentation git地址:https://github.com/grafana/loki/blob/master/docs/README.md 下載地址:https://github.com/grafana/loki/releases/
2.3 安裝Loki
Grafana Loki 是一個(gè)日志聚合工具,它是功能齊全的日志堆棧的核心。 Loki 是一個(gè)為有效保存日志數(shù)據(jù)而優(yōu)化的數(shù)據(jù)存儲(chǔ)。日志數(shù)據(jù)的高效索引將 Loki 與其他日志系統(tǒng)區(qū)分開來, 與其他日志系統(tǒng)不同,Loki 索引是根據(jù)標(biāo)簽構(gòu)建的,原始日志消息未編入索引。
?[root@master ~]# wget -c https://github.com/grafana/loki/releases/download/v2.9.8/loki-2.9.8.x86_64.rpm
?[root@master ~]# yum install loki-2.9.8.x86_64.rpm -y
?[root@master ~]# sed -i 's/127.0.0.1/192.168.110.21/' /etc/loki/config.yml # 修改IP
?[root@master ~]# systemctl enable loki --now
?[root@master ~]# systemctl is-active loki.service
?active
?[root@master ~]# ss -lnupt | egrep '3100|9096'
?tcp ? LISTEN ? ? 0 ? ? ?128 ? [::]:9096 ? ? ? ? ? ? ? [::]:* ? ? ? ? ? ? ? ? ? users:(("loki",pid=11871,fd=9))
?tcp ? LISTEN ? ? 0 ? ? ?128 ? [::]:3100 ? ? ? ? ? ? ? [::]:* ? ? ? ? ? ? ? ? ? users:(("loki",pid=11871,fd=8))
文件介紹
?[root@master ~]# rpm -ql loki
?/etc/loki/config.yml ? ? ? ? ? ? ? # 配置文件
?/etc/systemd/system/loki.service ? # 系統(tǒng)服務(wù)文件
?/usr/bin/loki ? ? ? ? ? ? ? ? ? ? ?# 二進(jìn)制文件
??
?[root@master ~]# cat /etc/loki/config.yml
?auth_enabled: false ? ? # 是否啟用鑒權(quán)
??
?server:
? http_listen_port: 3100 ?# http訪問端口
? grpc_listen_port: 9096 ?# rpc訪問端口
??
?common:
? instance_addr: 127.0.0.1 ?# 修改為自己的IP或localhost
? path_prefix: /tmp/loki
? storage:
? ? filesystem:
? ? ? chunks_directory: /tmp/loki/chunks # 記錄塊存儲(chǔ)目錄,默認(rèn)chunks塊上的日志數(shù)量或到期后,將chunks數(shù)據(jù)打標(biāo)簽后存儲(chǔ)
? ? ? rules_directory: /tmp/loki/rules
? replication_factor: 1
? ring:
? ? kvstore:
? ? ? store: inmemory
??
?query_range: ? ? ? ? ? ?# 查詢規(guī)則
? results_cache:
? ? cache:
? ? ? embedded_cache:
? ? ? ? enabled: true
? ? ? ? max_size_mb: 100
??
?schema_config:
? configs:
? ? ?- from: 2020-10-24
? ? ? store: boltdb-shipper
? ? ? object_store: filesystem
? ? ? schema: v11
? ? ? index:
? ? ? ? prefix: index_
? ? ? ? period: 24h
??
?ruler:
? alertmanager_url: http://localhost:9093 ? # 告警通知url,修改為自己的IP
? ?
?# 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
2.4 安裝Promtail
?[root@master ~]# wget -c https://github.com/grafana/loki/releases/download/v2.9.8/promtail-2.9.8.x86_64.rpm
?[root@master ~]# yum install promtail-2.9.8.x86_64.rpm -y
?[root@master ~]# setfacl -m u:promtail:r /var/log/messages # 注意默認(rèn)是沒有讀權(quán)限
?[root@master ~]# systemctl enable promtail --now
?[root@master ~]# systemctl is-active promtail
?active
測試
訪問http://192.168.110.21:9080/targets
?[root@master ~]# cat /tmp/positions.yaml
?positions:
? /var/log/messages: "1008852"
配置文件詳解
?[root@master ~]# cat /etc/promtail/config.yml
?# This minimal config scrape only single log file.
?# Primarily used in rpm/deb packaging where promtail service can be started during system init process.
?# And too much scraping during init process can overload the complete system.
?# https://github.com/grafana/loki/issues/11398
??
?server:
? http_listen_port: 9080
? grpc_listen_port: 0
??
?positions:
? filename: /tmp/positions.yaml # 用于記錄每次讀取日志文件的索引行數(shù),如:promtail重啟后從該配置中恢復(fù)日志文件的讀取位置
??
?clients:
?- url: http://localhost:3100/loki/api/v1/push ?# 推送日志流到Loki中的api
??
?scrape_configs: ? ? # 發(fā)現(xiàn)日志文件的位置并從中提取標(biāo)簽
?- job_name: system ?# 任務(wù)名稱
? static_configs: ? # 目錄配置
? ?- targets: ? ? ? ?# 標(biāo)簽
? ? ? ?- localhost
? ? labels:
? ? ? job: varlogs ?# 子任務(wù)名稱,通常以項(xiàng)目命令
? ? ? ?#NOTE: Need to be modified to scrape any additional logs of the system.
? ? ? __path__: /var/log/messages ?# 收集本地系統(tǒng)日志
2.5 安裝命令行工具
?[root@master ~]# wget -c https://github.com/grafana/loki/releases/download/v2.9.8/logcli-2.9.8.x86_64.rpm
?[root@master ~]# yum install logcli-2.9.8.x86_64.rpm -y
?[root@master ~]# logcli --help
?--help:顯示幫助信息。
?--version:顯示應(yīng)用程序版本。
?--quiet:抑制查詢元數(shù)據(jù)。
?--stats:顯示查詢統(tǒng)計(jì)信息。
?--output:指定輸出模式,可選 default, raw, jsonl。raw 模式會(huì)抑制日志標(biāo)簽和時(shí)間戳。
?--timezone:指定格式化輸出時(shí)間戳?xí)r使用的時(shí)區(qū),默認(rèn)為本地時(shí)區(qū)或 UTC。
?--cpuprofile 和 --memprofile:指定 CPU 和內(nèi)存分析文件的寫入位置。
?--stdin:從標(biāo)準(zhǔn)輸入讀取日志輸入。
?--addr:設(shè)置 Loki 服務(wù)器地址,默認(rèn)為 http://localhost:3100。
?--username 和 --password:設(shè)置 HTTP 基本認(rèn)證的用戶名和密碼。
?--ca-cert:設(shè)置服務(wù)器 CA 證書的路徑。
?--tls-skip-verify:跳過 TLS 服務(wù)器證書驗(yàn)證。
?--cert 和 --key:設(shè)置客戶端證書和密鑰的路徑。
?--org-id:添加 API 請求的租戶 ID。
?--query-tags:添加 HTTP 請求頭,用于跟蹤查詢統(tǒng)計(jì)。
?--bearer-token 和 --bearer-token-file:設(shè)置用于 API 請求的身份驗(yàn)證令牌。
?--retries:設(shè)置查詢失敗時(shí)的重試次數(shù)。
?--min-backoff 和 --max-backoff:設(shè)置重試之間的最小和最大回退時(shí)間。
?--auth-header:設(shè)置用于 API 請求的身份驗(yàn)證頭部。
?--proxy-url:設(shè)置 HTTP 或 HTTPS 代理的 URL。
2.6 安裝Granafa
?[root@master ~]# wget -c https://dl.grafana.com/enterprise/release/grafana-enterprise-10.0.2-1.x86_64.rpm
?[root@master ~]# yum install grafana-enterprise-10.0.2-1.x86_64.rpm -y
?[root@master ~]# systemctl enable --now grafana-server
訪問: http://192.168.110.21:3000
用戶名密碼都為:admin
添加Loki數(shù)據(jù)源
Data sources ——> Add new data source ——> Loki
2.7 增加一個(gè)標(biāo)簽
?[root@master ~]# vim /etc/promtail/config.yml
?...
?scrape_configs:
?- job_name: system
? static_configs:
? ?- targets:
? ? ? ?- localhost
? ? labels:
? ? ? job: varlogs
? ? ? ?#NOTE: Need to be modified to scrape any additional logs of the system.
? ? ? __path__: /var/log/messages
? ?- targets: ? ? ? ? ? ? ?# 添加一個(gè)targets
? ? ? ?- localhost
? ? labels:
? ? ? job: varlogs-02
? ? ? __path__: /var/log/secure
??
?[root@master ~]# setfacl -m u:promtail:r /var/log/secure
?[root@master ~]# systemctl restart promtail.service
添加監(jiān)控指標(biāo)
3. LogQL語法
在 Loki 中,查詢?nèi)罩緮?shù)據(jù)時(shí)可以使用標(biāo)簽選擇器和過濾表達(dá)式來精確地定位和檢索所需的日志流。
3.1 標(biāo)簽選擇器
標(biāo)簽選擇器用于指定一組標(biāo)簽,以便從 Loki 中檢索匹配特定標(biāo)簽的日志流。 選擇器放在 {} 中,多個(gè)標(biāo)簽表達(dá)式用逗號(hào)分隔。
3.1.1 支持的符號(hào)
=:表示標(biāo)簽的值需要完全相同。 !=:表示標(biāo)簽的值需要不平等。 =~:表示標(biāo)簽的值需要與正則表達(dá)式匹配。 !~:表示標(biāo)簽的值不能與正則表達(dá)式匹配。
3.1.2 過濾表達(dá)式
在選擇了日志流之后,可以使用搜索表達(dá)式進(jìn)一步過濾結(jié)果。 搜索表達(dá)式可以是文本或正則表達(dá)式。
3.1.3 示例
{job="mysql"} |= "error":選擇 job 標(biāo)簽為 mysql 的日志流,并且日志行包含字符串 "error"。 {name="kafka"} |~ "tsdb-ops.*io:2003":選擇 name 標(biāo)簽為 kafka 的日志流,并且日志行匹配正則表達(dá)式 "tsdb-ops.*io:2003"。 {instance=~"kafka-[23]",name="kafka"} != kafka.server:type=ReplicaManager:選擇 instance 標(biāo)簽匹配正則表達(dá)式 "kafka-[23]" 并且 name 標(biāo)簽為 kafka 的日志流,排除包含 kafka.server:type=ReplicaManager 的行。
3.1.4 支持的操作符
|=:日志行包含指定的字符串。 !=:日志行不包含指定的字符串。 |~:日志行匹配指定的正則表達(dá)式。 !~:日志行不匹配指定的正則表達(dá)式。
3.1.5 支持多個(gè)過濾
可以在同一查詢中使用多個(gè)過濾條件,例如:
{job="mysql"} |= "error" != "timeout":選擇 job 標(biāo)簽為 mysql 的日志流,日志行包含 "error" 但不包含 "timeout"。
4. Loki收集Nginx日志
為了方便grafana dashbord展示,我們把日志格式修改為json
4.1 修改Nginx配置
# 在被監(jiān)控端上做
[root@node-01~]# vim /etc/nginx/nginx.conf # 再http模塊內(nèi)添加
http {
...
log_format json escape=json '{'
'"remote_addr": "$remote_addr", '
'"request_uri": "$request_uri", '
'"request_length": "$request_length", '
'"request_time": "$request_time", '
'"request_method": "$request_method", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_host": "$http_host", '
'"server_name": "$server_name", '
'"upstream": "$upstream_addr", '
'"upstream_response_time": "$upstream_response_time", '
'"upstream_status": "$upstream_status"'
# '"geoip_country_code": "$geoip2_data_country_code", '
# '"geoip_country_name": "$geoip2_data_country_name", '
# '"geoip_city_name": "$geoip2_data_city_name"'
'};';
access_log /var/log/nginx/json_access.log json;
}
[root@node-01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node-01 ~]# systemctl start nginx
訪問Nginx
[root@node-01 ~]# tail /var/log/nginx/access.log
192.168.110.23 - - [15/Jun/2024:19:45:37 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.23 - - [15/Jun/2024:19:45:38 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.23 - - [15/Jun/2024:19:45:38 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.21 - - [15/Jun/2024:19:45:42 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.21 - - [15/Jun/2024:19:45:42 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.21 - - [15/Jun/2024:19:45:44 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.21 - - [15/Jun/2024:19:45:44 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.21 - - [15/Jun/2024:19:45:44 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.21 - - [15/Jun/2024:19:45:45 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.110.21 - - [15/Jun/2024:19:45:45 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
4.2 安裝Promtail
4.2.1 安裝
[root@node-01 ~]# yum install promtail-2.9.8.x86_64.rpm -y
4.2.2 修改targets
[root@node-01 ~]# vim /etc/promtail/config.yml
# This minimal config scrape only single log file.
# Primarily used in rpm/deb packaging where promtail service can be started during system init process.
# And too much scraping during init process can overload the complete system.
# https://github.com/grafana/loki/issues/11398
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://192.168.110.21:3100/loki/api/v1/push # 指定收集端
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: nginxlogs # 修改標(biāo)簽
host: 192.168.110.22
#NOTE: Need to be modified to scrape any additional logs of the system.
__path__: /var/log/nginx/*.log # 采集的日志
4.2.3 授權(quán)啟動(dòng)
[root@node-01 ~]# setfacl -m u:promtail:r /var/log/nginx/*.log
[root@node-01 ~]# systemctl restart promtail
4.3 Granafa采集數(shù)據(jù)
4.4 添加Doshbarod
ID:16101
柚子快報(bào)激活碼778899分享:日志收集 云原生日志Loki
文章鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。