> 技术文档 > Elasticsearch 配置详解_elasticsearch.yml配置文件详解

Elasticsearch 配置详解_elasticsearch.yml配置文件详解


Elasticsearch 配置详解

文章目录

  • Elasticsearch 配置详解
    • 一、核心配置 (elasticsearch.yml)
      • 1. 集群配置
      • 2. 网络配置
      • 3. 路径配置
      • 4. 内存配置
    • 二、重要性能配置
      • 1. 索引相关
      • 2. 线程池配置
      • 3. 缓存配置
    • 三、安全配置
      • 1. 基础安全
      • 2. TLS/SSL配置
    • 四、日志配置 (log4j2.properties)
      • 1. 日志级别
      • 2. 慢查询日志
    • 五、JVM配置 (jvm.options)
      • 1. 内存设置
      • 2. GC设置
    • 六、生产环境建议配置
    • 七、监控与维护配置

Elasticsearch 是一个分布式搜索和分析引擎,其配置项繁多且对性能影响重大。以下是 Elasticsearch 主要配置项的详细说明。

一、核心配置 (elasticsearch.yml)

1. 集群配置

  • cluster.name: 集群名称,同一集群的所有节点必须相同

    cluster.name: my-elasticsearch-cluster
  • node.name: 节点名称,默认为主机名

    node.name: node-1
  • node.roles: 节点角色 (ES 7.9+)

    node.roles: [ master, data, ingest ]

    可选值:master(主节点)、data(数据节点)、ingest(预处理节点)、ml(机器学习)、remote_cluster_client(跨集群客户端)

2. 网络配置

  • network.host: 绑定IP地址

    network.host: 0.0.0.0 # 绑定所有网络接口
  • http.port: HTTP API端口,默认9200

    http.port: 9200
  • transport.port: 节点间通信端口,默认9300

    transport.port: 9300
  • discovery.seed_hosts: 集群发现种子节点列表

    discovery.seed_hosts: [\"host1:9300\", \"host2:9300\"]
  • cluster.initial_master_nodes: 初始主节点列表(首次启动集群时设置)

    cluster.initial_master_nodes: [\"node-1\", \"node-2\"]

3. 路径配置

  • path.data: 数据存储路径,可配置多个路径

    path.data: /var/data/elasticsearch
  • path.logs: 日志存储路径

    path.logs: /var/log/elasticsearch

4. 内存配置

  • bootstrap.memory_lock: 锁定内存防止交换

    bootstrap.memory_lock: true
  • ES_JAVA_OPTS: JVM参数(在jvm.options或环境变量中设置)

    ES_JAVA_OPTS=\"-Xms4g -Xmx4g\"

二、重要性能配置

1. 索引相关

  • indices.query.bool.max_clause_count: 布尔查询最大子句数

    indices.query.bool.max_clause_count: 1024
  • indices.memory.index_buffer_size: 索引缓冲区大小

    indices.memory.index_buffer_size: 10%

2. 线程池配置

  • thread_pool: 各种线程池配置

    thread_pool: write: size: 16 queue_size: 10000 search: size: 20 queue_size: 1000

3. 缓存配置

  • indices.requests.cache.size: 请求缓存大小

    indices.requests.cache.size: 2%
  • indices.fielddata.cache.size: 字段数据缓存大小

    indices.fielddata.cache.size: 30%

三、安全配置

1. 基础安全

  • xpack.security.enabled: 启用安全功能

    xpack.security.enabled: true
  • xpack.security.transport.ssl.enabled: 节点间SSL加密

    xpack.security.transport.ssl.enabled: true

2. TLS/SSL配置

xpack.security.transport.ssl: keystore.path: certs/elastic-certificates.p12 truststore.path: certs/elastic-certificates.p12 verification_mode: certificate

四、日志配置 (log4j2.properties)

1. 日志级别

logger.index_search_slowlog_rolling.name = index.search.slowloglogger.index_search_slowlog_rolling.level = trace

2. 慢查询日志

index.search.slowlog: level: info threshold.query.warn: 10s threshold.query.info: 5s threshold.query.debug: 2s threshold.query.trace: 500ms

五、JVM配置 (jvm.options)

1. 内存设置

-Xms4g-Xmx4g

2. GC设置

-XX:+UseG1GC-XX:MaxGCPauseMillis=400

六、生产环境建议配置

  1. 硬件建议:

    • 64GB内存以下机器:堆内存不超过26GB
    • 使用SSD存储
    • 禁用交换分区
  2. 集群规划:

    • 主节点:3个专用节点(node.roles: [master])
    • 数据节点:根据数据量决定
    • 协调节点:处理客户端请求
  3. 重要参数:

    cluster.routing.allocation.disk.threshold_enabled: truecluster.routing.allocation.disk.watermark.low: 85%cluster.routing.allocation.disk.watermark.high: 90%
  4. 索引设置:

    • 根据数据特点设置合适的分片数(建议每个分片20-50GB)

    • 设置合理的刷新间隔:

      { \"settings\": { \"refresh_interval\": \"30s\" }}

七、监控与维护配置

  1. 监控配置:

    xpack.monitoring.collection.enabled: truexpack.monitoring.exporters: id1: type: http host: [\"http://monitoring-server:9200\"]
  2. 快照配置:

    path.repo: [\"/mount/backups\"]

以上配置需要根据实际业务需求、数据规模和硬件环境进行调整。建议在修改配置前进行充分测试,并监控调整后的性能表现。