三分鐘搭建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)圖如下:
開始搭建
本手冊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)限禀梳,具體如下:
創(chuàng)建用戶組:
groupadd elasticsearch
創(chuàng)建用戶加入用戶組:
useradd elasticsearch -g elasticsearch
-
設(shè)置ElasticSerach文件夾為用戶elasticsearch所有:
chown -R elasticsearch.elasticsearch /opt/elasticsearch-6.5.4
修改系統(tǒng)環(huán)境
-
打開文件/etc/security/limits.conf杜窄,添加下面4處內(nèi)容:
soft nofile 65536
hard nofile 131072
soft nproc 2048
hard nproc 4096
打開文件/etc/sysctl.conf,添加下面內(nèi)容:
vm.max_map_count=65536
加載sysctl配置算途,執(zhí)行命令:
sysctl -p
重啟電腦塞耕,執(zhí)行命令:
reboot
啟動ES
切換到用戶elasticsearch:
su elasticsearch
進入目錄/opt/elasticsearch-6.5.4
執(zhí)行啟動命令:
bin/elasticsearch -d
,此時會在后臺啟動elasticsearch(如果啟動報錯沒有權(quán)限的話嘴瓤,重新執(zhí)行上面的chown那部分命令設(shè)置權(quán)限)查看啟動日志可執(zhí)行命令:
tail -f /opt/elasticsearch-6.5.4/logs/elasticsearch.log
執(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
-
在目錄/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> 后臺啟動Logstash服務(wù)
nohup bin/logstash -f default.conf –config.reload.automatic &
查看啟動日志:
tail -f nohup.out
,看到successfully
就表明啟動成功
配置啟動FileBeat
-
修改配置文件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> -
配置完成后驾讲,啟動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
進入Kibana的目錄:/opt/kibana-6.5.4-linux-x86_64
-
修改配置文件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>
執(zhí)行啟動命令:
nohup bin/kibana &
查看啟動日志:
tail -f nohup.out
在瀏覽器訪問http://[ip]:5601
漢化Kibana
-
下載
上傳至服務(wù)器然后解壓
unzip kibana-hanhua.zip
-
開始漢化吮铭,注意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就可以看到漢化的界面