三分鐘搭建ELK日志分析平臺

三分鐘搭建ELK日志分析平臺

初步認識ELK

所謂的ELK就是指三個組件的首字母集合:

Elasticsearch 是一個實時的分布式搜索和分析引擎,它可以用于全文搜索,結(jié)構(gòu)化搜索以及分析。它是一個建立在全文搜索引擎 Apache Lucene 基礎(chǔ)上的搜索引擎,使用 Java 語言編寫。

Logstash 是一個具有實時渠道能力的數(shù)據(jù)收集引擎,主要用于日志的收集與解析,并將其存入 ElasticSearch中。

Kibana 是一款基于 Apache 開源協(xié)議,使用 JavaScript 語言編寫,為 Elasticsearch 提供分析和可視化的 Web 平臺。它可以在 Elasticsearch 的索引中查找,交互數(shù)據(jù),并生成各種維度的表圖。

由于這種架構(gòu)下,Logstash的壓力過大,所以在Logstash的前端加了一層輕量級的日志收集中間件:

引入Filebeat作為日志搜集器,主要是為了解決Logstash開銷大的問題。相比Logstash,F(xiàn)ilebeat 所占系統(tǒng)的 CPU 和內(nèi)存幾乎可以忽略不計。

因而最終形成的架構(gòu)圖如下:

image

開始搭建

本手冊base在你已經(jīng)有一定的Java編程基礎(chǔ)

基礎(chǔ)環(huán)境

系統(tǒng)環(huán)境:Centos6.8(使用vmware12虛擬機)

Java環(huán)境:1.8.0_161(由于ELK5.X和6.X版本最低要求是Java8,所以建議配置8以上版本)

軟件包相關(guān)信息

軟件版本:6.5.4

下載地址:

[官網(wǎng)下載地址] https://www.elastic.co/downloads

下載軟件集合:Elasticsearch、Kibana、Logstash、FileBeat(都選擇linux版的tar包進行下載)

下載后將安裝包傳輸?shù)教摂M機中。

安裝開始

解壓縮

首先安裝解壓縮到一個目錄下面,個人習慣放在/opt下面

創(chuàng)建用戶

因為ElasticSerach運行時不允許以root用戶身份的,所以這里需要手動創(chuàng)建用戶并分配權(quán)限,具體如下:

  1. 創(chuàng)建用戶組:groupadd elasticsearch

  2. 創(chuàng)建用戶加入用戶組:useradd elasticsearch -g elasticsearch

  3. 設(shè)置ElasticSerach文件夾為用戶elasticsearch所有:

    chown -R elasticsearch.elasticsearch /opt/elasticsearch-6.5.4

修改系統(tǒng)環(huán)境
  1. 打開文件/etc/security/limits.conf,添加下面4處內(nèi)容:

    • soft nofile 65536

    • hard nofile 131072

    • soft nproc 2048

    • hard nproc 4096

  2. 打開文件/etc/sysctl.conf,添加下面內(nèi)容:vm.max_map_count=65536

  3. 加載sysctl配置,執(zhí)行命令:sysctl -p

  4. 重啟電腦,執(zhí)行命令:reboot

啟動ES
  1. 切換到用戶elasticsearch:su elasticsearch

  2. 進入目錄/opt/elasticsearch-6.5.4

  3. 執(zhí)行啟動命令:bin/elasticsearch -d,此時會在后臺啟動elasticsearch(如果啟動報錯沒有權(quán)限的話,重新執(zhí)行上面的chown那部分命令設(shè)置權(quán)限)

  4. 查看啟動日志可執(zhí)行命令:tail -f /opt/elasticsearch-6.5.4/logs/elasticsearch.log

  5. 執(zhí)行curl命令檢查服務是否正常響應:curl 127.0.0.1:9200,如果成功會有Json報文返回

注:6版本內(nèi)核的linux系統(tǒng)啟動es中會報錯,可以通過修改config/elasticsearch.yml來糾正,在文件末尾添加

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n173" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">bootstrap.memory_lock: false
bootstrap.system_call_filter: false</pre>

配置啟動Logstash
  1. 在目錄/opt/elasticsearch-6.5.4下創(chuàng)建文件default.conf,內(nèi)容如下

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n190" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"># 監(jiān)聽5044端口作為輸入
    input {
    beats {
    port => "5044"
    }
    }

    數(shù)據(jù)過濾

    filter {
    grok {
    match => {
    "message" => "%{COMBINEDAPACHELOG}"
    }
    }
    geoip {
    source => "clientip"
    }
    }

    輸出配置為本機的9200端口,這是ElasticSerach服務的監(jiān)聽端口

    output {
    elasticsearch {
    hosts => ["127.0.0.1:9200"]
    }
    }</pre>

  2. 后臺啟動Logstash服務nohup bin/logstash -f default.conf –config.reload.automatic &

  3. 查看啟動日志:tail -f nohup.out,看到successfully就表明啟動成功

配置啟動FileBeat
  1. 修改配置文件filebeat.yml,主要修改兩個部分:日志存放位置以及輸出

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n225" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">- type: log

    Change to true to enable this prospector configuration.

    enabled: True

    Paths that should be crawled and fetched. Glob based paths.

    讀取 Nginx 的日志

    paths:

    • /var/logs/*.log

    輸出到本機的 LogStash

    output.logstash:

    The Logstash hosts

    hosts: ["localhost:5044"]
    ?</pre>

  2. 配置完成后,啟動Filebeat

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n231" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"># FileBeat 需要以 root 身份啟動,因此先更改配置文件的權(quán)限
    sudo chown root filebeat.yml
    sudo ./filebeat -e -c filebeat.yml -d "publish"</pre>

配置啟動Kibana
  1. 進入Kibana的目錄:/opt/kibana-6.5.4-linux-x86_64

  2. 修改配置文件config/kibana.yml,修改服務器主機名為相應的ip或者域名

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n249" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">server.host: "192.168.21.128"</pre>

  3. 執(zhí)行啟動命令:nohup bin/kibana &

  4. 查看啟動日志:tail -f nohup.out

  5. 在瀏覽器訪問http://[ip]:5601

漢化Kibana
  1. 下載

    [漢化包] https://github.com/anbai-inc/Kibana_Hanization

  2. 上傳至服務器然后解壓unzip kibana-hanhua.zip

  3. 開始漢化,注意python的版本不能過高,3.6的版本我試過是漢化不了的,換成2.7的可以了

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" contenteditable="true" cid="n285" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">python main.py /home/kibana-6.2.3/</pre>

最后啟動Kibana就可以看到漢化的界面

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容