Elasticsearch集群模式保姆级教程

发布于:2025-02-10 ⋅ 阅读:(44) ⋅ 点赞:(0)

一. 前期准备

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

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

mkdir -p /export/server

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

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

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

二. 安装 Elasticsearch

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

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 :

vim /export/server/elasticsearch/config/elasticsearch.yml
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 选项

sudo vi /export/server/elasticsearch/config/jvm.options

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

-Xms4g
-Xmx4g

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

cd /export/server
scp -r elasticsearch root@node2:/export/server/ 
scp -r elasticsearch root@node3:/export/server/ 

3.4【node2】修改 node2 的 elasticsearch.yml

vi /export/server/elasticsearch/config/elasticsearch.yml
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

vi /export/server/elasticsearch/config/elasticsearch.yml
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 用户

groupadd es
useradd es -g es
cd /export/server
sudo chown es:es -R elasticsearch/

4.2 设置系统资源限制

 vi /etc/security/limits.conf

#文件末尾添加

*               soft    nofile          65536
*               hard    nofile          65536

4.3 设置虚拟内存限制

vi /etc/sysctl.conf

在文件末尾添加:

vm.max_map_count=262145
sysctl -p

4.4 设置环境变量

sudo vi /etc/profile
export ES_JAVA_HOME=/export/server/elasticsearch/jdk
export PATH=$ES_JAVA_HOME/bin:$PATH

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

vi /etc/systemd/system/elasticsearch.service
[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 方式一:手动启动

su es
cd /export/server/elasticsearch/bin
./elasticsearch -d

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

# 启动elasticsearch
systemctl start elasticsearch
# 查看elasticsearch状态
systemctl status elasticsearch
# 设置开机自启动
systemctl enable elasticsearch

六. 查看服务状态

6.1 查看启动是否成功

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

curl http://内网ip:9200

6.2 检查 Elasticsearch 集群的健康状态

curl -X GET "内网ip:9200/_cat/health?v"

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

curl -X GET "http://内网ip:9200/_cat/nodes?v"


网站公告

今日签到

点亮在社区的每一天
去签到