Elasticsearch配置详解
大约 4 分钟ELK日志收集技术Elasticsearch配置管理
Elasticsearch配置详解
Elasticsearch配置文件
Elasticsearch的主要配置文件位于$ES_HOME/config/
目录下,包括:
- elasticsearch.yml:主配置文件
- jvm.options:JVM配置文件
- log4j2.properties:日志配置文件
elasticsearch.yml配置详解
1. 集群配置
基本集群设置:
# 集群名称
cluster.name: my-elasticsearch-cluster
# 节点名称
node.name: node-1
# 节点角色配置
node.master: true
node.data: true
node.ingest: true
node.ml: false
# 节点属性
node.attr.rack: r1
集群发现配置:
# 种子节点列表
discovery.seed_hosts: ["host1:9300", "host2:9300", "host3:9300"]
# 初始主节点列表
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
# 发现超时时间
discovery.zen.ping_timeout: 3s
# 最小主节点数
discovery.zen.minimum_master_nodes: 2
2. 网络配置
网络绑定设置:
# 网络绑定地址
network.host: 0.0.0.0
# HTTP端口
http.port: 9200
# 传输端口
transport.port: 9300
# 绑定特定网络接口
network.bind_host: 192.168.1.100
network.publish_host: 192.168.1.100
HTTP配置:
# 启用HTTP
http.enabled: true
# HTTP最大内容长度
http.max_content_length: 100mb
# HTTP压缩
http.compression: true
http.compression_level: 3
3. 路径配置
# 数据存储路径
path.data: /var/lib/elasticsearch
# 日志存储路径
path.logs: /var/log/elasticsearch
# 插件存储路径
path.plugins: /usr/share/elasticsearch/plugins
# 配置文件路径
path.conf: /etc/elasticsearch
4. 内存配置
# 锁定内存
bootstrap.memory_lock: true
# 直接内存访问
node.processors: 4
# 字段数据缓存
indices.fielddata.cache.size: 20%
5. 索引配置
默认索引设置:
# 默认分片数
index.number_of_shards: 5
# 默认副本数
index.number_of_replicas: 1
# 索引刷新间隔
index.refresh_interval: 30s
# 索引块设置
index.blocks.read_only_allow_delete: false
索引模板配置:
# 索引映射总数限制
indices.query.bool.max_clause_count: 1024
# 索引字段总数限制
indices.mapping.total_fields.limit: 1000
6. 安全配置
基本安全设置:
# 启用安全功能
xpack.security.enabled: true
# 启用传输层SSL
xpack.security.transport.ssl.enabled: true
# 启用HTTP SSL
xpack.security.http.ssl.enabled: true
# 启用审计日志
xpack.security.audit.enabled: true
SSL配置:
# 传输层SSL配置
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.key: /etc/elasticsearch/certs/elastic-certificates.p12
xpack.security.transport.ssl.certificate: /etc/elasticsearch/certs/elastic-certificates.p12
xpack.security.transport.ssl.certificate_authorities: ["/etc/elasticsearch/certs/elastic-stack-ca.crt"]
# HTTP SSL配置
xpack.security.http.ssl.key: /etc/elasticsearch/certs/elastic-certificates.p12
xpack.security.http.ssl.certificate: /etc/elasticsearch/certs/elastic-certificates.p12
xpack.security.http.ssl.certificate_authorities: ["/etc/elasticsearch/certs/elastic-stack-ca.crt"]
7. 性能配置
线程池配置:
# 搜索线程池
thread_pool.search.size: 10
thread_pool.search.queue_size: 1000
# 写入线程池
thread_pool.write.size: 10
thread_pool.write.queue_size: 1000
# 批量操作线程池
thread_pool.bulk.size: 10
thread_pool.bulk.queue_size: 500
缓存配置:
# 字段数据缓存
indices.fielddata.cache.size: 40%
# 查询缓存
indices.queries.cache.size: 10%
# 请求缓存
indices.requests.cache.size: 1%
jvm.options配置详解
1. 基本JVM设置
# 堆内存设置(建议不超过物理内存的50%)
-Xms2g
-Xmx2g
# 启用G1垃圾收集器(推荐)
-XX:+UseG1GC
-XX:G1ReservePercent=25
-XX:InitiatingHeapOccupancyPercent=30
# 堆内存回收设置
-XX:MaxGCPauseMillis=200
2. JVM系统属性
# 网络地址缓存
-Djava.net.preferIPv4Stack=true
# 文件描述符限制
-Dio.netty.tryReflectionSetAccessible=true
# 日志管理
-Dlog4j2.formatMsgNoLookups=true
3. JVM调试选项
# 启用JMX监控
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
# GC日志
-Xlog:gc*,gc+age=trace,safepoint:gc.log:utctime,pid,tags:filecount=32,filesize=64m
log4j2.properties配置详解
1. 日志级别配置
# 根日志级别
rootLogger.level = info
# 包级别日志
logger.action.name = org.elasticsearch.action
logger.action.level = debug
logger.cluster.name = org.elasticsearch.cluster
logger.cluster.level = debug
2. Appender配置
# 控制台输出
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%m%n
# 文件输出
appender.rolling.type = RollingFile
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
高级配置选项
1. 分片分配配置
# 分片分配策略
cluster.routing.allocation.enable: all
# 新分片恢复并发数
cluster.routing.allocation.node_concurrent_recoveries: 2
# 分片平衡策略
cluster.routing.allocation.balance.shard: 0.45f
cluster.routing.allocation.balance.index: 0.55f
cluster.routing.allocation.balance.threshold: 1.0f
2. 网关配置
# 网关恢复配置
gateway.recover_after_nodes: 3
gateway.recover_after_time: 5m
gateway.expected_nodes: 3
3. 网络配置
# 网络超时设置
network.tcp.keep_alive: true
network.tcp.no_delay: true
network.tcp.reuse_address: true
network.tcp.send_buffer_size: 64k
network.tcp.receive_buffer_size: 64k
4. HTTP配置
# HTTP连接设置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization
# HTTP最大初始行长度
http.max_initial_line_length: 4kb
# HTTP最大头大小
http.max_header_size: 8kb
配置最佳实践
1. 内存配置最佳实践
# 堆内存设置建议
-Xms4g
-Xmx4g
# 禁用swap
bootstrap.memory_lock: true
# G1垃圾收集器
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
2. 索引配置最佳实践
# 合理设置分片数
index.number_of_shards: 5
index.number_of_replicas: 1
# 优化刷新间隔
index.refresh_interval: 30s
# 预热全局序数
index.translog.durability: async
3. 网络配置最佳实践
# 绑定特定IP
network.host: 192.168.1.100
# 启用HTTP压缩
http.compression: true
# 配置CORS
http.cors.enabled: true
http.cors.allow-origin: "*"
4. 安全配置最佳实践
# 启用安全功能
xpack.security.enabled: true
# 配置SSL
xpack.security.transport.ssl.enabled: true
xpack.security.http.ssl.enabled: true
# 启用审计
xpack.security.audit.enabled: true
配置验证与调试
1. 配置验证
# 测试配置文件语法
./bin/elasticsearch -t
# 检查节点信息
curl -X GET "localhost:9200/_nodes/settings?pretty"
# 检查集群设置
curl -X GET "localhost:9200/_cluster/settings?pretty"
2. 动态配置更新
# 更新集群设置
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"persistent": {
"indices.recovery.max_bytes_per_sec": "50mb"
}
}
'
# 更新瞬时设置
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient": {
"cluster.routing.allocation.enable": "none"
}
}
'
常见配置问题
1. 内存相关问题
# 错误:内存不足
# 解决:调整堆内存大小
-Xms2g
-Xmx2g
# 错误:无法锁定内存
# 解决:禁用内存锁定或调整系统设置
bootstrap.memory_lock: false
2. 网络相关问题
# 错误:无法绑定地址
# 解决:检查网络配置
network.host: 0.0.0.0
# 错误:节点无法发现
# 解决:检查种子节点配置
discovery.seed_hosts: ["host1:9300", "host2:9300"]
3. 安全相关问题
# 错误:认证失败
# 解决:检查安全配置
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
总结
Elasticsearch的配置管理是确保集群稳定运行和高性能的关键。通过合理配置集群参数、网络设置、内存管理、安全选项等,可以构建一个高效、安全、可靠的Elasticsearch集群。在实际应用中,需要根据业务需求和硬件环境进行相应的调优和配置。