Elasticsearch集群优化

系统层面

修改默认文件描述符大小node

echo '* soft nofile 1048576' >>/etc/security/limits.conf
echo '* hard nofile 1048576' >>/etc/security/limits.conf
echo 'elasticsearch soft memlock unlimited' >>/etc/security/limits.conf
echo 'elasticsearch hard memlock unlimited' >>/etc/security/limits.conf

 关闭swap,若是/etc/fstab里面设置了开机自动挂载注释掉nginx

swapoff -a

JVM分配为物理内存一半,最多设置不要超过32Gjson

配置文件:/etc/elasticsearch/jvm.options缓存

内核优化配置文件bash

vm.swappiness = 1
vm.max_map_count=262144
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 30
net.core.rmem_max=8388608
net.core.wmem_max=8388608
net.core.rmem_default=65536
net.core.wmem_default=65536
net.ipv4.tcp_rmem='4096 87380 8388608'
net.ipv4.tcp_wmem='4096 65536 8388608'
net.ipv4.tcp_mem='8388608 8388608 8388608'
net.ipv4.route.flush=1
fs.file-max=1048576
net.ipv4.tcp_keepalive_time = 30
net.ipv4.tcp_keepalive_probes = 2
net.ipv4.tcp_keepalive_intvl = 2
net.ipv4.ip_local_port_range = 5000 65000
vm.overcommit_memory = 1
net.ipv4.ip_forward=1

Elasticsearch配置层面

加大refresh_interval参数,增长缓存刷新到磁盘的时间,该配置的代价就是60秒后才把缓存数据刷新到磁盘。cookie

curl -XPUT localhost:9200/logstash-*/_settings -d '{
    "index" : {
        "refresh_interval" : "60s"
    } }'

修改translog参数app

translog是为了保证数据的一致性,默认每隔5s强制刷新translog日志到磁盘上,为了保证不丢数据每次index/bulk/delete/update的时候必定触发刷新translog到磁盘上,才给请求返回200,若是考虑性能优先能够设置如下参数。curl

curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
  "index.translog.durability" : "async"
}'

副本分片是能够随时调整的,有些较大的索引,能够在作optimize前,先把副本所有取消掉,登optimize完后,再从新开启副本。jvm

curl -XPUT http://127.0.0.1:9200/索引名/_settings -d '
{ "index" : { "number_of_replicas" : 0 }
}'

修改primary shard和replica shard数量async

默认值为每一个indexer5个主分片1个副本分片可根据实际状况进行调整,个人集群目前为6个node,我变成了3个主分片1个副本分片,这样每一个node分配一个shard,好处是节省了空间,坏处是冗余少了,

经过脚本天天凌晨1点自动建立indexer,截取部分脚本以下:

#!/bin/bash
DATE=`date +%Y.%m.%d`

curl -H "Content-Type: application/json;charset=UTF-8" -XPUT http://elasticsearch:9200/logstash-nginx-error-${DATE} -d '
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 1
        }
    }
}'

另外附带几个经常使用的查看集群状态API

查看索引配置
curl -XGET http://127.0.0.1:9200/logstash-*/_settings?pretty

检查集群监控情况
curl -XGET http://127.0.0.1:9200/_cluster/health?pretty

检查节点状态
curl -XGET http://127.0.0.1:9200/_nodes/stats
相关文章
相关标签/搜索