TL;DR

  • 场景: 三节点 Elasticsearch 集群部署,实现高可用与水平扩展
  • 结论: 集群模式提供数据分片与副本机制,确保服务高可用
  • 产出: 完整集群部署流程、配置说明、错误速查

版本矩阵与环境准备

组件版本
Elasticsearch7.3.0 (linux-x86_64 tar.gz)
节点h121 / h122 / h123
运行用户es_server(非 root)

目录与权限设置

三台机器执行

mkdir -p /opt/servers/es/{data,logs}
chown -R es_server /opt/servers/es
chown -R es_server /opt/servers/elasticsearch-7.3.0

用户创建与权限

useradd es_server
passwd es_server

# 在 /etc/sudoers 添加:es ALL=(ALL) ALL

核心配置 elasticsearch.yml

h121 节点配置

cluster.name: wzkicu-es
node.name: h121.wzk.icu
path.data: /opt/servers/es/data
path.logs: /opt/servers/es/logs
network.host: h121.wzk.icu
network.bind_host: h121.wzk.icu
network.publish_host: 114.115.221.144
http.port: 9200
cluster.initial_master_nodes: ["h121.wzk.icu","h122.wzk.icu","h123.wzk.icu"]
discovery.seed_hosts: ["h121.wzk.icu", "h122.wzk.icu","h123.wzk.icu"]

h122 节点配置

cluster.name: wzkicu-es
node.name: h122.wzk.icu
path.data: /opt/servers/es/data
path.logs: /opt/servers/es/logs
network.host: h122.wzk.icu
network.bind_host: h122.wzk.icu
network.publish_host: 114.115.221.145
http.port: 9200
cluster.initial_master_nodes: ["h121.wzk.icu","h122.wzk.icu","h123.wzk.icu"]
discovery.seed_hosts: ["h121.wzk.icu", "h122.wzk.icu","h123.wzk.icu"]

h123 节点配置

cluster.name: wzkicu-es
node.name: h123.wzk.icu
path.data: /opt/servers/es/data
path.logs: /opt/servers/es/logs
network.host: h123.wzk.icu
network.bind_host: h123.wzk.icu
network.publish_host: 114.115.221.146
http.port: 9200
cluster.initial_master_nodes: ["h121.wzk.icu","h122.wzk.icu","h123.wzk.icu"]
discovery.seed_hosts: ["h121.wzk.icu", "h122.wzk.icu","h123.wzk.icu"]

系统参数配置

修改 sysctl.conf

vim /etc/sysctl.conf

# 添加
vm.max_map_count=655360

执行生效:

sysctl -p

修改 limits.conf

vim /etc/security/limits.conf

# 添加
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096

JVM 内存配置

修改 jvm.options

vim /opt/servers/elasticsearch-7.3.0/config/jvm.options

# 设置
-Xms2g
-Xmx2g

分发与启动

分发配置

# 将配置好的 ES 分发到各节点
scp -r /opt/servers/elasticsearch-7.3.0 es_server@h122.wzk.icu:/opt/servers/
scp -r /opt/servers/elasticsearch-7.3.0 es_server@h123.wzk.icu:/opt/servers/

各节点启动

su es_server
/opt/servers/elasticsearch-7.3.0/bin/elasticsearch -d

验证集群状态

使用 Elasticsearch Head 工具

访问 http://h121.wzk.icu:9200/_plugin/head/ 或使用 curl:

# 集群健康
curl -XGET 'http://h121.wzk.icu:9200/_cluster/health?pretty'

# 节点列表
curl -XGET 'http://h121.wzk.icu:9200/_cat/nodes?v'

# 分片状态
curl -XGET 'http://h121.wzk.icu:9200/_cat/shards?v'

错误速查表

症状根因修复
vm.max_map_count too low未设置内核参数/etc/sysctl.conf 添加 vm.max_map_count=655360
max file descriptors too lowlimits 未生效limits.conf 设置 nofile 65536
ES 拒绝 root 运行以 root 启动切换为 es_server 用户
节点启动但不入集群9300 未通/主机名解析失败检查 discovery.seed_hosts 与网络
node name already in use多节点 node.name 重复为每个节点分配唯一名称
集群 Red分片分配失败检查磁盘空间、节点状态
脑裂问题Master 节点选举分歧调整 minimum_master_nodes