ElasticSearch——Logstash輸出到Elasticsearch配置


 

位置

在Logstash的.conf配置文件中的output中配置ElasticSearch

示例:

output {
  elasticsearch{
      action => "index"
      index => "%{[fields][product_type]}-transaction-%{+YYYY-MM}"
     hosts => ["10.0.xx.xx:9200", "10.0.xx.xx:9200", "10.0.xx.xx:9200"]
   }
}

 

action

  • index 給一個文檔建立索引
  • delete 通過id值刪除一個文檔(這個action需要指定一個id值)
  • create 插入一條文檔信息,如果這條文檔信息在索引中已經存在,那么本次插入工作失敗
  • update 通過id值更新一個文檔。更新有個特殊的案例upsert,如果被更新的文檔還不存在,那么就會用到upsert

示例:

action => "index"

index

寫入事件所用的索引。可以動態的使用%{foo}語法,它的默認值是:
"logstash-%{+YYYY.MM.dd}",以天為單位分割的索引,使你可以很容易的刪除老的數據或者搜索指定時間范圍內的數據。

索引不能包含大寫字母。推薦使用以周為索引的ISO 8601格式,例如logstash-%{+xxxx.ww}

示例:

index => "%{[fields][product_type]}-transaction-%{+YYYY-MM}"

hosts

是一個數組類型的值

意http協議使用的是http地址,端口是9200,示例:

hosts => ["10.0.xx.xx:9200", "10.0.xx.xx:9200", "10.0.xx.xx:9200"]

document_type

定義es索引的type,一般你應該讓同一種類型的日志存到同一種type中,比如debug日志和error日志存到不同的type中

如果不設置默認type為logs

template

如果你願意,你可以設置指向你自己模板的路徑。如果沒有設置,那么默認的模板會被使用

template_name

這個配置項用來定義在Elasticsearch中模板的命名

注意刪除舊的模板示例:

curl -XDELETE <http://localhost:9200/_template/OldTemplateName?pretty>

template_overwrite

布爾類型 默認為false
設置為true表示如果你有一個自定義的模板叫logstash,那么將會用你自定義模板覆蓋默認模板logstash

manage_template

布爾類型 默認為true
設置為false將關閉logstash自動管理模板功能
比如你定義了一個自定義模板,更加字段名動態生成字段,那么應該設置為false

order參數

ELK Stack 在入門學習過程中,必然會碰到自己修改定制索引映射(mapping)乃至模板(template)的問題。
這時候,不少比較認真看 Logstash 文檔的新用戶會通過下面這段配置來制定自己的模板策略:

output {
    elasticsearch {
        host => "127.0.0.1"
        manage_template => true
        template => "/path/to/mytemplate"
        template_name => "myname"
    }
}

然而隨后就發現,自己辛辛苦苦修改出來的模板,通過 curl -XGET 'http://127.0.0.1:9200/_template/myname' 看也確實上傳成功了,但實際新數據索引創建出來,就是沒生效!

這個原因是:Logstash 默認會上傳一個名叫 logstash 的模板到 ES 里。如果你在使用上面這個配置之前,曾經運行過 Logstash(一般來說都會),那么 ES 里就已經存在這么一個模板了。你可以curl -XGET 'http://127.0.0.1:9200/_template/logstash' 驗證。

這個時候,ES 里就變成有兩個模板,logstash 和 myname,都匹配 logstash-* 索引名,要求設置一定的映射規則了。

ES 會按照一定的規則來嘗試自動 merge 多個都匹配上了的模板規則,最終運用到索引上

其中要點就是:template 是可以設置 order 參數的!而不寫這個參數,默認的 order 值就是 0。order 值越大,在 merge 規則的時候優先級越高。

所以,解決這個問題的辦法很簡單:在你自定義的 template 里,加一行,變成這樣:

{
    "template" : "logstash-*",
    "order" : 1,
    "settings" : { ... },
    "mappings" : { ... }
}

當然,其實如果只從 Logstash 配置角度出發,其實更簡單的辦法是:直接修改原來默認的 logstash 模板,然后模板名稱也不要改,就好了:

output {
    elasticsearch {
        host => "127.0.0.1"
        manage_template => true
        template_overwrite => true
    }
}

 

為elasticsearch配置模板

在使用logstash收集日志的時候,我們一般會使用logstash自帶的動態索引模板,雖然無須我們做任何定制操作,就能把我們的日志數據推送到elasticsearch索引集群中

但是在我們查詢的時候,就會發現,默認的索引模板常常把我們不需要分詞的字段,給分詞了,這樣以來,我們的比較重要的聚合統計就不准確了:

所以這時候,就需要我們自定義一些索引模板了

在logstash與elasticsearch集成的時候,總共有如下幾種使用模板的方式:

  1. 使用默認自帶的索引模板 ,大部分的字段都會分詞,適合開發和時候快速驗證使用

  2. 在logstash收集端自定義配置模板,因為分散在收集機器上,維護比較麻煩

  3. 在elasticsearc服務端自定義配置模板,由elasticsearch負責加載模板,可動態更改,全局生效,維護比較容易

使用默認自帶的索引模板

ElasticSearch默認自帶了一個名字為”logstash”的模板,默認應用於Logstash寫入數據到ElasticSearch使用

優點:最簡單,無須任何配置

缺點:無法自定義一些配置,例如:分詞方式

在logstash收集端自定義配置模板

使用第二種,適合小規模集群的日志收集

需要在logstash的output插件中使用template指定本機器上的一個模板json路徑, 例如 template => "/tmp/logstash.json"

優點:配置簡單

缺點:因為分散在Logstash Indexer機器上,維護起來比較麻煩

在elasticsearc服務端自定義配置模板

manage_template => false//關閉logstash自動管理模板功能  
template_name => "xxx"//映射模板的名字 

第三種需要在elasticsearch的集群中的config/templates路徑下配置模板json,在elasticsearch中索引模板可分為兩種

靜態模板

適合索引字段數據固定的場景,一旦配置完成,不能向里面加入多余的字段,否則會報錯

優點:scheam已知,業務場景明確,不容易出現因字段隨便映射從而造成元數據撐爆es內存,從而導致es集群全部宕機,維護比較容易,可動態更改,全局生效。

缺點:字段數多的情況下配置稍繁瑣

一個靜態索引模板配置例子如下:

{  
  "xxx" : {  
      "template": "xxx-*",  
        "settings": {  
            "index.number_of_shards": 3,  
            "number_of_replicas": 0   
        },  
    "mappings" : {  
      "logs" : {  
        "properties" : {  
          "@timestamp" : { //這是專門給kibana用的一個字段,時間索引
            "type" : "date",  
            "format" : "dateOptionalTime",  
            "doc_values" : true  
          },  
          "@version" : {  
            "type" : "string",  
            "index" : "not_analyzed",  
            "doc_values" : true      
          },  
          "id" : {  
            "type" : "string",  
            "index" : "not_analyzed"  
          },  
          "name" : {  
            "type" : "string",  
            "index" : "not_analyzed"  
          }
        }  
      }  
    }  
  }  
} 

動態模板

適合字段數不明確,大量字段的配置類型相同的場景,多加字段不會報錯

優點:可動態添加任意字段,無須改動scheaml,

缺點:如果添加的字段非常多,有可能造成es集群宕機

一個動態索引模板配置例子如下:

{  
  "template" : "xxx-*",  
  "settings" : {  
   "index.number_of_shards": 5,  
   "number_of_replicas": 0    
  
},  
  "mappings" : {  
    "_default_" : {  
      "_all" : {"enabled" : true, "omit_norms" : true},  
      "dynamic_templates" : [ {  
        "message_field" : {  
          "match" : "message",  
          "match_mapping_type" : "string",  
          "mapping" : {  
            "type" : "string", "index" : "analyzed", "omit_norms" : true,  
            "fielddata" : { "format" : "disabled" }  
          }  
        }  
      }, {  
        "string_fields" : {  
          "match" : "*",  
          "match_mapping_type" : "string",  
          "mapping" : {  
            "type" : "string", "index" : "not_analyzed", "doc_values" : true  
          }  
        }  
      } ],  
      "properties" : {  
        "@timestamp": { "type": "date" },  
        "@version": { "type": "string", "index": "not_analyzed" }, 
        "geoip"  : {  
          "dynamic": true,  
          "properties" : {  
            "ip": { "type": "ip" },  
            "location" : { "type" : "geo_point" },  
            "latitude" : { "type" : "float" },  
            "longitude" : { "type" : "float" }  
          }  
        }  
      }  
    }  
  }  
} 

只設置message字段分詞,其他的字段默認都不分詞

 

模板結構

  • 通用設置 主要是模板匹配索引的過濾規則,影響該模板對哪些索引生效
  • settings:配置索引的公共參數,比如索引的replicas,以及分片數shards等參數
  • mappings:最重要的一部分,在這部分中配置每個type下的每個field的相關屬性,比如field類型(string,long,date等等),是否分詞,是否在內存中緩存等等屬性都在這部分配置
  • aliases:索引別名,索引別名可用在索引數據遷移等用途上。

例子:

{
  "logstash" : {
    "order" : 0,
    "template" : "logstash-*",
    "settings" : {
      "index" : {
        "refresh_interval" : "5s"
      }
    },
    "mappings" : {
      "_default_" : {
        "dynamic_templates" : [ {
          "message_field" : {
            "mapping" : {
              "fielddata" : {
                "format" : "disabled"
              },
              "index" : "analyzed",
              "omit_norms" : true,
              "type" : "string"
            },
            "match_mapping_type" : "string",
            "match" : "message"
          }
        }, {
          "string_fields" : {
            "mapping" : {
              "fielddata" : {
                "format" : "disabled"
              },
              "index" : "analyzed",
              "omit_norms" : true,
              "type" : "string",
              "fields" : {
                "raw" : {
                  "ignore_above" : 256,
                  "index" : "not_analyzed",
                  "type" : "string"
                }
              }
            },
            "match_mapping_type" : "string",
            "match" : "*"
          }
        } ],
        "_all" : {
          "omit_norms" : true,
          "enabled" : true
        },
        "properties" : {
          "@timestamp" : {
            "type" : "date"
          },
          "geoip" : {
            "dynamic" : true,
            "properties" : {
              "ip" : {
                "type" : "ip"
              },
              "latitude" : {
                "type" : "float"
              },
              "location" : {
                "type" : "geo_point"
              },
              "longitude" : {
                "type" : "float"
              }
            }
          },
          "@version" : {
            "index" : "not_analyzed",
            "type" : "string"
          }
        }
      }
    },
    "aliases" : { }
  }
}
我們創建一個自定義Template動態模板,這個模板指定匹配所有以”go_logsindex“開始的索引,並且指定允許添加新字段,匹配所有string類型的新字段會創建一個raw的嵌套字段,這個raw嵌套字段類型也是string,但是是not_analyzed不分詞的(主要用於解決一些analyzed的string字段無法做統計,但可以使用這個raw嵌套字段做統計)
{
  "template": "go_logs_index_*",
  "order":0,
  "settings": {
      "index.number_of_replicas": "1",
      "index.number_of_shards": "5",
      "index.refresh_interval" : "10s"
  },
  "mappings": {
    "_default_": {
      "_all": {
        "enabled": false
      },
      "dynamic_templates": [
        {
          "my_template": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "string",
              "fields": {
                "raw": {
                  "type": "string",
                  "index": "not_analyzed"
                }
              }
            }
          }
        }
      ]
    },
    "go": {
      "properties": {
        "timestamp": {
          "type": "string",
          "index": "not_analyzed"
        },
        "msg": {
          "type": "string",
          "analyzer": "ik",
          "search_analyzer": "ik_smart"
        },
        "file": {
          "type": "string",
          "index": "not_analyzed"
        },
        "line": {
          "type": "string",
          "index": "not_analyzed"
        },
        "threadid": {
          "type": "string",
          "index": "not_analyzed"
        },
        "info": {
          "type": "string",
          "index": "not_analyzed"
        },
        "type": {
          "type": "string",
          "index": "not_analyzed"
        },
        "@timestamp": {
          "format": "strict_date_optional_time||epoch_millis",
          "type": "date"
        },
        "@version": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}

 

總結

第三種方式統一管理Template最好,推薦使用第三種方式,但是具體問題具體分析。例如場景是Logstash 和ElasticSearch都在一台服務器,第二種就比較好。

 

定制索引模板,是搜索業務中一項比較重要的步驟,需要注意的地方有很多,比如:
1.字段數固定嗎
2.字段類型是什么
3.分不分詞
4.索引不索引
5.存儲不存儲
6.排不排序
7.是否加權




參考鏈接:https://www.jianshu.com/p/0b89c07021f4


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM