Elasticsearch集群优化

时间:2023-03-09 07:56:44
Elasticsearch集群优化

版本配置:

ES版本:6.2.4

OS内存64G。

一、参数配置:

ES jvm内存31G。

vi /etc/sysctl.conf

vm.swappiness = 1

elasticsearch.yml中,设置这个:bootstrap.mlockall:true

有条件使用更好的硬盘如ssd。

如果有多块盘,每个盘mount到一个目录,data path配置多个。

二、合理的Index Mapping:

1.特殊字段:如Boolean、IP、时间等。

2.字符串:尽量使用keyword,默认的text会被analyze。keyword最大32766字节。

3.调整refresh间隔:refresh_interval: 30s,默认的1秒会导致大量segment产生,影响性能。

4.调整translog刷新机制为异步:

"index": {

"translog": {

"flush_threshold_size": "1gb",

"sync_interval": "30s",

"durability": "async"

}

}

三、缓存参数调整:

设置elasticsearch.yml中:

thread_pool.bulk.queue_size: 1024

indices.fielddata.cache.size: 1gb

indices.queries.cache.size: 1gb

indices.memory.index_buffer_size: 15% (默认10%)

indices.memory.index_buffer_size会影响写入性能,写入的分片数更多,则需要更多的buffer。

熔断circuit-breaker调整(调低):

indices.breaker.total.limit: 50%

indices.breaker.request.limit: 10%

indices.breaker.fielddata.limit: 10%

四、设置合理的分片数和副本数:

1.对于数据量较小(100GB以下)的index:一般设置3~5个shard

2.对于数据量较大(100GB以上)的index:一般把单个shard的数据量控制在20GB~40GB

3.对于30G内存的节点,shard数量最好控制在600个(即每1GB内存的shard数在20以内)

4.副本数:一般副本数为1,对数据可用性有高要求的,可以设置为2

五、段合并Segment Merge

1.通过_forcemerge API进行合并,减少segments数量,同时提高查询效率:

2.max_num_segments取值为:max_num_segments =(单个索引的大小G/分片数/2G)

六、时序数据:

1.合理按天、周、月、年去创建、关闭、删除索引。

2.索引太多时,索引预创建:每天定时任务把明天需要的索引先创建好。

3.从6.7版本开始kibana自带索引生命周期管理ILM(Indice Life Management)

七、查询写入:

1.使用批量请求bulk request。

2.尽量使用自动生成的ID。Elasticsearch自动生成的ID可以确保是唯一的,以避免版本查询。

3.尽量使用filter而不是query。使用filter不会计算评分score,只计算匹配。

4.如果不关心文档返回的顺序,则按_doc排序。

5.避免通配符查询。

6.不要获取太大的结果数据集,如果需要大量数据使用scroll API。

7.避免索引大的document数据,如超过100M的一条document。

参考:

https://cloud.tencent.com/developer/article/1357698

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/general-recommendations.html

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/tune-for-indexing-speed.html

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/tune-for-disk-usage.html

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/circuit-breaker.html

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/modules-indices.html

https://www.elastic.co/blog/how-many-shards-should-i-have-in-my-elasticsearch-cluster