【Elastic Stack】日誌收集系統

日誌收集系統

1. 爲何要收集日誌?

在項目開發和測試過程當中,日誌是定位問題的重要依據。html

日誌級別

經過日誌級別進行分級管理。node

  • DEBUG (調試)
  • INFO (記錄)
  • WARN (警告)
  • ERROE (異常)
日誌格式化

經過格式化提升日誌可讀性。
經常使用日誌格式:時間 模塊 行數 日誌內容linux

SpringBoot默認日誌格式(集成sleuth zipkin,日誌等級中會追加 [項目名,traceId,spanId,result]):git

2019-12-13 14:56:46.247  INFO [base-server,,,] 32576 --- [AsyncResolver-bootstrap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration$

時間戳 日誌級別 [項目名,traceId,spanId,result] 進程ID --- [線程名] log名 : 日誌內容github

2. Elastic Stack 是什麼?

Elastic Stack 包括 Elasticsearch、Kibana、Beats 和 Logstash(也稱爲 ELK Stack)。可以安全可靠地獲取任何來源、任何格式的數據,而後實時地對數據進行搜索、分析和可視化。
官網: ELK Stacknpm

Elasticsearch is the distributed search and analytics engine at the heart of the Elastic Stack. Logstash and Beats facilitate collecting, aggregating, and enriching your data and storing it in Elasticsearch. Kibana enables you to interactively explore, visualize, and share insights into your data and manage and monitor the stack. Elasticsearch is where the indexing, search, and analysis magic happen.bootstrap

Elasticsearch

是一個分佈式搜索和分析引擎,存儲數據索引、並提供搜索和分析。
文檔:Elasticsearch跨域

Kibana

基於可視化界面訪問ES數據索引,分析數據及數據可視化。
管理和監控ES的堆棧。
文檔:Kibana緩存

Beats

從數據源中獲取數據並存儲到ES。
也能夠上傳到LogStash,經過LogStash格式化再存儲到ES。
也能夠傳送到kafka,經過隊列進行緩存、削峯。安全

日誌收集主要是採集日誌文件,因此Beats選用FileBeat
FileBeat經過監聽文件內容變化,以行爲單位採集數據(支持多行合併)
文檔:FileBeat

Logstash

負責收集數據,處理數據,上傳數據(支持codec)。
input => decode => filter => encode => output
文檔:Logstash

3.如何基於ELK搭建日誌收集系統?

如圖:
ELK Stack

3.1 部署環境

基礎環境(點擊名稱進入對應下載頁面)
軟件 版本 備註
Liunx CentOS7
jdk 1.8.0_211
node 10.16.0
elasticsearch 7.5.0
kibana 7.5.0
logstash 7.5.0
filebeat 7.5.0
logstash 7.5.0
elasticsearch-head master ES插件,ES可視化
elasticsearch-analysis-ik master ES插件,IK分詞器
elasticsearch-analysis-pinyin master ES插件,pinyin分詞器
機器環境
NODE IP 節點類型
data-1 X.X.X.210 數據節點(ES/ik/pinyin)
data-2 X.X.X.211 數據節點(ES/ik/pinyin)
data-3 X.X.X.212 數據節點(ES/ik/pinyin)
seek-1 X.X.X.214 搜索節點(ES/kibana/head/ik/pinyin)
other * 業務日誌收集節點(filebeats)

3.2 ES集羣

部署前準備:

1 安裝jdk,並檢查版本。1.8以上便可
2 安裝node,最新版本便可
3 添加linux系統帳號,部署ES須要非root帳號

3.2.1 下載解壓
tar -xvf elasticsearch-7.5.0-linux-x86_64.tar.gz
3.2.2 配置
  • elasticsearch-7.5.0/config/elasticsearch.yml
# 集羣名 保持一致
cluster.name: logging-dev

# 節點名 保證惟一 數據節點[data-x] 搜索節點[seek-x]
node.name: data-1
# 容許做爲主節點(默認true),可將搜索節點設爲false
node.master: true
# 容許存儲數據(默認true),將搜索節點設爲false
node.data: true
# 容許預處理數據(默認true),一般會單獨部署Ingest node進行數據預處理
# 可將數據節點設爲false
node.ingest: true

# 配置數據/日誌路徑
# 數據路徑容許配置多個(屬於同一分片的文件會存儲在同一路徑)
# 爲了方便往後ES升級,建議從新配置path.data和path.logs爲公共路徑
path.data: /opt/es/data/log-data-1
path.logs: /opt/es/logs/log-data-1

# 配置IP地址(有坑)
# 建議閱讀文檔:https://www.elastic.co/guide/en/elasticsearch/reference/7.5/network.host.html
network.bind_host: 0.0.0.0
network.publish_host: 172.16.131.214
network.host: 172.16.131.214
# 配置HTTP訪問端口,默認(9200-9300)
http.port: 9200
# 配置transport監聽端口,默認(9300-9400)
transport.port: 9300
# 配置跨域,確保elasticsearch-head和其餘服務訪問ES
http.cors.enabled: true
http.cors.allow-headers: "*"

# 配置集羣節點host列表(不包含搜索節點),保持一致
discovery.seed_hosts: ["X.X.X.210:9300", "X.X.X.211:9300","X.X.X.212:9300"]
# 配置初始選舉主節點node列表(不包含搜索節點),保持一致,
cluster.initial_master_nodes: ["data-1", "data-2", "data-3"]
  • elasticsearch-7.5.0/config/jvm.options
    一般只須要修改堆棧大小-xms -xmx,避免ES內存溢出
  • elasticsearch-7.5.0/config/log4j2.properties
3.2.3 安裝插件**[可選]
下載elasticsearch-analysis-ik, 解壓到elasticsearch-7.5.0/plugins/ik下
下載elasticsearch-analysis-pinyin,解壓到elasticsearch-7.5.0/plugins/pinyin
參考路徑:
-plugins
    -ik
        -plugin-descriptor.properties
        -略...
    -pinyin
        -plugin-descriptor.properties
        -略...
3.2.4 運行
啓動(不要用root帳號啓動)
    ./bin/elasticsearch [-Ehttp.port=9200] [-d]
    -Ekey=val 能夠覆蓋配置屬性
    -d 後臺運行ES
    
    運行成功後,訪問http://X.X.X.210:9200返回信息便可
    
中止
    jps
    kill -9 [pid]
    或者
    jps | grep Elasticsearch | awk '{print $1}' | xargs kill -9
3.2.5 運行Elasticsearch-head**[可選]
在seek-1節點(X.X.X.214)上部署便可
1. 下載並解壓Elasticsearch-head
    unzip elasticsearch-head-master.zip
2. 修改配置
    2.1 elasticsearch-head/Gruntfile.js
        vi Gruntfile.js 編輯Gruntfile.js
        /connect    查找connect,格式以下,修改配置便可
            connect: {
                    server: {
                            options: {
                                    hostname: '*',
                                    port: 9100,
                                    base: '.',
                                    keepalive: true
                            }
                    }
            }
    2.2 elasticsearch-head/_site/app.js
        vi ./_site/app.js
        /app-base_uri 查找app-base_uri,一般再按n查找下一次便可找到,大概在4374行
            init: function(parent) {
                        this._super();
                        this.prefs = services.Preferences.instance();
                        // 在此處配置ES集羣IP
                        this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri")
                                || "http://X.X.X.210:9200" || "http://X.X.X.211:9200" || "http://X.X.X.212:9200" || "http://X.X.X.214:9200";
                        if( this.base_uri.charAt( this.base_uri.length - 1 ) !== "/" ) {
                                // XHR request fails if the URL is not ending with a "/"
                                this.base_uri += "/";
                        }
                        ...

3. 運行
    啓動
        npm run start
        nohup npm run start & //後臺運行
        
        運行成功 訪問http://X.X.X.214:9100, 返回集羣信息便可
        
    中止
        netstat -lntp | grep 9100
        kill -9 [pid]

3.3 Kibana

在seek-1節點(X.X.X.214)上部署便可

3.3.1 下載解壓
tar -xvf kibana-7.5.0-linux-x86_64.tar.gz
3.3.2 配置
  • kibana-7.5.0-linux-x86_64/config/kibana.yml
# 配置端口
server.port: 5601
# 配置host
server.host: "0.0.0.0"
# 配置界面爲中文
i18n.locale: "zh-CN"
3.3.3 運行
啓動
    nohup ./bin/kibana > kibana.log 2>&1 &
    tail -f kibana.log

    執行成功,訪問http://X.X.X.214:5601

中止
    netstat -lntp|grep 5601
    kill -9 [pid]

3.4 logstash

logstash 暫時先在seek-1節點(X.X.X.214)上部署

3.4.1 下載解壓
tar -xvf logstash-7.5.0.tar.gz
3.4.2 配置
  • logstash-7.5.0/config/logging-dev.conf
# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
# 從beats中讀取數據
    beats {
        port => 5044
    }
}

filter {
# 經過grok插件對SpringBoot日誌進行格式化,基於正則,性能較差
#    grok { match => [ "message", "%{TIMESTAMP_ISO8601:time}\s+%{LOGLEVEL:level}\s+\[(?<application_name>.*),(?<traceId>.*),(?<spanId>.*),(?<result>.*)\]\s+%{NUMBER:t_id}\s+---\s+\[(?<t_name>.*?)\]\s+(?<log>.*?)\s+:(?<content>.*)" ] }
# 經過dissert插件對SpringBoot日誌進行格式化,嚴格按照格式分割,性能要比正則好。
    dissect {
        mapping => {
            "message" => "%{[@metadata][timestamp]} %{+[@metadata][timestamp]} %{log_level} [%{application_name},%{traceId},%{spanId},%{result}] %{t_id} --- [%{t_name}] %{log} :%{content}"
        }
    }
    date { 
        match => [ "[@metadata][timestamp]", "yyyy-MM-dd HH:mm:ss.SSS" ] 
    }
}

output {
# 上傳到ES中
    elasticsearch {
        hosts => ["http://172.16.131.210:9200","http://172.16.131.211:9200","http://172.16.131.212:9200"]
        index => ""
    }
}
配置修改後,記得執行配置校驗命令
./bin.logstash -f config/logging-dev.conf -t
3.4.3 運行
啓動
    nohup ./bin/logstash -n node-1 -f config/logging-dev.conf -r >/dev/null 2>&1 &
    tail -99f 
    
中止
    jps | grep Logstash
    kill -9 [pid]

3.5 部署filebeat

在業務服務器上部署(須要收集的日誌所在服務器)

3.5.1 下載並解壓filebeat
tar -xvf filebeat-7.5.0-linux-x86_64.tar.gz
3.5.2 配置filebeat
  • filebeat-7.5.0-linux-x86_64/filebeat.yml
filebeat.inputs:
    - type: log
    paths:
        - /var/log/*.log

filebeat.config.modules:
    path: ${path.config}/modules.d/*.yml
    reload.enabled: false
    
setup.template.settings:
    index.number_of_shards: 1

setup.kibana:
    host: "X.X.X.214:5601"
    
output.logstash:
    hosts: ["172.16.131.214:5044"]
3.5.3 運行filebeat
啓動
    nohup ./filebeat >/dev/null 2>&1 &
相關文章
相關標籤/搜索