Liunx部署ES单机集群

发布于:2025-05-28 ⋅ 阅读:(24) ⋅ 点赞:(0)

ES 7.17.26 为例

一、单机

下载ES安装包

下载地址

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.26-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.26-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-7.17.26-linux-x86_64.tar.gz.sha512 
tar -xzf elasticsearch-7.17.26-linux-x86_64.tar.gz
cd elasticsearch-7.17.26/ 

修改配置

vim elasticsearch.yml

#开启远程访问  
network.host: 0.0.0.0
#单节点模式   初学者建议设置为此模式 
discovery.type: single-node

后台启动

./bin/elasticsearch -d -p pid

验证

http://192.168.65.174:9200

关闭ES

pkill -F pid

# 或
# 查找进程ID
pgrep -f 'elasticsearch'  # 输出类似 "12345"

# 终止进程(先尝试正常退出)
kill 12345

# 若未响应则强制终止(慎用)
kill -9 12345

二、集群

创建用户

adduser es
passwd es

修改host

切换到root用户,修改/etc/hosts

vim  /etc/hosts
192.168.65.174 es-node1  
192.168.65.192 es-node2  
192.168.65.204 es-node3  

修改elasticsearch.yml

node.name: node-1
cluster.name: es-cluster
node.master: true
node.data: true
network.host: 0.0.0.0
 # 如果服务器存在 Docker 或其他虚拟网卡,需在配置中 排除无关网卡:
 # 强制传输层使用 ens-33
ransport.host: _ens33:ipv4_  
discovery.seed_hosts: ["es-node1", "es-node2", "es-node3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
http.cors.enabled: true
http.cors.allow-origin: "*"

node.name: node-2
cluster.name: es-cluster
node.master: true
node.data: true
network.host: 0.0.0.0
ransport.host: _ens33:ipv4_
discovery.seed_hosts: ["es-node1", "es-node2", "es-node3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
http.cors.enabled: true
http.cors.allow-origin: "*"


node.name: node-3
cluster.name: es-cluster
node.master: true
node.data: true
network.host: 0.0.0.0
ransport.host: _ens33:ipv4_  
discovery.seed_hosts: ["es-node1", "es-node2", "es-node3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
http.cors.enabled: true
http.cors.allow-origin: "*"

启动ES服务

bin/elasticsearch -d 

验证集群

http://192.168.65.174:9200/_cat/nodes?pretty
==>
192.168.235.134 37 95 0 0.00 0.01 0.00 cdfhilmrstw - node-1
192.168.235.136 11 70 0 0.15 0.05 0.02 cdfhilmrstw - node-3
192.168.235.135 56 20 0 0.03 0.02 0.00 cdfhilmrstw * node-2

三、配置开机自启

通过Systemd服务(推荐,适用于CentOS 7+/Ubuntu 16.04+)

1. 创建systemd服务文件
在用户es下, 新建一个.service文件,例如es-cluster.service:

sudo vim /etc/systemd/system/es-cluster.service

添加以下内容(根据实际路径和用户调整):

[Unit]
Description=Elasticsearch Cluster Startup Script
 # 确保网络就绪后启动
After=network.target

[Service]
Type=forking
# 建议使用非root用户
User=es
# 用户所属组
Group=es    
 # 设置工作目录
WorkingDirectory=/home/es/elasticsearch-7.17.26
ExecStart=/home/es/elasticsearch-7.17.26/start.sh
 # 避免超时误判
TimeoutStartSec=0
 # 失败时自动重启
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • start.sh (使用绝对路径)

#!/bin/bash
# 启动 ES(使用绝对路径)
/home/es/elasticsearch-7.17.26/bin/elasticsearch -d

2. 设置脚本权限与服务配置

  • 确保start.sh有执行权限:
sudo chmod +x /home/es/elasticsearch-7.17.26/start.sh
  • 重载systemd配置并启用服务:
sudo systemctl daemon-reload
sudo systemctl enable es-cluster.service

3. 验证与操作

启动服务:sudo systemctl start es-cluster
查看状态:systemctl status es-cluster
查看日志:journalctl -u es-cluster -b(-b表示仅本次启动日志)

网站公告

今日签到

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