ElasticSearch + Logstash + kibana

項羽齊發表於2018-03-23

ElasticSearch + Logstash + kibana 

1、介紹:

  Logstash:搬運工

  ElasticSearch:搜尋引擎

  Kilbana:視覺化系統

 

  

ElasticSearch是基於lucene的搜尋框架,它提供了一個分散式多使用者能力的全文搜尋引擎。

  基於restful web介面

  上手容易,擴充節點方便。

  可用於儲存和檢索海量資料,接近時實檢索,海量資料量增加,搜尋效能幾乎不受影響。

  分散式搜尋框架,副本機制,自動發現節點,保障可用性。

 

阿里雲伺服器 快速安裝ElasticSearch

簡介:阿里雲ecs介紹,wget命令下載安裝包,快速部署 elasticSearch節點

 

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目錄的基本結構

  核心配置檔案:config

    1、elasticsearch.yml

      主配置檔案       

        cluster.name:叢集名稱,同一網段自動加入

        node.name:節點名稱

        http.port:http埠

    2、jvm.options  

        虛擬機器引數配置檔案,配置heap堆一樣

    3、log4j2.properties

 

  配置叢集:

    複製ElasticSearch檔案包啟動即可

    注意事項:本地啟動多個節點,複製es安裝包的時候,需要刪除裡面data目錄裡面的資料,不然無法加入叢集

  

  ElasticSearch基礎概念

    ElasticSearch的index索引,Document文件、副本,分片,多節點等概念。

    通俗的解釋

      在ElasticSearch中,文件歸屬於一種型別(type),而這些型別存在於索引(index)中,索引名稱必須是小寫。

       

    分片shards

      1、資料量特大,沒有足夠大的硬碟空間來一次性儲存,

      2、且一次性搜尋那麼多的資料,響應跟不上es提供把資料進行分片儲存,這樣方便進行擴充和提高吞吐

 

    副本replicas

      分片的拷貝,當主分片不可用的時候,副本就充當主分片進行使用

      Elasticsearch中的每個索引分配5個主分片和1個副本

        如果你的叢集中至少有兩個節點,你的索引將會有5個主分片和另外5個複製分片(1個完全拷貝),這樣每個索引總共就有10個分片。 

    search搜尋語句入門之URL搜尋

      健康檢查

        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型別,ID1

curl -XPUT -H "Content-Type: application/json" 'localhost:9201/blog/article/2?pretty' -d '
{
  "title": "東邪西毒",
  "content":"我知道那個人不會再來,但我還是在等,我在門口坐了兩天兩夜"
}'

      ID查詢記錄

        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

  1、配置檔案:

    修改ElasticSearch配置:elasticsearch.yml

    取消註釋並修改為:network.host 0.0.0.0

    修改後會有一些啟動錯誤,可以檢視上面連結,或者百度進行解決。

  2、阿里雲需要在安全防火牆開放埠

query dsl

        1、Domain Specific Language 領域特定語言

        2、ElasticSearch提供了完整的dsl查詢語句,基於json定義查詢

        3、用於構造複雜的查詢語句

        

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的速度

    2filter不會計算相關度得分,且結果會有快取,效率高

    3、全文搜尋、評分排序,使用query

    4、是非過濾,精確匹配,使用filter

 

 

    postman工具

 

 

Logstash

  

 

  什麼是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核數增加12即可

      jvm.options   修改 xmsxmx為相同,一般是系統記憶體三份之二

 日誌檔案輸入輸出

    簡介:講解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

 

    1、設定配置檔案

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"
}
第一種配置

    2、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"
}
filter使用,mutate外掛

    3、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
採集ElasticSearch日誌到ElasticSearch資料庫

  

  

Kibana視覺化工具

  下載及安裝: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   

    注意事項

    1、kibana和elsticserch版本不能差別大,否則無法正常使用 比如 Kibana 6.x 和 Elasticsearch 2.x不能正常使用
    2、執行比Kibana更高版本的Elasticsearch通常可以工作 例如Kibana 5.0和Elasticsearch 5.1
    3、小版本差異會有一些警告出現,除非兩者升級到相同的版本

  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

    1、建立索引表示式
    使用*統配符,去匹配ES中的一個或多個索引(如果沒有匹配,無法點選下一步)

    2、discover皮膚發現資料
    可以指定時間進行查詢
    可以使顯示的欄位
    查詢索引的資料,可以使用lucence語法進行查詢

 

 

  專案實戰系列之《採集業務應用日誌》配置
    簡介:選擇日誌源,配置logstash採集並輸送到elasticSeach

    常見問題解決
    1、JVM記憶體溢位導致的 ES或者Logstash服務啟不來,報錯 insufficient memory
      解決:升級機器的記憶體和CPU;
      或者改elasticSeach和logstash的JVM.option,最大堆記憶體xmx和初始堆記憶體xms


    2、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

    3、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

 

 

相關文章