標籤:elk
一、簡介
1、組成
ELK由Elasticsearch、Logstash和Kibana三部分組件組成;
Elasticsearch是個開源分布式搜尋引擎,它的特點有:分布式,零配置,自動探索,索引自動分區,索引複本機制,restful風格介面,多資料來源,自動搜尋負載等。
Logstash是一個完全開源的工具,它可以對你的日誌進行收集、分析,並將其儲存供以後使用
kibana 是一個開源和免費的工具,它可以為 Logstash 和 Elasticsearch 提供的日誌分析友好的 Web 介面,可以協助您匯總、分析和搜尋重要資料日誌。
2、組件
Logstash: logstash server端用來搜集日誌;
Elasticsearch: 儲存各類日誌;
Kibana: web化介面用作查尋和可視化日誌;
Logstash Forwarder: logstash client端用來通過lumberjack 網路通訊協定發送日誌到logstash server;
3、工作流程
在需要收集日誌的所有服務上部署logstash,作為logstash_agent(logstash shipper)用於監控並過濾收集日誌,將過濾後的內容發送到Redis,然後logstash_server 將日誌收集在一起交給全文檢索搜尋服務Elasticsearch,可以用Elasticsearch進行自訂搜尋通過Kibana 來結合自訂搜尋進行頁面展示。
4、服務分布
主機A 192.168.0.100 Elasticsearch+Logstash-server+Kinaba+Redis主機B 192.168.0.101 Logstash-agent
二、開始部署服務
在主機B上面 192.168.0.101
部署java環境
#下載軟體包、解壓、設定環境變數
wget http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.tar.gztar -xf jdk-8u111-linux-x64.tar.gz -C /usr/localmv /usr/local/jdk-8u111-linux-x64 /usr/local/javaecho "export PATH=\$PATH:/usr/local/java/bin" > /etc/profile.d/java.sh. /etc/profile
2.部署Logstash-agent
wget https://download.elastic.co/logstash/logstash/logstash-2.2.0.tar.gztar -xf logstash-2.2.0.tar.gz -C /usr/localecho "export PATH=\$PATH:/usr/local/logstash-2.2.0/bin" > /etc/profile.d/logstash.sh. /etc/profile
3、logstash常用參數
-e :指定logstash的配置資訊,可以用於快速測試; -f :指定logstash的設定檔;可以用於生產環境;
4、啟動logstash
4.1 通過-e參數指定logstash的配置資訊,用於快速測試,直接輸出到螢幕。
#logstash -e "input {stdin{}} output {stdout{}}" my name is zhengyansheng. //手動輸入後斷行符號, 等待10秒後會有返回結果Logstash startup completed2015-10-08T13:55:50.660Z 0.0.0.0 my name is zhengyansheng.這種輸出是直接原封不動的返回...
4.2 通過-e參數指定logstash的配置資訊,用於快速測試,以json格式輸出到螢幕。
5.1 logstash輸出資訊儲存到redis資料庫中
將logstash的輸出資訊儲存到redis資料庫中,如下
前提是(192.168.0.100)有redis資料庫,那麼下一步我們就是安裝redis資料庫.
# cat logstash_agent.confinput { stdin { } }output { stdout { codec => rubydebug } redis { host => ‘192.168.0.100‘ port => ‘6379‘ password => ‘12345678‘ data_type => ‘list‘ key => ‘logstash:redis‘ }} 如果提示Failed to send event to Redis,表示串連Redis失敗或者沒有安裝,請檢查...
6、 查看logstash進程
# logstash agent -f logstash_agent.conf --verbose# ps -ef|grep logstash
在主機A上面 192.168.0.100
部署java環境(同上)
部署redis
wget http://download.redis.io/releases/redis-2.8.19.tar.gzyum install tcl -ytar zxf redis-2.8.19.tar.gzcd redis-2.8.19make MALLOC=libcmake test //這一步時間會稍久點...make install cd utils/./install_server.sh //指令碼執行後,所有選項都以預設參數為準即可Welcome to the redis service installerThis script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.confPlease select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.logPlease select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379Please select the redis executable path [/usr/local/bin/redis-server] Selected config:Port : 6379Config file : /etc/redis/6379.confLog file : /var/log/redis_6379.logData dir : /var/lib/redis/6379Executable : /usr/local/bin/redis-serverCli Executable : /usr/local/bin/redis-cliIs this ok? Then press ENTER to go on or Ctrl-C to abort.Copied /tmp/6379.conf => /etc/init.d/redis_6379Installing service...Successfully added to chkconfig!Successfully added to runlevels 345!Starting Redis server...Installation successful!
查看redis的監控連接埠
# netstat -tnlp |grep redistcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 3843/redis-server * tcp 0 0 127.0.0.1:21365 0.0.0.0:* LISTEN 2290/src/redis-serv tcp 0 0 :::6379 :::* LISTEN 3843/redi
測試redis是否正常工作
# cd redis-2.8.19/src/# ./redis-cli -h 192.168.0。100 -p 6379 //串連redis192.168.0.100:6379> pingPONG192.168.0.100:6379> set name zhengyanshengOK192.168.0.100:6379> get name"zhengyansheng"192.168.0.100:6379> quit 啟動redis/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
基於入口redis啟動logstash(在主機B上面啟動logstash-agent)
# cat logstash_agent.confinput { stdin { } }output { stdout { codec => rubydebug } redis { host => ‘192.168.0.100‘ data_type => ‘list‘ key => ‘logstash:redis‘ }}# logstash agent -f logstash_agent.conf --verbosePipeline started {:level=>:info}Logstash startup completeddajihao linux{ "message" => "dajihao linux", "@version" => "1", "@timestamp" => "2015-10-08T14:42:07.550Z", "host" => "0.0.0.0"}
在主機A上面安裝elasticsearch
1、安裝Elasticsearch
# wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-2.2.0.tar.gz# tar zxf elasticsearch-2.2.0.tar.gz -C /usr/local/
2、修改elasticsearch設定檔elasticsearch.yml並且做以下修改.
# vim /usr/local/elasticsearch-2.2.0/config/elasticsearch.ymldiscovery.zen.ping.multicast.enabled: false #關閉廣播,如果區域網路有機器開9300 連接埠,服務會啟動不了network.host: 192.168.0.100 #指定主機地址,其實是可選的,但是最好指定因為後面跟kibana整合的時候會報http串連出錯(直觀體現好像是監聽了:::9200 而不是0.0.0.0:9200)http.port: 9200
3、啟動elasticsearch服務
nohup /usr/local/elasticsearch-2.2.0/bin/elasticsearch >/usr/local/elasticsearch-2.2.0/nohub &
如果此種方式無法啟動請建立普通使用者es啟動
groupadd elkuseradd es -g elkchown -R es.elk /usr/local/elasticsearch-2.2.0su - esnohup /usr/local/elasticsearch-2.2.0/bin/elasticsearch >/usr/local/elasticsearch-2.2.0/nohub &
4、查看elasticsearch的監聽連接埠
# netstat -tnlp |grep javatcp 0 0 :::9200 :::* LISTEN 7407/java tcp 0 0 :::9300 :::* LISTEN 7407/java
在主機A上安裝logstash-server(同上)注意設定檔有所不同
cat logstash_server.conf input { redis { host => ‘192.168.0.100‘ port => ‘6379‘ password => ‘12345678‘ data_type => ‘list‘ key => ‘logstash:redis‘ type => "redis-input" }}output { elasticsearch { hosts => "192.168.0.100" index => "logstash-%{+YYYY.MM.dd}" }}啟動logstash-server (從redis中擷取資料並輸送到es中)logstash agent -f logstash_server.conf --verbose
安裝elasticsearch外掛程式
#Elasticsearch-kopf外掛程式可以查詢Elasticsearch中的資料,安裝elasticsearch-kopf,只要在你安裝Elasticsearch的目錄中執行以下命令即可:
# cd /usr/local/elasticsearch-2.2.0/bin/# ./plugin install lmenezes/elasticsearch-kopf-> Installing lmenezes/elasticsearch-kopf...Trying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip...Downloading .............................................................................................Installed lmenezes/elasticsearch-kopf into /usr/local/elasticsearch-2.2.0/plugins/kopf 執行外掛程式安裝後會提示失敗,很有可能是網路等情況...-> Installing lmenezes/elasticsearch-kopf...Trying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip...Failed to install lmenezes/elasticsearch-kopf, reason: failed to download out of all possible locations..., use --verbose to get detailed information 解決辦法就是手動下載該軟體,不通過外掛程式安裝命令...cd /usr/local/elasticsearch-2.2.0/pluginswget https://github.com/lmenezes/elasticsearch-kopf/archive/master.zipunzip master.zipmv elasticsearch-kopf-master kopf以上操作就完全等價於外掛程式的安裝命令
瀏覽器訪問kopf頁面訪問elasticsearch儲存的資料
在主機A上面安裝kibana
1、安裝Kinaba
# wget https://download.elastic.co/kibana/kibana/kibana-4.4.0-linux-x64.tar.gz# tar zxf kibana-4.4.0-linux-x64.tar.gz -C /usr/local
2、修改kinaba設定檔kinaba.yml
# vim /usr/local/kibana-4.4.0-linux-x64/config/kibana.ymlelasticsearch_url: "http://192.168.0.100:9200"
3、啟動kinaba
nohup /usr/local/kibana-4.4.0-linux-x64/bin/kibana > /usr/local/kibana-4.4.0-linux-x64/nohub.out &
輸出以下資訊,表明kinaba成功.
{"name":"Kibana","hostname":"localhost.localdomain","pid":1943,"level":30,"msg":"No existing kibana index found","time":"2015-10-08T00:39:21.617Z","v":0}
{"name":"Kibana","hostname":"localhost.localdomain","pid":1943,"level":30,"msg":"Listening on 0.0.0.0:5601","time":"2015-10-08T00:39:21.637Z","v":0}
kinaba預設監聽在本地的5601連接埠上
4、瀏覽器訪問kinaba
4.1 使用預設的logstash-*的索引名稱,並且是基於時間的,點擊“Create”即可。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/74/25/wKioL1YWFTbx56DuAAOecFQkxcA301.jpg" style="float:none;" title="01(首頁).png" alt="wKioL1YWFTbx56DuAAOecFQkxcA301.jpg" width="650" />
4.2 看到如下介面說明索引建立完成。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/74/27/wKiom1YWFR-hbgxOAAQZ85RcxMg067.jpg" style="float:none;" title="02(create).png" alt="wKiom1YWFR-hbgxOAAQZ85RcxMg067.jpg" width="650" />
4.3 點擊“Discover”,可以搜尋和瀏覽Elasticsearch中的資料。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/74/25/wKioL1YWFTaRWGZmAAQ81v02YE8823.jpg" style="float:none;" title="03(discover).png" alt="wKioL1YWFTaRWGZmAAQ81v02YE8823.jpg" width="650" />
>>>結束<<<
1、ELK預設連接埠號碼elasticsearch:9200 9300logstash : 9301kinaba : 5601
2、錯誤匯總
(1)java版本過低
[2015-10-07 18:39:18.071] WARN -- Concurrent: [DEPRECATED] Java 7 is deprecated, please use Java 8.
(2)Kibana提示Elasticsearch版本過低...
This version of Kibana requires Elasticsearch 2.0.0 or higher on all nodes. I found the following incompatible nodes in your cluster:
Elasticsearch v1.7.2 @ inet[/192.168.1.104:9200] (127.0.0.1)
解決辦法:
參考:http://467754239.blog.51cto.com/4878013/1700828
Elasticsearch+Logstash+Kinaba+Redis日誌分析系統