Elasticsearch集群模式保姆级教程

一. 前期准备

【node1、2、3】创建工作目录

以部署到 /export/server 目录为例:

plain 复制代码
mkdir -p /export/server

【node1、2、3】开放必要端口

确保以下端口在防火墙中开放:

  • 9200:HTTP REST API 端口,用于与 Elasticsearch 集群进行交互。
  • 9300:节点间通信端口,用于 Elasticsearch 节点之间的内部通信。

二. 安装 Elasticsearch

【node1】解压 Elasticsearch,并创建数据目录

plain 复制代码
tar -zxvf elasticsearch-7.17.7-linux-x86_64.tar.gz
mv elasticsearch-7.17.7 /export/server/elasticsearch
mkdir -p /export/server/elasticsearch/data

三. 配置 Elasticsearch

3.1【node1】配置 elasticsearch.yml :

plain 复制代码
vim /export/server/elasticsearch/config/elasticsearch.yml
plain 复制代码
cluster.name: es-cluster
node.name: node-1
node.master: true
node.data: true
path.data: /export/server/elasticsearch/data
path.logs: /export/server/elasticsearch/logs
network.host: 0.0.0.0
discovery.seed_hosts: ["node2", "node3"]
network.tcp.keep_alive: true
network.tcp.no_delay: true
action.destructive_requires_name: true
gateway.recover_after_nodes: 2
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
http.port: 9200
cluster.routing.allocation.cluster_concurrent_rebalance: 16
cluster.routing.allocation.node_concurrent_recoveries: 16
cluster.routing.allocation.node_initial_primaries_recoveries: 16
  • discovery.seed_hosts:发现其他节点的初始列表。这里填写其他节点的 IP 地址或主机名,不包括当前节点。
  • cluster.initial_master_nodes:用于指定在集群启动时哪些节点可以作为初始主节点候选。填写全部节点名称。

3.2【node1】配置 JVM 选项

plain 复制代码
sudo vi /export/server/elasticsearch/config/jvm.options

在文件末尾新建一行添加:

plain 复制代码
-Xms4g
-Xmx4g

3.3【node1】将 elasticsearch目录递归复制到 node2、node3 主机的 /export/server/ 目录下

plain 复制代码
cd /export/server
scp -r elasticsearch root@node2:/export/server/ 
scp -r elasticsearch root@node3:/export/server/ 

3.4【node2】修改 node2 的 elasticsearch.yml

plain 复制代码
vi /export/server/elasticsearch/config/elasticsearch.yml
plain 复制代码
cluster.name: es-cluster
node.name: node-2
node.master: true
node.data: true
path.data: /export/server/elasticsearch/data
path.logs: /export/server/elasticsearch/logs
network.host: 0.0.0.0
discovery.seed_hosts: ["node1", "node3"]
network.tcp.keep_alive: true
network.tcp.no_delay: true
action.destructive_requires_name: true
gateway.recover_after_nodes: 2
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
http.port: 9200
cluster.routing.allocation.cluster_concurrent_rebalance: 16
cluster.routing.allocation.node_concurrent_recoveries: 16
cluster.routing.allocation.node_initial_primaries_recoveries: 16

3.5【node3】修改 node3 的 elasticsearch.yml

plain 复制代码
vi /export/server/elasticsearch/config/elasticsearch.yml
plain 复制代码
cluster.name: es-cluster
node.name: node-3
node.master: true
node.data: true
path.data: /export/server/elasticsearch/data
path.logs: /export/server/elasticsearch/logs
network.host: 0.0.0.0
discovery.seed_hosts: ["node1", "node2"]
network.tcp.keep_alive: true
network.tcp.no_delay: true
action.destructive_requires_name: true
gateway.recover_after_nodes: 2
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
http.port: 9200
cluster.routing.allocation.cluster_concurrent_rebalance: 16
cluster.routing.allocation.node_concurrent_recoveries: 16
cluster.routing.allocation.node_initial_primaries_recoveries: 16

四.【node1、2、3】系统配置

4.1 创建 Elasticsearch 用户

plain 复制代码
groupadd es
useradd es -g es
cd /export/server
sudo chown es:es -R elasticsearch/

4.2 设置系统资源限制

plain 复制代码
 vi /etc/security/limits.conf

#文件末尾添加

*               soft    nofile          65536
*               hard    nofile          65536

4.3 设置虚拟内存限制

plain 复制代码
vi /etc/sysctl.conf

在文件末尾添加:

plain 复制代码
vm.max_map_count=262145
plain 复制代码
sysctl -p

4.4 设置环境变量

plain 复制代码
sudo vi /etc/profile
plain 复制代码
export ES_JAVA_HOME=/export/server/elasticsearch/jdk
export PATH=$ES_JAVA_HOME/bin:$PATH

4.5 创建 Systemd 服务(可选,不需要的话直接执行5.1)

plain 复制代码
vi /etc/systemd/system/elasticsearch.service
plain 复制代码
[Unit]
Description=Elasticsearch
After=network.target
 
[Service]
User=es
Group=es
Environment="ES_JAVA_OPTS=-Xms512m -Xmx512m"
Environment="ES_JAVA_HOME=/export/server/elasticsearch/jdk"
Environment="JAVA_HOME=/export/server/jdk"
ExecStart=/export/server/elasticsearch/bin/elasticsearch
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
Restart=on-failure
LimitMEMLOCK=infinity
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target

五.【node1、2、3】启动 Elasticsearch

5.1 方式一:手动启动

plain 复制代码
su es
cd /export/server/elasticsearch/bin
./elasticsearch -d

5.2 方式二:使用 Systemd 服务 (可选)

plain 复制代码
# 启动elasticsearch
systemctl start elasticsearch
# 查看elasticsearch状态
systemctl status elasticsearch
# 设置开机自启动
systemctl enable elasticsearch

六. 查看服务状态

6.1 查看启动是否成功

项目启动大约需要2-4 分钟时间,要确保项目完全启动后再执行,否则会出现拒绝访问:

plain 复制代码
curl http://内网ip:9200

6.2 检查 Elasticsearch 集群的健康状态

plain 复制代码
curl -X GET "内网ip:9200/_cat/health?v"

6.3 分析 Elasticsearch 集群的状态和各个节点的信息

plain 复制代码
curl -X GET "http://内网ip:9200/_cat/nodes?v"
相关推荐
天诚智能门锁6 分钟前
天诚cat.1人脸公租房智能锁及管控平台助力三门县公租房管理
大数据·人工智能·物联网·智慧城市·公租房
2601_9564141415 分钟前
2026年5月PCB厂家推荐:TOP5榜产品应对5G基站散热挑战
大数据·人工智能·5g
Justice Young20 分钟前
Flink第五章:DataStream API
大数据·flink
千月落23 分钟前
HDFS数据迁移
大数据·hadoop·hdfs
N串41 分钟前
2.4 采购部门——权力来自信息不对称
大数据
南棱笑笑生1 小时前
20260503给万象奥科的开发板HD-RK3576-PI适配瑞芯微原厂的Android14时适配AP6256
大数据·elasticsearch·搜索引擎·rockchip
王莎莎-MinerU1 小时前
从 PDF 到知识资产:MinerU 文档解析如何成为企业 RAG 系统的“数据基石”
大数据·人工智能·pdf·个人开发
缝艺智研社1 小时前
誉财 YC - 21 平板下摆机:服装下摆与袖口加工的卓越之选
大数据·人工智能·自动化·电脑·新人首发·线上模板机
逸Y 仙X2 小时前
文章二十:Elasticsearch高亮搜索完全指南
java·大数据·运维·elasticsearch·搜索引擎·全文检索
2601_956139422 小时前
集团品牌全案公司哪家专业
大数据·人工智能·python