2017年底花了一個(gè)月搭建ELK(Elasticsearch+Logstash+Kibana),2018年10月又做了一次服務(wù)器從阿里云到google的切換,因此搭建了兩次ELK系統(tǒng),現(xiàn)在數(shù)據(jù)量超過30億,今天有時(shí)間把搭建過程和關(guān)鍵點(diǎn)整理出來:

1. 環(huán)境準(zhǔn)備
1.1. 安裝JDK
google 服務(wù)器一臺(tái),需要安裝JDK1.8最新版本,可以參考之前的文章安裝JDK,并配置profile
最新JDK下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
1.2. 下載安裝Elasticsearch+Logstash+Kibana
最新版es下載地址:https://www.elastic.co/downloads/elasticsearch
es文檔地址:https://www.elastic.co/guide/index.html
Logstash和kibana的下載和文檔地址可以從es鏈接頁(yè)面中跳轉(zhuǎn)過去,一定要保證3個(gè)組件版本號(hào)相同即可
1.3. 服務(wù)器配置
根據(jù)使用近一年的經(jīng)驗(yàn)判斷,目前我使用的是8核Cpu,16G內(nèi)存,系統(tǒng)硬盤40G(基本不用于存放數(shù)據(jù),只放JDK文件),擴(kuò)展掛載1T硬盤
但是在導(dǎo)入數(shù)據(jù)的時(shí)候發(fā)現(xiàn)CPU在數(shù)據(jù)導(dǎo)入壓力測(cè)試時(shí)load average是> 8*2*0.75的(最好 < 8*2*0.7)說明CPU最好使用16核
es在jvm中分配了8G的容量,logstash分配了1G。logstash夠用,但是es建議分配16G,防止高并發(fā)查詢時(shí)gc報(bào)錯(cuò),但是不能高于32G(這是es的建議值)
關(guān)于數(shù)據(jù)盤的大?。耗壳?T夠用,es數(shù)據(jù)占用500G左右,另外有些臨時(shí)文件最高占用100G,最低占用60G(每10天壓縮打包一次)
2. 配置運(yùn)行elk
2.1. 運(yùn)行elasticsearch
./bin/elasticsearch 運(yùn)行如果報(bào)錯(cuò),是因?yàn)閑lasticsearch不能在root下運(yùn)行
解決辦法:
groupadd elsearch????????? #增加es組
useradd elsearch -g elsearch -p pwd????????? #增加es用戶并附加到es組
chown -R elsearch:elsearch elasticsearch????????? #給目錄權(quán)限
su es????????? #使用es用戶
./bin/elasticsearch -d ???????? #后臺(tái)運(yùn)行es
解決: file descriptor問題
vi /etc/security/limits.conf
# End of file
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536
修改config下的elasticsearch.yml配置文件:
cluster.name: gamebeer #集群名稱
node.name: node-1 #這臺(tái)機(jī)器的節(jié)點(diǎn)名稱
bootstrap.memory_lock: true #打開之后,不會(huì)與swap交換內(nèi)存
network.host: 0.0.0.0 #監(jiān)聽的ip
http.port: 52125 #http端口號(hào),默認(rèn)9200,我改成了52125
在jvm.options文件中修改heap size,es建議是機(jī)器物理內(nèi)存的1/2:
-Xms8g
-Xmx8g
如果用elastic用戶運(yùn)行elastic程序沒有報(bào)錯(cuò),則表明能夠運(yùn)行成功。
測(cè)試url接口訪問:瀏覽器輸入35.xxx.xxx.xxx:52125 web能訪問且命令行得到結(jié)果即為運(yùn)行測(cè)試通過,也可以在命令行輸入curl測(cè)試命令:
root@elastic:~# curl -X GET 'http://localhost:52125'
{
? "name" : "KRs-AjM",
? "cluster_name" : "elasticsearch",
? "cluster_uuid" : "CmiON8GeQkibisuuidTL1w",
? "version" : {
? ? "number" : "5.6.2",
? ? "build_hash" : "57e20f3",
? ? "build_date" : "2017-09-23T13:16:45.703Z",
? ? "build_snapshot" : false,
? ? "lucene_version" : "6.6.1"
? },
? "tagline" : "You Know, for Search"
}
2.2. 運(yùn)行l(wèi)ogstash
首先寫一個(gè)logstash的配置文件,例如jsonToEs.conf,這個(gè)文件告訴logstash從哪里讀數(shù)據(jù),并寫入到哪里。我這里是從一個(gè)文件中讀數(shù)據(jù),寫入到es
input{
? ? file{
? ? ? ? path => ["/bigdata/data/srvOutput/jsonToEs.json"]
? ? ? ? codec => "json"
? ? ? ? #delimiter => "\n"
? ? ? ? start_position => "end"
? ? ? ? #max_open_files => 2048
? ? }
}
filter {
? ? json {
? ? ? ? source => "message"
? ? }
? ? date {
? ? ? ? match => [ "time", "UNIX" ]
? ? ? ? #target => "date"
? ? ? ? remove_field => [ "time" ]
? ? ? ? remove_field => [ "Time" ]
? ? }
#? ? date {? ?
#? ? ? match => ["time","UNIX"]? ?
#? ? ? target => "@timestamp"
#? ? ? remove_field => [ "time" ]
#? ? }
#? ? ruby {? ?
#? ? ? code => "event.set('timestamp', event.get('@timestamp').time.localtime + 8*60*60)"?
#? ? }
#? ? ruby {?
#? ? ? code => "event.set('@timestamp',event.get('timestamp'))"
#? ? }
#? ? mutate {?
#? ? ? remove_field => ["timestamp"]
#? ? }
}
output{
? ? elasticsearch {
? ? ? ? hosts => ["127.0.0.1:52125"]
? ? ? ? index => "server-%{+YYYY.MM.dd}"
? ? ? ? #index => "test-%{+YYYY.MM.DD}"
? ? }
#? ? stdout {
#? ? ? codec => rubydebug
#? ? }
}
啟動(dòng)logstash,啟動(dòng)時(shí)間大約是半分鐘:
./bin/logstash -f ./conf/jsonToEs.conf
2.3. 運(yùn)行kibana
修改config下的kibana.yml配置文件:
server.port: 52025 #kibanaweb訪問的端口號(hào)
server.host: 0.0.0.0 #kibana偵聽的ip
elasticsearch.url: "http://localhost:52125" #kibanna綁定es的url
elasticsearch.requestTimeout: 60000 #kibana查詢等待的超時(shí)時(shí)間,我把30s改成了60s
運(yùn)行kibana:
./bin/kibana
3. 其他
3.1. 清除es elasticsearch中的數(shù)據(jù)(慎用,刪除后無法恢復(fù)):
curl -XDELETE "http://localhost:52125/index-*"
3.2. elk總結(jié):
1. 如果遇到問題只能找很多日志文件,在shell下面awk分析,耗時(shí)很長(zhǎng)。如果Elasticsearch很短時(shí)間就可以搞定
2. 統(tǒng)計(jì)表、kibana、可視化,即席查詢
3. 實(shí)時(shí)性強(qiáng),根據(jù)數(shù)據(jù)量和性能決定能查詢到過去的多少時(shí)間
4. 模糊查詢,根據(jù)查詢?cè)~組得出score匹配度。適用于電商網(wǎng)站,搜索框
5. 目前用于查詢禮包id,帳號(hào)、訂單等基本查詢。優(yōu)勢(shì)是免費(fèi)
6. 分布式:硬件分布式做主副分片備份、分布式查詢減少服務(wù)器負(fù)載
3.3. 查詢elk的pid:
# jps
2983 Elasticsearch? ?#這是elasticsearch的pid
18824 Jps? ? #jps臨時(shí)pid
3256 Logstash? ? #logstash的pid
# ps -aux|grep node
3299就是kibana的pid
root? ? ? 3299? 0.4? 1.2 1354432 201672 ?? ? ? Sl? Oct15? 52:12 /bigdata/kibana-6.4.2-linux-x86_64/bin/../node/bin/node --no-warnings /bigdata/kibana-6.4.2-linux-x86_64/bin/../src/cli
root? ? 18866? 0.0? 0.0? 12944? 960 pts/4? ? R+? 20:55? 0:00 grep --color=auto node
3.4.?kibana 安全控制訪問
xxx.xxx.xxx.xxx
查看iptables當(dāng)前規(guī)則:
iptables -nL --line-number
添加規(guī)則:
iptables -I INPUT -p tcp --dport 52025 -j DROP
iptables -I INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 52025 -j ACCEPT
iptables -I INPUT -s 127.0.0.1 -p tcp --dport 52025 -j ACCEPT
iptables -I INPUT -p tcp --dport 52125 -j DROP
iptables -I INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 52125 -j ACCEPT
iptables -I INPUT -s 127.0.0.1 -p tcp --dport 52125 -j ACCEPT
刪除規(guī)則:
iptables -D INPUT -p tcp --dport 52025 -j DROP
iptables -D INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 52025 -j ACCEPT
iptables -D INPUT -s 127.0.0.1 -p tcp --dport 52025 -j ACCEPT
iptables -D INPUT -p tcp --dport 52125 -j DROP
iptables -D INPUT -s xxx.xxx.xxx.xxx -p tcp --dport 52125 -j ACCEPT
iptables -D INPUT -s 127.0.0.1 -p tcp --dport 52125 -j ACCEPT
3.5. 更改端口需要更改三個(gè)地方:
kibanba配置文件
logstash的配置文件
es的配置文件
3.6.?開關(guān)index
curl?-XPOST?"http://localhost:52125/server-2018.04.30/_close"
curl?-XPOST?"http://localhost:52125/server-2018.04.30/_open"
3.7. 查看es狀態(tài)
查看一個(gè)node:
GET /_cat/nodes?v&h=name,port,sm,hp
kibana查看一個(gè)索引:
GET /_cat/segments/server-2018.04.30?v&h=shard,segment,size,size.memory
查看集群運(yùn)行狀態(tài)所有接口:
https://blog.csdn.net/u013673976/article/details/73556650
集群重啟:
http://www.itdecent.cn/p/7b010806c58e
清緩存:
https://doc.yonyoucloud.com/doc/mastering-elasticsearch/chapter-5/54_README.html