一、概述
ELK 已經(jīng)成為目前最流行的集中式日志解決方案煞抬,它主要是由Logstash霜大、Elasticsearch、Kibana三個組件組成革答,來共同完成實時日志的收集战坤,存儲,展示等一站式的解決方案残拐,本文主要講ELK最常用的部署架構(gòu)的組件安裝途茫,以及安裝過程中的問題解決,后續(xù)文章會詳細(xì)介紹ELK的常用架構(gòu)溪食,以及在生產(chǎn)環(huán)境中相關(guān)問題的解決方法囊卜。
二、安裝部署
注:這里安裝的ELK組件版本為5.6错沃,需要JDK1.8及以上版本支持栅组。
1、創(chuàng)建ELK用戶
# groupadd elk //創(chuàng)建用戶組
# useradd -g elk elk //創(chuàng)建用戶枢析,并指定用戶組
# passwd elk //修改用戶密碼
# chown -R elk [ELK組件的安裝目錄] //給用戶elk分配目錄權(quán)限
2玉掸、從官網(wǎng)分別下載ELK組件
filebeat-5.6.3-linux-x86_64.tar.gz
logstash-5.6.3.tar.gz
elasticsearch-5.6.3.tar.gz
elasticsearch-head-master.zip
kibana-5.6.3-linux-x86_64.tar.gz
官網(wǎng)地址:https://www.elastic.co/cn/products
3、安裝Elasticsearch
# tar -zxvf elasticsearch-5.6.3.tar.gz
3.1 啟動
# ./bin/elasticsearch
3.2 Elasticsearch啟動相關(guān)問題解決
(1)啟動Elasticsearch5.6時報:can not run elasticsearch as root 錯誤登疗!這里一般以rpm方式安裝的話不存在該問題排截,如果安裝的是tag.gz包,就會報該錯誤辐益,解決辦法断傲,新建一個用戶即可。
(2)啟動Elasticsearch5.6報:process likely too low, increase to at least 錯誤智政,原因是新建的用戶默認(rèn)的可創(chuàng)建最大進(jìn)程數(shù)被限制认罩,解決方法:
- 切換到root用戶
- vi /etc/security/limits.conf
添加如下內(nèi)容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
參數(shù)解釋:
soft nproc: 單個用戶可用的最大進(jìn)程數(shù)量(軟限制)
hard nproc: 單個用戶可用的最大進(jìn)程數(shù)量(硬限制)
soft nofile:單個用戶可打開的最大文件描述符數(shù)量(軟限制)
hard nofile:單個用戶可打開的最大文件描述符數(shù)量(硬限制)
(3)Elasticsearch5.6啟動后9200端口無法被訪問,請修改config/elasticsearch.yml 文件续捂,修改network.host項:network.host: localhost垦垂,或填寫本機(jī)真實的IP
4、安裝Elasticsearch-head-master
4.1 安裝該插件后可在web頁面查看Elasticsearch的相關(guān)信息牙瓢,由于該插件運(yùn)行的環(huán)境要依賴nodejs劫拗,所以需要先安裝nodejs
(1)下載node-v8.9.0-linux-x64.tar.xz軟件包
(2)$ xz -d node-v8.9.0-linux-x64.tar.xz //解壓軟件包
(3)$ tar -xvf node-v8.9.0-linux-x64.tar
(4)建立node與npm命令的軟鏈接,這樣可在任何地方訪問該命令了
$ ln -s /home/project/elk/node-v8.9.0-linux-x64/bin/npm /usr/local/bin/npm
$ ln -s /home/project/elk/node-v8.9.0-linux-x64/bin/node /usr/local/bin/node
(5)驗證是否安裝成功:$ node -v
4.2 下載elasticsearch-head包:https://github.com/mobz/elasticsearch-head
(1)解壓下載好的包
(2)$ cd elasticsearch-head-master/
(3)$ npm install -g cnpm --registry=https://registry.npm.taobao.org //安裝依賴包
(4)安裝好后啟動該插件矾克,$ npm run start
(5)該插件默認(rèn)端口為9100页慷,打開后發(fā)現(xiàn)無法連接到Elasticsearch,在Elasticsearch版本5以上需要在Elasticsearch配置文件中加如下參數(shù)即可:
http.cors.enabled: true
http.cors.allow-origin: "*"
5胁附、安裝Logstash
# tar -zxvf logstash-5.6.3.tar.gz
5.1 Logstash相關(guān)命令
(1)測試啟動酒繁,看配置文件是否正確:bin/logstash -f first-pipeline.conf --config.test_and_exit
(2)以config.reload.automatic方式啟動,這樣在修改配置文件后無需重新啟動控妻,它會自動加載:bin/logstash -f first-pipeline.conf --config.reload.automatic
(3)列出所有已安裝的插件:bin/logstash-plugin list
(4)安裝外部的插件:bin/logstash-plugin install [插件名稱]
(5)更新所有插件:bin/logstash-plugin update
(6)更新指定插件:bin/logstash-plugin update logstash-output-kafka
(7)刪除指定插件:bin/logstash-plugin remove logstash-output-kafka
6州袒、安裝Filebeat
# tar -zxvf filebeat-5.6.3-linux-x86_64.tar.gz
(1)啟動:./filebeat -e -c filebeat.yml -d "publish"
(2)Filebeat的輸出方向為Elasticsearch,默認(rèn)啟動Filebeat加載的ES索引模板是 filebeat.template.json弓候,如:
output.elasticsearch: hosts: ["localhost:9200"] template.name: "filebeat" template.path: "filebeat.template.json" template.overwrite: false
默認(rèn)情況下郎哭,如果一個模板已經(jīng)存在于索引中,那么它就不會被覆蓋菇存。要覆蓋現(xiàn)有的模板彰居,需要設(shè)置
template.overwrite: true
如:Filebeat的輸出方向為Logstash,則需手動指定加載的ES索引模板撰筷,手動載入ES模板命令:
url -XPUT 'http://localhost:9200/_template/filebeat' -d@/etc/filebeat/filebeat.template.json
刪除索引模板:curl -XDELETE 'http://localhost:9200/filebeat-*'
(3)啟用已經(jīng)構(gòu)建好的模塊陈惰,-modules項后可填寫多個模塊,第一次啟動需要設(shè)置-setup毕籽,之后無需-setup
./filebeat -e -modules=system,nginx,mysql -setup
注:命令參數(shù)解釋
- -E <setting>=<value> //覆蓋特定的配置設(shè)置抬闯,如:
./filebeat -c filebeat.yml -E name=mybeat
- -c <file> //指定配置文件的路徑
詳細(xì)參數(shù)請看:https://www.elastic.co/guide/en/beats/filebeat/current/command-line-options.html
(4)使Filebeat重新從指定日志文件的開頭讀取數(shù)據(jù)
- 先停掉Filebeat
-
rm data/registry
7、安裝Kibana
# tar kibana-5.6.3-linux-x86_64.tar.gz
7.1 問題解決
啟動kibana后在瀏覽器中無法訪問关筒,解決方法:修改kibana.yml文件溶握,修改server.host值 為 "localhost"
7.2 查詢技巧:
(1)要精確查詢字符串請使用雙引號,如查詢"hello, how are you"
(2)* 表示匹配0到多個字符蒸播,如 hello*
(3)? 表示匹配單個字符睡榆,如h?o
(4)+ 表示搜索結(jié)果中必須包含此項萍肆,- 表示搜索中不包含此項
(5)運(yùn)算符AND、OR胀屿、NOT表示與或非塘揣,注意必須大寫,如 hello AND you宿崭,表示查詢結(jié)果中必須同時包含hello與you
(6)@version:1 表示查詢@version字段值為1的結(jié)果項
(7)@version:[1 TO 10] 表示查詢@version的范圍在1到10的結(jié)果項
(8)轉(zhuǎn)義特殊字符 + – && || ! ( ) { } [ ] ^ ” ~ * ? : \ 轉(zhuǎn)義特殊字符只需在字符前加上符號\
8亲铡、配置
8.1 配置Logstash
新建logstash.conf配置文件,內(nèi)容為:
input {
beats {
port => 5044 //配置啟動端口為5044
}
}
output {
elasticsearch { //配置輸出目的地為elasticsearch
hosts => "localhost:9200"
}
}
8.2 配置Filebeat
新建filebeat-test.yml葡兑,內(nèi)容為:
filebeat.prospectors:
-
paths:
- /home/project/elk/logs/test.log //日志路徑
input_type: log
multiline: //日志多行內(nèi)容合并
pattern: '^\['
negate: true
match: after
document_type: test //文檔類型奖蔓,可用于索引的構(gòu)建
output:
logstash: //輸出目的地為logstash
hosts: ["localhost:5044"]
9、運(yùn)行
分別啟動:Elasticsearch -> Logstash -> Filebeat -> Kibana
三讹堤、總結(jié)
本文主要介紹了ELK相關(guān)組件的安裝吆鹤,和安裝過程中相關(guān)問題的解決,以及各組件的使用技巧洲守,這里建議多看看Kibana中的查詢技巧檀头,可以在生產(chǎn)環(huán)境根據(jù)日志來快速定位問題。