Elasticsearch 安装指南:单机与分布式部署详解
Elasticsearch(ES)的安装部署是使用其功能的基础,本文将基于 RPM 包方式,详细讲解单机环境和分布式集群的安装步骤、配置调整及常见问题解决,适用于 CentOS/RHEL 系统。
安装前准备
环境要求
操作系统:CentOS 7/8 或 RHEL 7/8(64 位)。
Java 环境:ES 依赖 Java,需安装 JDK 8 或 11(推荐 11,ES 7.x+ 对 JDK 11 支持更优)。
验证 Java 环境:
1
| java -version # 需输出 java version "1.8.0_xxx" 或 "11.0.xxx"
|
用户权限:ES 不允许 root 用户直接运行,建议创建专用用户(如 elasticsearch)。
导入 Elastic 官方密钥
1
| rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
|
单机安装步骤
下载并安装 RPM 包
以 ES 7.1.1 为例(可替换为其他版本,如 7.17.x 长期支持版):
1 2 3 4 5
| # 下载 RPM 包 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.1.1-x86_64.rpm
# 安装 rpm -ivh elasticsearch-7.1.1-x86_64.rpm
|
安装后,ES 相关文件路径:
- 配置文件:
/etc/elasticsearch/elasticsearch.yml
- 日志文件:
/var/log/elasticsearch/
- 数据目录:
/var/lib/elasticsearch/
- 系统服务:
/usr/lib/systemd/system/elasticsearch.service
解决常见启动错误
错误 1:虚拟内存限制过低
启动时报错:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决:
1 2 3 4 5
| # 修改系统配置 echo "vm.max_map_count=262144" >> /etc/sysctl.conf
# 生效配置 sysctl -p
|
错误 2:文件权限问题
若启动失败,可能是权限不足,需确保 ES 目录归 elasticsearch 用户所有:
1 2
| chown -R elasticsearch:elasticsearch /var/lib/elasticsearch/ chown -R elasticsearch:elasticsearch /var/log/elasticsearch/
|
配置调整(elasticsearch.yml)
1 2 3 4 5 6 7 8 9
| network.host: 0.0.0.0
discovery.type: single-node
|
启动并验证
启动服务
1 2 3 4 5 6 7 8
| # 启动 systemctl start elasticsearch
# 设置开机自启 systemctl enable elasticsearch
# 查看状态(确保为 active (running)) systemctl status elasticsearch
|
查看日志(排查问题用)
1 2 3 4 5
| # 查看 ES 服务日志 journalctl -u elasticsearch -f # -f 实时跟踪日志
# 查看最近 50 行日志 journalctl -u elasticsearch -n 50
|
验证安装成功
通过 HTTP 请求测试(默认端口 9200):
1
| curl http://localhost:9200?pretty
|
成功响应如下(包含版本、集群名称等信息):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "name" : "node-1", "cluster_name" : "elasticsearch", "cluster_uuid" : "xxxxxxxxxxxx", "version" : { "number" : "7.1.1", "build_flavor" : "default", "build_type" : "rpm", "build_hash" : "7a013de", "build_date" : "2019-05-23T14:04:00.380842Z", "build_snapshot" : false, "lucene_version" : "8.0.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
|
分布式集群安装(一主二从)
集群规划
- 主节点(master):负责集群元数据管理,不存储数据(可选)。
- 数据节点(data):存储数据并处理查询请求。
- 示例架构:1 主节点(9200 端口)+ 2 从节点(9201、9202 端口,伪分布式部署在同一台机器)。
主节点配置(elasticsearch.yml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| cluster.name: es-cluster
node.name: master-node
node.master: true
node.data: false
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["localhost:9200"]
cluster.initial_master_nodes: ["master-node"]
|
从节点 1 配置(elasticsearch.yml,端口 9201)
1 2 3 4 5 6 7 8 9 10 11
| cluster.name: es-cluster node.name: data-node-1 node.master: false node.data: true
network.host: 0.0.0.0 http.port: 9201
discovery.seed_hosts: ["localhost:9200"] cluster.initial_master_nodes: ["master-node"]
|
从节点 2 配置(elasticsearch.yml,端口 9202)
1 2 3 4 5 6 7 8 9 10
| cluster.name: es-cluster node.name: data-node-2 node.master: false node.data: true
network.host: 0.0.0.0 http.port: 9202
discovery.seed_hosts: ["localhost:9200"] cluster.initial_master_nodes: ["master-node"]
|
启动集群并验证
启动所有节点
1 2 3 4 5 6 7 8
| # 主节点 systemctl start elasticsearch # 若使用默认服务名,需为从节点创建独立服务(伪分布式建议手动启动)
# 从节点 1(假设配置文件路径为 /etc/elasticsearch/node1/elasticsearch.yml) elasticsearch -Epath.conf=/etc/elasticsearch/node1 -d
# 从节点 2 elasticsearch -Epath.conf=/etc/elasticsearch/node2 -d
|
查看集群状态
1 2
| # 检查集群健康(green 为正常) curl http://localhost:9200/_cluster/health?pretty
|
成功响应:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "cluster_name" : "es-cluster", "status" : "green", # green:所有分片正常;yellow:副本缺失;red:主分片缺失 "timed_out" : false, "number_of_nodes" : 3, "number_of_data_nodes" : 2, "active_primary_shards" : 0, "active_shards" : 0, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 }
|
查看节点列表
1
| curl http://localhost:9200/_cat/nodes?v
|
输出类似:
1 2 3 4
| ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 127.0.0.1 21 97 1 0.05 0.14 0.18 m * master-node 127.0.0.1 18 97 1 0.05 0.14 0.18 d - data-node-1 127.0.0.1 17 97 1 0.05 0.14 0.18 d - data-node-2
|
(m 表示主节点,d 表示数据节点,* 表示当前主节点)
注意事项
生产环境建议:
- 分布式集群需部署在不同物理机 / 虚拟机,避免单点故障。
- 主节点至少 3 个(奇数),确保选举机制稳定。
- 数据节点根据数据量和查询压力横向扩展。
防火墙配置:
开放 9200(HTTP 端口)和 9300(节点间通信端口):
1 2 3
| firewall-cmd --add-port=9200/tcp --permanent firewall-cmd --add-port=9300/tcp --permanent firewall-cmd --reload
|
版本兼容性:
集群所有节点必须使用相同版本的 ES,避免因版本差异导致通信失败