1 軟件介紹
1.1 Elasticsearch
Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data so you can discover the expected and uncover the unexpected.
Elasticsearch是一個(gè)分布式的RESTful搜索和分析引擎蓬推,能夠解決越來(lái)越多的用例茶宵。 作為彈性堆棧的核心卡乾,它集中存儲(chǔ)您的數(shù)據(jù),所以你能夠自主的操作您的數(shù)據(jù)。
大概意思是說(shuō)桃笙,ES既可以為用戶提供數(shù)據(jù)存儲(chǔ)彻采,有能為用戶提供用戶接口以作數(shù)據(jù)分析。
官網(wǎng):https://www.elastic.co/products/elasticsearch
1.2 Logstash
Logstash is an open source, server-side data processing pipeline that ingests data from a multitude of sources simultaneously, transforms it, and then sends it to your favorite “stash.” (Ours is Elasticsearch, naturally.)
Logstash是一個(gè)開源的服務(wù)器端數(shù)據(jù)處理管道瘫拣,它從多個(gè)源同時(shí)獲取數(shù)據(jù)亿絮,對(duì)其進(jìn)行轉(zhuǎn)換,然后將其發(fā)送到您所想要的“存儲(chǔ)”(這里主要是ES)麸拄。
大概意思是說(shuō)派昧,Logstash是一個(gè)不局限于數(shù)據(jù)源和目的源的數(shù)據(jù)管道工具,它提供了各種數(shù)據(jù)源插件拢切,以供用戶搜集蒂萎、轉(zhuǎn)換不同的日志。
官網(wǎng):https://www.elastic.co/products/logstash
1.3 Kibana
Kibana lets you visualize your Elasticsearch data and navigate the Elastic Stack, so you can do anything from learning why you're getting paged at 2:00 a.m. to understanding the impact rain might have on your quarterly numbers.
Kibana讓你可視化你的Elasticsearch數(shù)據(jù)和導(dǎo)航彈性堆棧淮椰。讓您清晰的了解到您的但不局限于系統(tǒng)日志等信息的操作五慈。
官網(wǎng):https://www.elastic.co/products/kibana
2 環(huán)境搭建
2.1 軟件環(huán)境要求
系統(tǒng):CentOS 6.5 x86_64
Java:JDK 1.8_64bit
Nodejs:v6.10.0
軟件:Elasticsearch-5.2.2、Logstash-5.2.2主穗、Kibana-5.2.2-x86_64
2.2 Elasticsearch安裝
說(shuō)明:新版ES必須要使用普通用戶啟動(dòng)泻拦,不能使用root用戶。
1)下載ES安裝包
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.tar.gz
~]#sudo tar -xvzf elasticsearch-5.2.2.tar.gz
2)修改ES的配置文件
編輯config下的elasticsearch.yml
cluster.name: elk-sdnware
node.name: elk-node1
node.attr.rack: r1
path.data: /home/esuser/es/data
path.logs: /home/esuser/es/logs
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 192.168.200.201
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.200.201", "[::1]"]
discovery.zen.minimum_master_nodes: 1
gateway.recover_after_nodes: 1
http.cors.enabled: true
http.cors.allow-origin: "*"
表1 - ES配置參數(shù)說(shuō)明
參數(shù)含義
cluster.name????集群名稱
node.name????節(jié)點(diǎn)名稱
node.attr.rack????默認(rèn)參數(shù)描述
path.dataES????數(shù)據(jù)文件
path.logsES????日志文件路徑
bootstrap.memory_lock????是否鎖定使用內(nèi)存忽媒,為了不產(chǎn)生內(nèi)存交換以保證性能(CentOS7以下不支持SecComp)
bootstrap.system_call_filter????同上
network.host????網(wǎng)絡(luò)主機(jī)
http.port????HTTP端口
discovery.zen.ping.unicast.hosts????初始節(jié)點(diǎn)列表
discovery.zen.minimum_master_nodes????集群至少有n個(gè)master
gateway.recover_after_nodes????n個(gè)節(jié)點(diǎn)啟動(dòng)后開始數(shù)據(jù)恢復(fù)
http.cors.enabled????是否支持跨域
http.cors.allow-origin????當(dāng)設(shè)置允許跨域聪轿,默認(rèn)為*,表示支持所有域名,如果我們只是允許某些網(wǎng)站能訪問猾浦,那么可以使用正則表達(dá)式陆错。比如只允許本地地址。?/https?:\/\/localhost(:[0-9]+)?/
3)啟動(dòng)ES
~/bin/elasticsearch >/dev/null 2>&1 &
啟動(dòng)成功后訪問http://192.168.200.201:9200金赦,如下圖:
4)安裝Elasticsearch-head插件
該插件是為了方便簡(jiǎn)單的管理ES中的索引音瓷、集群等信息,新版head插件安裝與老版ES有所差異夹抗,官方已經(jīng)獨(dú)立出來(lái)成為了一個(gè)插件庫(kù)绳慎,需要獨(dú)立安裝。
a)下載head插件安裝包
git clone git://github.com/mobz/elasticsearch-head.git
b)下載安裝nodejs
wget?https://nodejs.org/dist/v6.10.0/node-v6.10.0-linux-x64.tar.xz
tar -xvf node-v6.10.0-linux-x64.tar.xz
設(shè)置NODE環(huán)境變量漠烧,node -v杏愤、npm -v測(cè)試安裝是否成功
c)進(jìn)入剛下載的elasticsearch-head目錄,執(zhí)行npm install
~/elasticsearch-head/ ] # npm install
在執(zhí)行安裝head插件時(shí)會(huì)去下載phantomjs已脓,網(wǎng)絡(luò)不好情況下下載極慢而導(dǎo)致npm install執(zhí)行失敗珊楼,為了保證正常安裝,建議下載好該安裝包度液,默認(rèn)放在/tmp/phantomjs/,當(dāng)前版本使用的是phantomjs-2.1.1-linux-x86_64.tar.bz2厕宗,具體版本看安裝時(shí)打印信息画舌;
d)安裝完成后,需要修改相應(yīng)的web文件
i.修改服務(wù)器監(jiān)聽地址
目錄:head/Gruntfile.js
connect: {
????server: {
????????options: {
????????????port: 9100,
????????????hostname: '*',
????????????base: '.',
????????????keepalive: true
????????}
????}
}
增加hostname屬性已慢,設(shè)置為*
ii.修改連接地址
目錄:head/_site/app.js
修改head的連接地址:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
把localhost修改成你es的服務(wù)器地址曲聂,如:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.200.201:9200";
e)啟動(dòng)head服務(wù)
elasticsearch-head ] # node_modules/grunt/bin/grunt server &
在此之前保證ES已經(jīng)啟動(dòng)成功
打開http://192.168.200.201:9100,如下圖:
2.3 Logstash安裝
1)下載Logstash安裝包
https://artifacts.elastic.co/downloads/logstash/logstash-5.2.2.tar.gz
~]#sudo tar -xvzflogstash-5.2.2.tar.gz
2)添加Logstash的用戶(agent)配置
vim log4jes.conf佑惠,內(nèi)容如下:
input {
????log4j {
????????mode => "server"
????????host => "192.168.200.201"
????????port => 4567
????}
}
filter {
#Only matched data are send to output.
}
output {
????elasticsearch {
????????action => "index"
????????hosts ?=> ["192.168.200.201:9200"]
????????index ?=> "cloudap-%{+YYYY.MM.dd}"
????}
}
3)模擬Log4j的java應(yīng)用
a)創(chuàng)建一個(gè)maven項(xiàng)目朋腋,結(jié)構(gòu)如下:
b)編輯Log4jDemo.java文件
c)修改log4j.properties配置文件
4)啟動(dòng)Logstash服務(wù)
logstash-5.2.2 ] # bin/logstash -f log4jes.conf>/dev/null 2>&1 &
啟動(dòng)成功后,運(yùn)行java應(yīng)用膜楷,然后打開elasticsearch-head旭咽,如圖:
會(huì)看到有cloudap-2017.03.08的索引,切換數(shù)據(jù)瀏覽把将,可以查看到詳細(xì)的數(shù)據(jù)信息:
2.4 Kibana安裝
1)下載Kibana安裝包
https://artifacts.elastic.co/downloads/kibana/kibana-5.2.2-linux-x86_64.tar.gz
~]#sudo tar -xvzf kibana-5.2.2-linux-x86_64.tar.gz
2)修改Kibana配置
kibana-5.2.2 ] # vim config/kibana.yml
server.port: 5601
server.host: “192.168.200.201”
elasticsearch.url: http://192.168.200.201:9200
kibana.index: “.kibana”
3)啟動(dòng)Kibana
kibana-5.2.2 ] #bin/kibana>/dev/null 2>&1 &
打開瀏覽器輸入:http://192.168.200.201:5601
初始進(jìn)入是轻专,需要自行添加索引忆矛,如圖:
創(chuàng)建成功察蹲,后便可以針對(duì)索引進(jìn)行數(shù)據(jù)的查看了。