ES集羣性能優化及維護
注:集羣 elasticsearch 版本爲 v7.2.1。
1.ES索引刷新間隔設置:
index.refresh_interval 刷新時間,默認1
PUT index(_all)/_settings?preserve_existing=false
{
"index.refresh_interval": "15s"
}
2.ES索引備份數設置:
index.number_of_replicas 備份數,默認1
PUT index/_settings?preserve_existing=false
{
"index.number_of_replicas": "1"
}
3.ES索引分片數設置:
index.number_of_shards:分片數,默認1,最大:1024
注:在創建索引時設置!
例:
{
"settings": {
"index": {
"number_of_shards": 5,
"number_of_replicas": 1,
"refresh_interval": "5s",
"max_result_window": "50000"
}
}"aliases": {
"aliase": {}
},
"mappings": {}
}
4.ES搜索返回結果數最大10000條數據設置:
index.max_result_window: 10000 (默認)
PUT _all/_settings?preserve_existing=false
{
"index.max_result_window": "50000"
}
5.ES搜索聚合最大桶數10000設置:
search.max_buckets:10000(默認)
方式①:配置文件 elasticsearch.yml
增加配置項:
# 單個查詢最大的桶數,默認10000
search.max_buckets: 50000
方式②:動態修改該配置項:
PUT _cluster/settings
{
"transient": {
"search.max_buckets": "50000"
}
}
6.ES集羣設置刪除性操作不允許使用_all及通配符設置:
PUT /_cluster/settings
{
"persistent": {
"action.destructive_requires_name": true
}
}
7.ES集羣總分片數不足,每臺機器最大分片數設置:
ES7.0.0版本以上,默認只允許1000個分片。
查詢ES集羣節點分片數:
_cat/allocation?v
方式①:配置文件 elasticsearch.yml
增加配置項:
cluster.max_shards_per_node: 2000
方式②:動態修改該配置項:
_cluster/settings GET、PUT請求
{
"transient": {
"cluster": {
"max_shards_per_node":2000
}
}
}
恢復之前默認:
_cluster/settings GET、PUT請求
{
"transient": {
"cluster": {
"max_shards_per_node":null
}
}
}
8.ES集羣API請求設置與配置文件設置優先級說明:
集羣屬性設置方式:
(1)API方式設置:
a.臨時設置(集羣節點全部重啓失效):
{
"transient" : {
"xxx" : "yyy"
}
}
b.永久設置(集羣節點全部重啓依然生效):
PUT /_cluster/settings
{
"persistent" : {
"xxx" : "yyy"
}
}
(2)配置文件設置(集羣節點全部重啓依然生效):elasticsearch.yml
優先級:
transient cluster settings > persistent cluster settings > settings in the elasticsearch.yml configuration file.
9.ES集羣狀態異常,一直出現UnassignedShards解決方案:
①查詢索引分片信息:
GET _cat/shards?v
結果中查找:state 爲 UNASSIGNED 的索引。
②當索引無數據寫入時可執行關閉、開啓索引操作,重新觸發索引分片:
POST index/_close
POST index/_open
10.ES節點延遲分片分配-臨時重啓操作:
index.unassigned.node_left.delayed_timeout 默認 1m
PUT _all/_settings
{
"settings": {
"index.unassigned.node_left.delayed_timeout": "5m"
}
}
11.ES節點禁用分片轉移-臨時重啓操作:
PUT _cluster/settings
{
"transient": {
"cluster.routing.allocation.enable": "none"
}
}
恢復分片轉移:
PUT _cluster/settings
{
"transient": {
"cluster.routing.allocation.enable": "all"
}
}
12.ES節點手動分片轉移-永久下線操作:
PUT _cluster/settings
{
"transient": {
"cluster.routing.allocation.exclude._name": "node-1,node-2"
}
}
重新加入集羣:
PUT _cluster/settings
{
"transient": {
"cluster.routing.allocation.exclude._name": ""
}
}