自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Loki日志全鏈路實(shí)戰(zhàn):從收集到告警的完整指南

云計(jì)算 云原生
Loki相對(duì)輕量級(jí),具有較高的可擴(kuò)展性和簡(jiǎn)化的存儲(chǔ)架構(gòu),但需要額外的組件,Loki使用的是自己的查詢語(yǔ)言LokiQL,有一定的學(xué)習(xí)曲線。若是數(shù)據(jù)量適中,數(shù)據(jù)屬于時(shí)序類,如應(yīng)用程序日志和基礎(chǔ)設(shè)施指標(biāo),并且應(yīng)用使用kubernetes Pod形式部署,則選擇Loki比較合適。

1.為什么選擇Loki ?

傳統(tǒng)日志系統(tǒng)的痛點(diǎn)

  • 資源消耗高:ELK(Elasticsearch)需要為全文檢索建立復(fù)雜索引,存儲(chǔ)成本飆升,官方數(shù)據(jù)是通??梢詫?shù)據(jù)壓縮20%~30%,實(shí)測(cè)下來(lái)在某些場(chǎng)景下可以達(dá)到50%,使用DEFLATE壓縮算法,最低可以降低超過(guò)70%
  • 云原生適配差:Kubernetes動(dòng)態(tài)環(huán)境下的日志標(biāo)簽管理困難

Loki的核心優(yōu)勢(shì)

  • 輕量級(jí)設(shè)計(jì):僅索引元數(shù)據(jù)(標(biāo)簽),有近10倍的壓縮比,5G數(shù)據(jù)輸入存儲(chǔ)實(shí)際增長(zhǎng)0.5G。
  • 無(wú)縫集成:原生支持Prometheus生態(tài),原生集成grafana,支持集群本身監(jiān)控告警和日志監(jiān)控告警。
  • 動(dòng)態(tài)標(biāo)簽:完美適配Kubernetes Pod/Node等動(dòng)態(tài)資源標(biāo)簽

2.Loki架構(gòu)解析

核心組件與數(shù)據(jù)流

Loki則采用了分布式架構(gòu),將日志數(shù)據(jù)存儲(chǔ)在多個(gè)節(jié)點(diǎn)上,Promtail進(jìn)行日志收集,可視化依賴于Grafana。

關(guān)鍵概念解析

圖片

3.部署

環(huán)境準(zhǔn)備

  • 硬件建議:

生產(chǎn)環(huán)境:至少3節(jié)點(diǎn)集群(每個(gè)節(jié)點(diǎn)4核8GB+)

存儲(chǔ):優(yōu)先選擇SSD + S3兼容存儲(chǔ)(MinIO)

  • 版本選擇:
推薦使用v2.8+版本
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update

Kubernetes集群部署

使用Helm部署Loki Stack(包含Promtail)
helm upgrade --install loki grafana/loki-stack \
  --set promtail.enabled=true \
  --set loki.persistence.enabled=true \
  --set loki.persistence.size=100Gi

Promtail配置示例

#promtail-config.yaml
server:
  http_listen_port: 9080


positions:
  filename: /tmp/positions.yaml


clients:
url: http://loki:3100/loki/api/v1/push


scrape_configs:
job_name: kubernetes
kubernetes_sd_configs:
role: pod
  pipeline_stages:
docker: {}
  relabel_configs:
source_labels: [__meta_kubernetes_pod_label_app]
  target_label: app

4.告警集成

grafana告警規(guī)則配置

圖片圖片

圖片圖片

5.性能優(yōu)化與分析實(shí)踐

存儲(chǔ)優(yōu)化技巧

#loki-config.yaml 優(yōu)化片段
schema_config:
  configs:
    - from: 2023-01-01
      store: boltdb-shipper
      object_store: s3
      schema: v11
      index:
        prefix: index_
        period: 24h


chunk_store_config:
  max_look_back_period: 720h # 日志保留30天

日志查詢和分析

圖片圖片

  1. 首先,指定 查詢文件 filename="/usr/local/nginx/logs/loki-error.log"
  2. 然后,通過(guò)json的方式
  3. 最后,設(shè)置條件選擇,比如status=500、server_name=test.com

查詢加速策略

  • 預(yù)過(guò)濾標(biāo)簽
#先通過(guò)精確標(biāo)簽縮小范圍
{cluster="aws", pod=~"frontend-.+"} |= "timeout"
  • 避免全文本掃描:優(yōu)先使用|=(包含)而非|~(正則)

6.真實(shí)場(chǎng)景案例

案例1:nginx5xx日志收集

重新編譯nginx

#上傳安裝包
nginx-1.25.3.tar.gz、ngx_http_geoip2_module-3.4.tar.gz、libmaxminddb-1.9.1.tar.gz、GeoLite2-City.mmdb、GeoLite2-Country.mmdb


#nginx重新編譯
./configure --prefix=/usr/local/nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-dynamic-module=/root/nginx/ngx_http_geoip2_module-3.4

修改日志格式

load_module modules/ngx_http_geoip2_module.so;
#修改日志格式
  log_format json_analytics escape=json '{'
                    '"msec": "$msec", ' 
                    '"connection": "$connection", '
                    '"connection_requests": "$connection_requests", ' 
                    '"http_referer": "$http_referer", '
                    '"http_user_agent": "$http_user_agent", ' 
                    '"request_method": "$request_method", '
                    '"server_protocol": "$server_protocol", '
                    '"status": "$status", '
                    '"pipe": "$pipe", ' 
                    '"server_name": "$server_name", '
                    '"request_time": "$request_time", ' 
                    '"gzip_ratio": "$gzip_ratio", '
                    '"http_cf_ray": "$http_cf_ray",'
                    '"geoip_country_code": "$geoip2_data_country_code",'
                    '"geoip2_city_name_en": "$geoip2_city_name_en", '
                    '"geoip2_city_name_cn": "$geoip2_city_name_cn", '
                    '"geoip2_longitude": "$geoip2_longitude", '
                    '"geoip2_latitude": "$geoip2_latitude"'
                    '}';
    map $http_x_forwarded_for $real_ip {
        default "";
        "~^(?P<ip>\d+\.\d+\.\d+\.\d+)" $ip;
        "~^(?P<ip>\d+\.\d+\.\d+\.\d+),.*" $ip;
    }
#指定日志的位置,會(huì)放在nginx目錄下
    map $status $log_path {
        ~^4       /logs/loki-error.log;
        ~^5       /logs/loki-error.log;
        default   /logs/access.log;
    }
    geoip2 /usr/local/nginx/modules/GeoLite2-Country.mmdb {
        auto_reload 5m;
        $geoip2_metadata_country_build metadata build_epoch;
        $geoip2_data_country_code source=$real_ip country iso_code;
        $geoip2_data_country_name country names zh-CN;
    }


    geoip2 /usr/local/nginx/modules/GeoLite2-City.mmdb {
         $geoip2_city_name_en source=$real_ip city names en;
         $geoip2_city_name_cn source=$real_ip city names zh-CN;
         $geoip2_longitude source=$real_ip location longitude ;
         $geoip2_latitude source=$real_ip location latitude ;
    }
    fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
    fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
    fastcgi_param CITY_NAME    $geoip2_city_name_cn;    check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
    check_http_send "HEAD /health/check/status HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;

配置promtail收集

cat << EOF >> /data/promtail/config.yaml
server:
  http_listen_port: 9080
  grpc_listen_port: 0


positions:
  filename: /tmp/positions.yaml


clients:
url: http://192.168.119.191:3100/loki/api/v1/push


scrape_configs:
job_name: nginx
static_configs:
targets:
localhost
    labels:
    job: nginx
    host: $host
    agent: promtail
    path    : /usr/local/nginx/logs/loki-error.log
EOF

結(jié)合grafana呈現(xiàn)效果

圖片圖片

案例2:數(shù)據(jù)庫(kù)慢查詢收集

配置promtail收集

cat config.yaml 
server:
  http_listen_port: 9080
  grpc_listen_port: 0
positions:
  filename: /tmp/positions.yaml
clients:
url: http://192.168.119.191:3100/loki/api/v1/push
scrape_configs:
    - job_name: mysql-slowlogs
      static_configs:
        - labels:
            instance: usa4-rds03-slave
            projectname: chat
            job: mysql
            __path__: /database/mysql/1266/mysql-slow.log
      pipeline_stages:
        - multiline:
              firstline: '#\sTime:.'
        - regex:
*              expression: '#\s*Time:\s*(?P<timestamp>.*)\n#\s*User@Host:\s*(?P<user>[^\[]+).*@\s*(?P<host>.*]).***Id:\s***(?P<id>\d+)\n#\s*Query_time:\s*(?P<query_time>\d+\.\d+)\s*Lock_time:\s*(?P<lock_time>\d+\.\d+)\s*Rows_sent:\s*(?P<rows_sent>\d+)\s*Rows_examined:\s*(?P<rows_examined>\d+)\n(?P<query>(?s:.*))$'
        - labels:
            timestamp:
            user:
            host:
            id:
            query_time:
            lock_time:
            rows_sent:
            rows_examined:
            query:
        - drop:
            expression: "^ *$"
            drop_counter_reason: "drop empty lines"

結(jié)合grafana呈現(xiàn)效果

圖片圖片

7.總結(jié)

總體而言,Loki相對(duì)輕量級(jí),具有較高的可擴(kuò)展性和簡(jiǎn)化的存儲(chǔ)架構(gòu),但需要額外的組件,Loki使用的是自己的查詢語(yǔ)言LokiQL,有一定的學(xué)習(xí)曲線。若是數(shù)據(jù)量適中,數(shù)據(jù)屬于時(shí)序類,如應(yīng)用程序日志和基礎(chǔ)設(shè)施指標(biāo),并且應(yīng)用使用kubernetes Pod形式部署,則選擇Loki比較合適。

Loki正在成為云原生時(shí)代日志管理的標(biāo)準(zhǔn)解決方案,其與以下技術(shù)的整合值得關(guān)注:

  • OpenTelemetry:統(tǒng)一日志、指標(biāo)、追蹤的采集標(biāo)準(zhǔn)
  • WAL(Write-Ahead Logging):提升高吞吐場(chǎng)景下的可靠性
  • 邊緣計(jì)算:輕量級(jí)特性適合IoT設(shè)備日志收集
責(zé)任編輯:武曉燕 來(lái)源: 新鈦云服
相關(guān)推薦

2022-04-27 10:53:34

web優(yōu)化性能

2022-07-22 07:59:17

日志方案

2022-12-29 08:00:26

Loki網(wǎng)絡(luò)設(shè)備

2024-10-23 12:50:13

數(shù)據(jù)飛輪數(shù)字化

2012-08-31 09:57:08

Ajaxajax開發(fā)開發(fā)

2021-04-23 23:27:00

數(shù)據(jù)資產(chǎn)IT運(yùn)營(yíng)

2017-10-31 09:43:31

2024-02-04 00:00:00

Loki性能查詢

2024-03-11 00:01:00

PromtailLoki服務(wù)器

2021-05-18 07:30:36

開發(fā)Spring Boot日志

2023-01-30 22:34:44

Node.js前端

2022-01-18 08:12:34

JWT鏈路微服務(wù)

2024-01-05 00:29:36

全鏈路灰度發(fā)布云原生

2024-09-06 17:45:55

Linux磁盤

2024-08-21 08:09:17

2024-05-06 08:45:25

Spring分布式日志

2023-10-16 23:43:52

云原生可觀測(cè)性

2023-11-13 10:41:44

Spring微服務(wù)

2024-02-01 09:48:17

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)