Elasticsearch 的配置目錄 config/ 下包含兩個核心文件:
本文聚焦 elasticsearch.yml 的生產環境關鍵參數調優。
cluster.name: "production-cluster" # 集群唯一標識,防止誤加入其他集群 node.name: "es-master-01" # 節點名稱,建議使用主機名+角色 node.master: true # 是否具備 Master 選舉資格 node.data: true # 是否存儲數據(純 Master 節點設 false)
index.number_of_shards: 5 # 默認主分片數,建議根據數據量規劃(如 1GB/分片) index.number_of_replicas: 1 # 副本數,生產環境至少 1,高可用建議 2
注意:分片數創建後不可變更(除非 reindex),副本數可動態調整。
path.data: /data/elasticsearch/data1,/data/elasticsearch/data2 # 多數據目錄提升 I/O path.logs: /var/log/elasticsearch path.plugins: /usr/share/elasticsearch/plugins path.repo: /mnt/backup/snapshots # 快照倉庫路徑
bootstrap.memory_lock: true # 鎖定物理內存,防止 Swap 導致性能抖動
配合環境變量:
export ES_JAVA_OPTS="-Xms4g -Xmx4g" # 堆內存設為物理內存 50% 以內,且 min=max ulimit -l unlimited # Linux 允許進程鎖內存
network.host: 192.168.1.100 # 綁定具體 IP,生產禁用 0.0.0.0 http.port: 9200 # REST API 端口 transport.tcp.port: 9300 # 節點間通信端口 transport.tcp.compress: true # 啟用 TCP 壓縮,跨機房必開 http.max_content_length: 100mb
discovery.seed_hosts: ["192.168.1.100:9300", "192.168.1.101:9300"] # 種子節點 cluster.initial_master_nodes: ["es-master-01", "es-master-02"] # 7.x+ 必填 discovery.zen.minimum_master_nodes: 2 # 6.x 防腦裂公式:(master資格節點/2)+1
thread_pool.write.queue_size: 10000 # 寫入隊列,防止突發流量拒絕 thread_pool.search.queue_size: 5000 # 搜索隊列 thread_pool.get.queue_size: 1000 # 獲取隊列
index.search.slowlog.threshold.query.warn: 10s index.search.slowlog.threshold.query.info: 5s index.search.slowlog.threshold.query.debug: 2s index.search.slowlog.threshold.query.trace: 500ms index.search.slowlog.threshold.fetch.warn: 1s index.search.slowlog.threshold.fetch.info: 800ms index.search.slowlog.threshold.fetch.debug: 500ms index.search.slowlog.threshold.fetch.trace: 200ms
建議生產環境開啟 warn 級別,定期分析慢查詢優化 DSL。
indices.breaker.total.limit: 70% # 總熔斷閾值 indices.breaker.request.limit: 60% # 請求熔斷 indices.recovery.max_bytes_per_sec: 40mb # 恢復限速,避免佔滿帶寬 cluster.routing.allocation.node_concurrent_recoveries: 2
cluster.name 唯一且有業務含義bootstrap.memory_lock: true + JVM 堆內存固定network.host 綁定內網 IP,http.port 防火牆限制訪問discovery.seed_hosts 與 initial_master_nodes