Elasticsearch 集羣自我保護配置

Elasticsearch 集羣集羣自我保護配置

配置調試原因主要是爲了保護ES 集羣能夠自我保護,避免業務人員寫暴力查詢語句,大量的佔用內存或者CPU最終將es集羣直接查掛。文檔所有值均爲參考值,具體設置多少合適需要根據集羣規模、index分片數量、節點配置等等。。因素進行考量,設置合理閾值。

調試環境

ES 版本 :elasticsearch-6.2.4-1.noarch
OS:centos7.X

全局超時 search.default_search_timeout

除了在每個請求中設置超時之外,ES還支持全局性的搜索超時search.default_search_timeout,此設置沒有默認值,設置爲-1可以取消先前設置的值。

設置方法:

persistent :重啓後依然有效
transient :重啓後無效

PUT /_cluster/settings
{
    "persistent" : {
        "search.default_search_timeout" : "35s"
    }
}

取消查詢 earch.low_level_cancellation

搜索可以通過標準的任務取消機制來取消。默認情況下ES僅僅在段邊界(segment boundaries)來檢查請求是否已經被取消,因此取消操作可能由於大段而延遲。要降低取消操作的響應時間,可以設置search.low_level_cancellation=true,但是要注意此設置會導致更加頻繁的檢查。定期檢查自己是否被取消,如果是則退出查詢。

設置方法:

PUT /_cluster/settings
{
    "persistent" : {
        "search.low_level_cancellation" :true
    }
}

設置此參數之後可以使用api接口手動取消任務;

#查詢正在運行的task
GET _tasks?detailed=true&actions=*indices:data/read/search

#取消任務
POST _tasks/CdoilmnzRVyllc0PbRbB2w:7280/_cancel

search 併發和並行

action.search.shard_count.limit

這個參數主要用於限制一次操作過多的分片,防止過多的佔用內存和CPU資源。默認情況下ES不限制搜索請求牽涉到的分片數量,你可以設置軟限制 action.search.shard_count.limit 來拒絕命中太多分片的請求。

如果看到錯誤日誌如下,修改此參數可以解決問題。

"reason" : "Trying to query 1344 shards, which is over the limit of 1000. This limit exists because querying 
many shards at the same time can make the job of the coordinating node very CPU and/or memory 
intensive. It is usually a better idea to have a smaller number of larger shards. Update 
[action.search.shard_count.limit] to a greater value if you really want to query that many shards at the same time."
PUT /_cluster/settings
{
    "persistent" : {
        "action.search.shard_count.limit" :"1000"
    }
}

or

curl -u admin:admin -XPUT 'https://localhost:9200/_cluster/settings' -H 'Content-Type: application/json' -d' 
{
    "persistent" : {
        "action.search.shard_count.limit" : "1000"
    }
}
max_concurrent_shard_requests (貌似新版本已經放棄此參數,確認中。。)
參數可以限制搜索請求最多同時在多少個分片上執行,可以防止單個搜索請求消耗整個集羣的資源。此參數的默認值取決於集羣中數據節點的數量,最多256。

###### 設置方法:

PUT /_cluster/settings
{
    "persistent" : {
        "max_concurrent_shard_requests : "80"
    }
}

index.max_result_window

默認大小:10000

就是from+size,from:決定要返回的文檔從哪裏開始,size:決定返回多少條。假如from+size很大的話,將會消耗很多的內存和時間;這個設置就是爲了防止內存不夠用的情況。
這個設置是索引層的,即便是使用_all設置了,看日誌也是對逐個索引加這個配置,後續新加的索引,max_result_window默認還是1w。
使用Elasticsearch進行search查詢的過程中,出現了Result window is too large問題。一般是這個參數設置過小導致的。
可通過如下方式修改:

配置單個索引

#查看單個索引設置
GET /27_hot_v1/_settings

#修改配置
PUT /27_hot_v1/_settings
{
  "index.max_result_window":"10000"
}

or

PUT /27_hot_v1/_settings
{
  "index.max_result_window":"10000"
}

curl -XPUT 192.168.40.31:9200/$(Index-name)/_settings -d '{ "index.max_result_window" :"1000000"}'

設置所有索引生效

curl -XPUT 192.168.40.31:9200/_all/_settings -d '{ "index.max_result_window" :"1000000"}'

驗證配置結果

curl -XGET 172.20.7.22:9200/$(index-name)/_settings

index.max_inner_result_window:

默認是:100。這個設置限制的是 返回 結果中的 結果!
index.max_inner_result_window:和index.max_result_window原理差不多,這個設置限制的是返回結果中的結果。

配置單個索引

#查看單個索引設置
GET /27_hot_v1/_settings

#修改配置
PUT /27_hot_v1/_settings
{
  "max_inner_result_window":"5000"
}

or

PUT /27_hot_v1/_settings
{
  "index.max_inner_result_window":"100"
}

index.max_docvalue_fields_search

默認最多請求100字段,Doc-value 字段成本很高,因爲它們可能會導致每個字段的每個文檔搜索。可以通過使用index.max_docvalue_fields_search索引設定覆蓋.

PUT /27_hot_v1/_settings
{
  "index.max_docvalue_fields_search":"100"
}

index.max_docvalue_fields_search

默認爲32。ndex.max_script_fields:查詢中允許的最大script_field數。
PUT /27_hot_v1/_settings
{
  "ndex.max_script_fields":"32"
}

參考文檔:
https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search.html#global-search-cancellation
https://mp.weixin.qq.com/s/mKL2PJuNUJTl71Axv4-Rcw
https://www.cnblogs.com/huangpeng1990/p/4364341.html
https://kionf.com/2019/01/22/errornote-elk/
https://www.elastic.co/guide/en/elasticsearch/reference/6.6/modules-threadpool.html#_literal_fixed_literal

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章