CentOS7使用Elasticsearch+ Logstash+kibana快速搭建日誌分析平臺
CentOS7使用Elasticsearch+ Logstash+kibana快速搭建日誌分析平臺
介紹:
安裝logstash,elasticsearch,kibana三件套,搜尋程式一般由索引鏈及搜尋元件組成。
索引鏈功能的實現需要按照幾個獨立的步驟依次完成:檢索原始內容、根據原始內容來建立對應的文件、對建立的文件進行索引。
搜尋元件用於接收使用者的查詢請求並返回相應結果,一般由使用者介面、構建可程式設計查詢語句的方法、查詢語句執行引擎及結果展示元件組成。
Elasticsearch是個開源分散式搜尋引擎,它的特點有:分散式,零配置,自動發現,索引自動分片,索引副本機制, restful 風格介面,多資料來源,自動搜尋負載等。
Logstash 是一個完全開源的工具,他可以對你的日誌進行收集、分析,並將其儲存供以後使用(如,搜尋)。
kibana也是一個開源和免費的工具,他Kibana 可以為 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 介面,可以幫助您彙總、分析和搜尋重要資料日誌
一、安裝elastic:
1、安裝java:
[[email protected] elasticsearch-5.6.3]# yum install -y *jdk*
2、配置訪問最大檔案數:
[[email protected] src]# cat/etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
2、在解壓目錄下建立,log,data檔案目錄:
[[email protected]]# mkdir data
[[email protected]]# mkdir log
[[email protected]]# mkdir logs ##有時候沒有這個目錄就自己手動建立
3、修改elastic.yaml:監聽主機
[[email protected] elasticsearch-5.6.3]# vimconfig/elasticsearch.yml
node.name: cml3
network.host: 192.168.5.104
discovery.zen.minimum_master_nodes: 1 ##因為我這裡演示的是在一臺機器上安裝(所以節點改為1)
4、建立elastic使用者用來開啟elasticsearch
[[email protected] elasticsearch-5.6.3]# useraddelastic
[[email protected] elasticsearch-5.6.3]# ls
binconfig data libLICENSE.txt log logsmodules NOTICE.txt pluginsREADME.textile
##有時候會小了目錄自己手動建立上就可以了:
[[email protected] logs]# ll
total 4
-rw-rw-r-- 1 elastic elastic 0 Nov3 19:52 elasticsearch_deprecation.log
-rw-rw-r-- 1 elastic elastic 0 Nov3 19:52 elasticsearch_index_indexing_slowlog.log
-rw-rw-r-- 1 elastic elastic 0 Nov3 19:52 elasticsearch_index_search_slowlog.log
##因為5.0的版本為了安全不能使用root使用者啟動所以給elasticsearch-5.6.3目錄下elastic(可以自定義使用者)的使用者許可權
##開啟haed:外掛:
http.cors.enabled:true
http.cors.allow-origin:"*"
##開啟的時候會有以下錯誤:
OpenJDK64-BitServerVMwarning:Ifthenumberofprocessorsisexpectedtoincreasefromone,thenyoushouldconf...CThreads=N
OpenJDK64-BitServerVMwarning:INFO:os::commit_memory(0x0000000085330000,2060255232,0)failed;error='Cannota...'(errno=12)
##解決方法:
意思是記憶體不夠,加大記憶體重啟就可以了!
##有時候還會出現以下錯誤:
[[email protected] bin]$ ./elasticsearch
Exception in thread "main"2017-11-03 20:38:47,194 main ERROR No log4j2 configuration file found. Usingdefault configuration: logging only errors to the console. Set system property'log4j2.debug' to show Log4j2 internal initialization logging.
2017-11-03 20:38:47,646 main ERROR Couldnot register mbeans java.security.AccessControlException: access denied("javax.management.MBeanTrustPermission" "register")
##解決方法:直接安裝幾個log4j包就ok啦
[[email protected] elasticsearch-5.6.3]# yuminstall -y log4j*
5、啟動elastic:直接用curl訪問9200埠成功即可
[[email protected] elasticsearch-5.6.3]# netstat-ntlp
Active Internet connections (only servers)
tcp60 0 192.168.5.104:9200 :::* LISTEN 3749/java
tcp60 0 192.168.5.104:9300 :::* LISTEN 3749/java
二、安裝head外掛:
1、介紹:
在以往的es版本有一個非常好用的外掛叫head,可以方便的檢視索引,叢集等相關狀態:
5.0之後head安裝支援目前只是支援外掛的方式:
2、建立存放這個外掛的目錄:
#mkdir head ##隨意目錄下建立就行
#cd head
#git clonegit://github.com/mobz/elasticsearch-head.git
#cd elasticsearch-head
yum install -y npm* ##缺少npm就直接安裝就ok了
#npm install ##這個步驟下載包都是國外地址所以安裝起來比較慢
#npm install -g grunt-cli
#grunt server
3、監聽埠為localhost,可以修改配置檔案:
[[email protected] head]# cdelasticsearch-head/
[[email protected] elasticsearch-head]#vim Gruntfile.js
做如下修改:
server: {
options: {
port: 9100,
hostname: '0.0.0.0', #####新增這句。
base: '.',
keepalive: true
}
}
}
修改es配置檔案新增如下:
[[email protected]]# vim elasticsearch.yml
http.cors.enabled:true
http.cors.allow-origin:"*"
#####然後重啟es服務
4、啟動head外掛,然後訪問web的ip與埠即可:
#gruntserver
##就可以看到我們相關索引的狀態,分片叢集等相關操作都可以在上面完成:
三、安裝配置logstash檔案然後啟動:
1、下載解壓logstash:
下載地址: https://artifacts.elastic.co/downloads/logstash/logstash-5.6.3.tar.gz
[[email protected] src]# tar -xflogstash-5.6.3.tar.gz
2、建立conf的目錄
[[email protected] ~]# mkdir /logstash/
[[email protected] logstash]# viminput_flter_output.conf
input {
file{
path=> "/usr/local/nginx/logs/cml.log" ##nginx生成日誌的目錄
type=> "cml" ##索引的型別
start_position=> "beginning" ##一開始就輸入原來的日誌資訊
}
stdin{}
}
filter{
grok {
match => {
"message" =>"(?<remote_IP>\d+.\d+.\d+.\d+)\s-\s-\s\[(?<DATA>\d+/\w+/\d+:\d+:\d+:\d+)[[:space:]](?<time_zone>\+\d+)\]\s\"(?<action>\w+)%{URIPATHPARAM:request} (?<Version>\w+/\d+.\d+)\"\s(?<status>\w+)\s(?<web_size>\w+)\s\"(?<check>\S+)\"\s"
##自定義grok
}
}
}
output{
elasticsearch{
action=> "index"
hosts=> "192.168.5.104:9200"##輸出到elasticsearch上面
index=> "log-%{+yyyy.MM.dd}" ##生成一個log-時間的索引
}
stdout {codec=>rubydebug} ##在終端上輸出方便檢測
}
[[email protected] logstash]# /usr/local/src/logstash-5.6.3/bin/logstash-f input_flter_output.conf
四、安裝kibana,然後修改監聽ip直接啟動即可
1、下載安裝kibana:
[[email protected] logstash]# cd /usr/local/src/
[[email protected] src]# wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.3-x86_64.rpm
[[email protected] src]# rpm -ivhkibana-5.6.3-x86_64.rpm
2、修改kibana配置檔案:
[[email protected] config]# vim kibana.yml
server.host: "192.168.63.246"
elasticsearch.url: http://192.168.63.246:9200
elasticsearch.username: "elastic"
elasticsearch.password: "changeme ##x-pack預設超級使用者的登入密碼(es和kibana兩個的超級管理員賬號密碼都一樣,這裡我就為了方便不使用密碼使用者登入了。)
3、啟動kibana
[[email protected] src]# systemctl start kibana
瀏覽器輸入http://IP:5601配置input_flter_output.conf檔案定義的索引。(圖形化配置kibana)
##點選Discover選擇log*索引就可以看到日誌的資料出來了。
##點選儀表板然後建立一個儀表盤:
##接下來就是建立一個圖表
##這裡就可以定義自己想要的圖表了:
##我比較喜歡看柱形圖,選擇自己的索引:
##最終可以出來的結果(我添加了request項是訪問的url)也可以是客戶端的IP
轉載於:https://blog.51cto.com/legehappy/1979029