一、介紹:html
Logstash:搬運工java
ElasticSearch:搜索引擎node
Kilbana:可視化系統linux
ElasticSearch是基於lucene的搜索框架,它提供了一個分佈式多用戶能力的全文搜索引擎。web
基於restful web接口數據庫
上手容易,拓展節點方便。json
可用於存儲和檢索海量數據,接近時實檢索,海量數據量增長,搜索性能幾乎不受影響。bootstrap
分佈式搜索框架,副本機制,自動發現節點,保障可用性。vim
簡介:阿里雲ecs介紹,wget命令下載安裝包,快速部署 elasticSearch節點windows
linux下使用wget下載jdk8:
進到目錄/usr/local/software
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
vim /etc/profile
加入
export JAVA_HOME=/usr/local/src/jdk8/jdk1.8.0_141
export JAVA_BIN=/usr/local/src/jdk8/jdk1.8.0_141
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
source /etc/profile 讓配置文件立刻生效
使用wget 下載elasticsearch安裝包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.2.tar.gz
解壓
tar -zxvf elasticsearch-6.2.2.tar.gz
執行:./elasticsearch 會報錯
chmod -R 777 ./
su - xdclass
curl localhost:9200 linux查看本地服務器
配置es出現相關問題處理:
1、問題一
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/software/temp/elasticsearch-6.2.2/hs_err_pid1912.log
解決:內存不夠,購買阿里雲的機器能夠動態增長內存,至少須要2G內存
2、問題二
[root@iZwz95j86y235aroi85ht0Z bin]# ./elasticsearch
[2018-02-22T20:14:04,870][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.2.jar:6.2.2]
解決:用非root用戶
添加用戶:useradd -m xiang
而後設置密碼:passwd xiang
給予用戶全部權限須要使用root權限來受權:chmod -R 777 ./ (./表示當前目錄)
切換到用戶:su - xiang
/usr/local/src/elasticsearch/elasticsearch-6.2.2
3、問題三
./elasticsearch
Exception in thread "main" java.nio.file.AccessDeniedException: /usr/local/software/temp/elasticsearch-6.2.2/config/jvm.options
解決:權限不夠 chmod 777 -R 當前es目錄
常見配置問題資料:https://www.jianshu.com/p/c5d6ec0f35e0
一、elasticsearch.yml
主配置文件
cluster.name:集羣名稱,同一網段自動加入
node.name:節點名稱
http.port:http端口
二、jvm.options
虛擬機參數配置文件,配置heap堆同樣
三、log4j2.properties
複製ElasticSearch文件包啓動便可
注意事項:本地啓動多個節點,複製es安裝包的時候,須要刪除裏面data目錄裏面的資料,否則沒法加入集羣
ElasticSearch的index索引,Document文檔、副本,分片,多節點等概念。
在ElasticSearch中,文檔歸屬於一種類型(type),而這些類型存在於索引(index)中,索引名稱必須是小寫。
一、數據量特大,沒有足夠大的硬盤空間來一次性存儲,
二、且一次性搜索那麼多的數據,響應跟不上es提供把數據進行分片存儲,這樣方便進行拓展和提升吞吐
副本replicas
分片的拷貝,當主分片不可用的時候,副本就充當主分片進行使用
Elasticsearch中的每一個索引分配5個主分片和1個副本
若是你的集羣中至少有兩個節點,你的索引將會有5個主分片和另外5個複製分片(1個徹底拷貝),這樣每一個索引總共就有10個分片。
http://localhost:9200/_cat/health?v
http://localhost:9201/_cluster/health(推薦
狀態說明
green:正常
yellow: 集羣正常 數據正常,部分副本不正常
red: 集羣部分正常,數據可能丟失,須要緊急修復
http://localhost:9200/_cat/nodes?v
http://localhost:9200/_cat/indices?v
補充:
curl
-X 指定http的請求方法 有HEAD GET POST PUT DELETE
-d 指定要傳輸的數據
-H 指定http請求頭信息
curl -XPUT 'localhost:9201/blog_test?pretty'
curl -XPUT 'localhost:9201/blog?pretty'
curl -XDELETE 'localhost:9200/blog_test?pretty'
新增一條記錄,並指定爲article類型,ID爲1
curl -XPUT -H "Content-Type: application/json" 'localhost:9201/blog/article/2?pretty' -d ' { "title": "東邪西毒", "content":"我知道那我的不會再來,但我仍是在等,我在門口坐了兩天兩夜" }'
curl -XGET 'localhost:9201/blog/article/1'
curl -XGET 'localhost:9201/blog/article/1?pretty'(美化推薦)
curl -XGET 'http://localhost:9201/blog/article/_search?q=title:小D'
一、配置文件:
修改ElasticSearch配置:elasticsearch.yml
取消註釋並修改成:network.host 0.0.0.0
修改後會有一些啓動錯誤,能夠查看上面連接,或者百度進行解決。
二、阿里雲鬚要在安全防火牆開放端口
一、Domain Specific Language 領域特定語言
二、ElasticSearch提供了完整的dsl查詢語句,基於json定義查詢
三、用於構造複雜的查詢語句
curl查詢(空格處理不當,會出問題) curl -XPOST -H "Content-Type: application/json" 'http://localhost:9201/blog/article/_search' -d '{ "query" : { "term" : { "title" : "東" } } }'
bool查詢入門 { "query": { "bool": { "must": [ { "match": { "title": "elk" } } ], "must_not": [ { "match": { "title": "小D" } } ] } } }
filter查詢入門(filtered語法已經在5.0版本後移除了,在2.0時候標記過時,改用filter ) 參考地址:https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-filtered-query.html { "query": { "bool": { "filter": { "range": { "PV": { "gt": 15 } } }, "must": { "match": { "title": "ELK" } } } } }
總結:(官網參考 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html)
1、大部分filter的速度快於query的速度
2、filter不會計算相關度得分,且結果會有緩存,效率高
3、全文搜索、評分排序,使用query
4、是非過濾,精確匹配,使用filter
postman工具
什麼是logstash (文檔地址 https://www.elastic.co/guide/en/logstash/current/index.html)
開源的日誌收集引擎,具有實時傳輸的能力
讀取不一樣的數據源,並進行過濾,開發者自定義規範輸出到目的地
日誌來源多(如系統日誌,應用日誌,服務器日誌等)
流程講解
logstash經過管道pipeline進行傳輸,必選的兩個組件是輸入input和輸出output,還有個可選過濾器filter
logstash將數據流中等每一條數據稱之爲一個event,即讀取每一行數據的行爲叫作事件
#輸入
input {
...
}
# 過濾器
filter {
...
}
# 輸出
output {
...
}
下載地址: https://www.elastic.co/downloads/logstash
在linux解壓便可:
啓動:在bin目錄下 ./logstash -e 'input {stdin {}} output {stdout {}}'
啓動會有些慢
須要java8 不支持java9
目錄文件說明
https://www.elastic.co/guide/en/logstash/6.2/dir-layout.html
配置講解
https://www.elastic.co/guide/en/logstash/6.2/logstash-settings-file.html
logstash.yml 修改 pipeline.workers,根據CPU核數增長1到2便可
jvm.options 修改 xms和xmx爲相同,通常是系統內存三份之二
簡介:講解Logstash採集日誌和輸送日誌流程測試,包括input,filter和output元素的測試
bin/logstash -f test1.conf
./logstash -f ../config/test1.conf
codec的使用( Coder/decoder 兩個單詞首字母縮寫)
Codec: 解碼編碼 數據格式
好處 更方便logstash與支持自定義數據格式的運維產品進行使用
logstash更細化的處理流程
input->decode->filter->encode->output
一、設置配置文件
input { # 從文件讀取日誌信息 輸送到控制檯 file { path => "/usr/local/src/elasticsearch/elasticsearch-6.2.2/logs/elasticsearch.log" #codec => "json" ## 以JSON格式讀取日誌 type => "elasticsearch" start_position => "beginning" } } # filter { # # } output { # 標準輸出 # stdout {} # 輸出進行格式化,採用Ruby庫來解析日誌 stdout { codec => rubydebug } } ========================================== 輸出結果: { "type" => "elasticsearch", "message" => "[2018-03-24T14:39:54,536][INFO ][o.e.g.GatewayService ] [node-xiang] recovered [2] indices into cluster_state", "host" => "iz2ze6bvf2t30pcc1l1jc1z", "path" => "/usr/local/src/elasticsearch/elasticsearch-6.2.2/logs/elasticsearch.log", "@timestamp" => 2018-03-24T06:39:55.091Z, "@version" => "1" }
二、filter使用
例子
切割插件mutate,隨意輸入一串以|分割的字符,好比 "123|000|ttter|sdfds*=123|dfwe
配置二 test2_filter.conf ======================================== input { stdin {} } filter { mutate { split => ["message", "|"] } } output { # 標準輸出 # stdout {} # 輸出進行格式化,採用Ruby庫來解析日誌 stdout { codec => rubydebug } } ======================================== 輸入結果: xiang|ning|xiang|wang|ning|n^Hj { "@version" => "1", "message" => [ [0] "xiang", [1] "ning", [2] "xiang", [3] "wang", [4] "ning", [5] "n\bj" ], "@timestamp" => 2018-03-24T06:55:42.822Z, "host" => "iz2ze6bvf2t30pcc1l1jc1z" }
三、logstash案例實戰之讀取日誌輸出到elasticsearch
簡介:從日誌文件中讀取日誌,輸出到elasticsearch集羣中
logstash配置文件
配置三 test3_es.conf
======================================== input { file { path => "/Users/jack/Desktop/person/elk/elasticsearch-6.1.1/logs/elasticsearch.log" type => "elasticsearch" start_position => "beginning" #從文件開始處讀寫 } } output{ elasticsearch{ hosts=>["127.0.0.1:9201"] index => "es-message-%{+YYYY.MM.dd}" } stdout{codec => rubydebug} } ======================================== 驗證 查看索引列表 http://localhost:9201/_cat/indices?v 查看數據 http://localhost:9201/es-message-2018.02.26/_search
下載及安裝:wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.2-linux-x86_64.tar.gz
解壓 tar -zxvf kibana-6.2.2-linux-x86_64.tar.gz
訪問地址
本機:localhost:5601
阿里雲機器:http://120.79.160.143:5601
開放端口,修改配置文件 confing目錄下的kibana.yml
server.host: "0.0.0.0"
nohup XXX &
tail - f nohup.out
kibana基本介紹、和elasticSearch版本兼容問題
簡介:講解什麼是kibana,目錄文件講解,配置等
官網文檔地址:https://www.elastic.co/guide/en/kibana/current/setup.html
ELK
注意事項
一、kibana和elsticserch版本不能差異大,不然沒法正常使用 好比 Kibana 6.x 和 Elasticsearch 2.x不能正常使用
二、運行比Kibana更高版本的Elasticsearch一般能夠工做 例如Kibana 5.0和Elasticsearch 5.1
三、小版本差別會有一些警告出現,除非二者升級到相同的版本
windows下安裝啓動文檔
https://www.elastic.co/guide/en/kibana/current/windows.html
kibana.yml常見配置項
elasticsearch.pingTimeout 平常用的ping
elasticsearch.requestTimeout 讀取es的超時時間
elasticsearch.url es主機地址
elasticsearch.username es鑑權的用戶名
elasticsearch.password es鑑權的密碼
kibana面板講解和功能使用說明
簡介:講解kibana的web界面,各個模塊劃分,功能的基本使用
kibana狀態及服務器資源使用率
http://120.79.160.143:5601/status
基礎操做文檔:https://www.elastic.co/guide/en/kibana/current/getting-started.html
一、建立索引表達式
使用*統配符,去匹配ES中的一個或多個索引(若是沒有匹配,沒法點擊下一步)
二、discover面板發現數據
能夠指定時間進行查詢
可使顯示的字段
查詢索引的數據,可使用lucence語法進行查詢
項目實戰系列之《採集業務應用日誌》配置
簡介:選擇日誌源,配置logstash採集並輸送到elasticSeach
常見問題解決
一、JVM內存溢出致使的 ES或者Logstash服務啓不來,報錯 insufficient memory
解決:升級機器的內存和CPU;
或者改elasticSeach和logstash的JVM.option,最大堆內存xmx和初始堆內存xms
二、ES啓動報錯
seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed
修改elasticsearch.yml 添加一下內容
bootstrap.memory_lock: false 爲了不內存和磁盤之間的swap
bootstrap.system_call_filter: false
三、ERROR: bootstrap checks failed
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least
臨時設置:sudo sysctl -w vm.max_map_count=262144
永久修改:
修改/etc/sysctl.conf 文件,添加 「vm.max_map_count」設置
並執行:sysctl -p
項目實戰系列之Kibana圖形、報表分析
簡介:講解業務應用日誌在Kibana上的可視化分析,柱狀圖,餅狀圖等
官方文檔地址:
https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html
下載數據集
wget https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip
解壓 unzip accounts.zip
導入數據到es中
curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9201/bank/account/_bulk?pretty' --data-binary @accounts.json
示例地址 https://www.elastic.co/guide/en/kibana/current/tutorial-visualizing.html