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

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

初步認(rèn)識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機中芬迄。

安裝開始

解壓縮

首先安裝解壓縮到一個目錄下面问顷,個人習(xí)慣放在/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命令檢查服務(wù)是否正常響應(yīng):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服務(wù)的監(jiān)聽端口

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

  2. 后臺啟動Logstash服務(wù)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席赂,修改服務(wù)器主機名為相應(yīng)的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. 上傳至服務(wù)器然后解壓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)系作者
  • 序言:七十年代末沐兵,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子便监,更是在濱河造成了極大的恐慌扎谎,老刑警劉巖碳想,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異毁靶,居然都是意外死亡胧奔,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門预吆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來龙填,“玉大人,你說我怎么就攤上這事拐叉⊙乙牛” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵凤瘦,是天一觀的道長宿礁。 經(jīng)常有香客問我,道長蔬芥,這世上最難降的妖魔是什么梆靖? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮笔诵,結(jié)果婚禮上返吻,老公的妹妹穿的比我還像新娘。我一直安慰自己乎婿,他們只是感情好测僵,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著谢翎,像睡著了一般捍靠。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上岳服,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機與錄音希俩,去河邊找鬼吊宋。 笑死,一個胖子當(dāng)著我的面吹牛颜武,可吹牛的內(nèi)容都是我干的璃搜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鳞上,長吁一口氣:“原來是場噩夢啊……” “哼这吻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起篙议,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤唾糯,失蹤者是張志新(化名)和其女友劉穎怠硼,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體移怯,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡香璃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了舟误。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片葡秒。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖嵌溢,靈堂內(nèi)的尸體忽然破棺而出眯牧,到底是詐尸還是另有隱情,我是刑警寧澤赖草,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布学少,位于F島的核電站,受9級特大地震影響疚顷,放射性物質(zhì)發(fā)生泄漏旱易。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一腿堤、第九天 我趴在偏房一處隱蔽的房頂上張望阀坏。 院中可真熱鬧徙赢,春花似錦者填、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽士修。三九已至,卻和暖如春樱衷,著一層夾襖步出監(jiān)牢的瞬間棋嘲,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工矩桂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留沸移,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓侄榴,卻偏偏與公主長得像雹锣,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子癞蚕,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容