ELK是三個開源軟件的縮寫,分別表示:Elasticsearch , Logstash, Kibana , 它們都是開源軟件。新增了一個FileBeat,它是一個輕量級的日誌收集處理工具(Agent),Filebeat佔用資源少,適合於在各個服務器上搜集日誌後傳輸給Logstash,官方也推薦此工具。前端
192.168.10.157 linux-node1 192.168.10.161 linux-node2
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch //導入密鑰 vim elasticsearch.repo //配置yum源 [elasticsearch-2.x] name=Elasticsearch repository for 2.x packages baseurl=http://packages.elastic.co/elasticsearch/2.x/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enable=1 yum install elasticsearch -y yum install java -y (1.8版本) java -version //查看java版本
cd /etc/elasticsearch/ vim elasticsearch.yml cluster.name: xxy //17行 集羣名稱 node.name: linux-node1 //23行 節點名稱 path.data: /data/es-data //33行 工做目錄 path.logs: /var/log/elasticsearch/ bootstrap.memory_lock: true //43行 防止交換swap分區 network.host: 0.0.0.0 //54行 監聽網絡 http.port: 9200 //58行 端口
mkdir -p /data/es-data chown -R elasticsearch:elasticsearch /data/es-data/ //屬主屬組設爲elasticsearch systemctl start elasticsearch.service netstat -ntap | grep 9200
測試 http://192.168.10.157:9200
curl -i -XGET 'http://192.168.175.132:9200/_count?pretty' -d '{"query": {"match_all": {}}}' #顯示 HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 95 { "count" : 0, "_shards" : { "total" : 0, "successful" : 0, "failed" : 0 } }
/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head //安裝位置/usr/share/elasticsearch/plugins/head 測試 http://192.168.10.157:9200/_plugin/head/
複合查詢 /index-demo/test POST { "user":"xxy", "mesg":"hello world" } 提交請求
/index-demo/test/AWVDUuVUPJxKK7V6Dj8E GET {}
/index-demo/test/AWVDUuVUPJxKK7V6Dj8E DELETE {}
修改配置文件java
cd /etc/elasticsearch/ vim elasticsearch.yml cluster.name: xxy //17行 集羣名稱 node.name: linux-node2 //23行 節點名稱 discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.10.157"] //69行 自動發現機制 啓動elasticsearch 一樣在linux-node1中配置 discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.10.161"] //69行 單播列表自動發現機制
http://192.168.10.161:9200/_plugin/head/ 會看到主分片和副本分片
/usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf 瀏覽器輸入:http://192.168.175.132:9200/_plugin/kopf/#!/cluster
Logstash 是一個接收,處理,轉發日誌的工具。支持系統日誌,webserver日誌,錯誤日誌,應用日誌,總之包括全部能夠拋出來的日誌類型。在一個典型的使用場景下(ELK):用 Elasticsearch做爲後臺數據的存儲,kibana用來前端的報表展現。Logstash在其過程當中擔任搬運工的角色,它爲數據存儲,報表查詢和日誌解析建立了一個功能強大的管道鏈。Logstash 提供了多種多樣的 input,filters,codecs 和output 組件,讓使用者輕鬆實現強大的功能。node
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch cd /etc/yum.repos.d/ vim logstash.repo [logstash-2.1] name=Logstash repository for 2.1.x packages baseurl=http://packages.elastic.co/logstash/2.1/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enable=1 yum install logstash -y
cd /opt/logstash/ ./bin/logstash -e 'input { stdin{} } output { stdout{} }'
cd /opt/logstash/ ./bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } }'
/opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.10.157:9200"] } }' 輸入事件 abc123 tom456 123jerry
在elasticsearch的web中點擊鏈接查看,點擊數據瀏覽選項卡能夠查看到事件的信息
linux
/opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.10.157:9200"] } stdout { codec => rubydebug } }'
vim /etc/logstash/conf.d/01-logstash.conf input { stdin { } } output { elasticsearch { hosts => ["192.168.10.157:9200"] } stdout { codec => rubydebug } } /opt/logstash/bin/logstash -f /etc/logstash/conf.d/01-logstash.conf
ln -s /opt/logstash/bin/logstash /usr/bin/ [root@localhost ~]# vim file.conf input { file { path => "/var/log/messages" type => "system" start_position => "beginning" } } output { elasticsearch { hosts => ["192.168.10.157:9200"] index => "system-%{+YYYY.MM.dd}" } } logstash -f /root/file.conf
vim file.conf input { file { path => "/var/log/messages" type => "system" start_position => "beginning" } file { path => "/var/log/elasticsearch/xxy.log" type => "es-error" start_position => "beginning" } } output { if [type] == "system" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "system-%{+YYYY.MM.dd}" } } if [type] == "es-error" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "es-error-%{+YYYY.MM.dd}" } } } logstash -f /root/file.conf
codec插件處理堆棧信息 //引用正則表達式 vim multiline.conf input { stdin { codec => multiline { pattern => "^\[" negate => true what => "previous" } } } output { stdout { codec => "rubydebug" } } logstash -f /root/multiline.conf
輸入測試,識別事件 [1] [2] [abc] [abcd efghi jklmn] [3]
#從新定義file.conf input { file { path => "/var/log/messages" type => "system" start_position => "beginning" } file { path => "/var/log/elasticsearch/yun.log" type => "es-error" start_position => "beginning" codec => multiline { pattern => "^\[" negate => true what => "previous" } } } output { if [type] == "system" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "system-%{+YYYY.MM.dd}" } } if [type] == "es-error" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "es-error-%{+YYYY.MM.dd}" } } } //添加多行日誌內容進行驗證 logstash -f /root/file.conf
概覽
web
Kibana 也是一個開源和免費的工具,Kibana能夠爲 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 界面,能夠幫助彙總、分析和搜索重要數據日誌。正則表達式
wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz //下載軟件包 tar zxvf kibana-4.3.1-linux-x64.tar.gz -C /opt/ mv kibana-4.3.1-linux-x64/ /usr/local/ mv kibana-4.3.1-linux-x64/ kibana //重命名 vim /usr/local/kibana/config/kibana.yml //修改配置文件 server.port: 5601 //2行 server.host: "0.0.0.0" //5行 elasticsearch.url: "http://192.168.10.157:9200" //12行 ES地址 kibana.index: ".kibana" //20行
Screen是一款由GNU計劃開發的用於命令行終端切換的自由軟件。用戶能夠經過該軟件同時鏈接多個本地或遠程的命令行會話,並在其間自由切換。GNU Screen能夠看做是窗口管理器的命令行界面版本。它提供了統一的管理多個會話的界面和相應的功能。json
yum install screen -y /usr/local/kibana/bin/kibana //啓動監聽 #ctrl+a+d 進行丟入後臺
瀏覽器訪問bootstrap
http://192.168.10.157:5601/