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"
相关推荐
南讯股份Nascent39 分钟前
洽洽全域会员项目启动会圆满召开
大数据·人工智能
大郭鹏宇43 分钟前
基于 LangGraph 构建智能分诊系统(一):项目概述与环境搭建
大数据·人工智能·microsoft·langchain
阿里云大数据AI技术4 小时前
阿里云 ES AI 引擎版:面向 Agent 场景,为亿级租户、千亿规模向量设计的搜索引擎
人工智能·elasticsearch·agent
天天爱吃肉82184 小时前
【电源拆解:跟着问答逐一认识开关适配器PCB元器件】
大数据·人工智能·python·功能测试·嵌入式硬件·汽车
阿里云大数据AI技术4 小时前
FalconSeek 技术解析:阿里云 Elasticsearch 云原生内核如何让查询性能飙升600%
人工智能·elasticsearch
Elastic 中国社区官方博客5 小时前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
大数据·人工智能·elasticsearch·搜索引擎·容器·kubernetes·grafana
abcy0712135 小时前
storm核心实时机制
大数据·storm
灵机一物5 小时前
合伙制律所分红和提成律师个税怎么合规优化?
大数据·人工智能
Elasticsearch7 小时前
使用重新设计的 AutoOps 更快地进行 Elasticsearch 问题排查
elasticsearch
REST_30277 小时前
告别Excel孤岛:一款任务链路可视化工具带来的真实改变
大数据·微服务·云计算·编辑器·excel·动态规划