1. 环境准备
1.1 服务器规划
节点 IP 地址 主机名 数据路径
node-1 192.168.1.10 es-node-1 /data/es/data
node-2 192.168.1.11 es-node-2 /data/es/data
node-3 192.168.1.12 es-node-3 /data/es/data
说明:IP 地址请根据实际环境修改。三台服务器的数据路径可以不同,这里统一路径仅为方便演示1.2 修改主机名(三台分别执行)
# node-1 上执行
hostnamectl set-hostname es-node-1
# node-2 上执行
hostnamectl set-hostname es-node-2
# node-3 上执行
hostnamectl set-hostname es-node-3
# 验证
hostname1.3 配置 hosts 解析(三台都要执行)
cat >> /etc/hosts << EOF
192.168.1.10 es-node-1
192.168.1.11 es-node-2
192.168.1.12 es-node-3
EOF1.4 关闭防火墙(三台执行)
systemctl stop firewalld
systemctl disable firewalld1.5 配置系统参数(三台执行)
# 1. 设置最大虚拟内存区域数(解决 vm.max_map_count 过小问题)
echo 'vm.max_map_count=262144' >> /etc/sysctl.conf
sysctl -p# 2. 设置文件描述符和进程限制
cat >> /etc/security/limits.conf << EOF
es soft nofile 65535
es hard nofile 65535
es soft nproc 4096
es hard nproc 4096
EOF1.6 安装 JDK(三台执行)
Elasticsearch 7.9.3 需要 JDK 8 或更高版本:
yum install -y java-1.8.0-openjdk
java -version2. 安装 Elasticsearch
说明:以下步骤先在 node-1 执行,然后将安装目录复制到 node-2 和 node-3。
cd /opt
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-linux-x86_64.tar.gz
tar -xzf elasticsearch-7.9.3-linux-x86_64.tar.gz
mv elasticsearch-7.9.3 /usr/local/elasticsearch2.2 创建数据和日志目录(node-1 执行)
mkdir -p /data/es/data
mkdir -p /data/es/logs
数据存储路径可以根据实际磁盘规划调整,不同节点的路径可以不一样2.3 创建专用用户(三台都要创建)
Elasticsearch 不允许使用 root 用户启动,必须创建专用用户
groupadd es
useradd es -g es -p es123
chown -R es:es /usr/local/elasticsearch
chown -R es:es /data/es2.4 配置 elasticsearch.yml(node-1 执行)
su - es
vi /usr/local/elasticsearch/config/elasticsearch.yml# 集群名称(三台必须一致)
cluster.name: my-es-cluster
# 节点名称(每台唯一)
node.name: es-node-1
# 节点角色:既是 master 候选节点,也存储数据
node.master: true
node.data: true
# 绑定地址(0.0.0.0 表示监听所有网卡)
network.host: 0.0.0.0
# HTTP 端口(REST API)
http.port: 9200
# 节点间通信端口
transport.tcp.port: 9300
# 数据和日志路径
path.data: /opt/elasticsearch-cluster/elasticsearch-a/data
path.logs: /opt/elasticsearch-cluster/elasticsearch-a/logs
# 集群发现:种子节点列表(三台节点的 IP:9300)
discovery.seed_hosts:- 192.168.1.10:9300- 192.168.1.11:9300- 192.168.1.12:9300
# 初始主节点候选列表(**仅首次启动时使用**)
cluster.initial_master_nodes:- es-node-1- es-node-2- es-node-3
# 启用跨域(方便 Kibana 等工具连接)
http.cors.enabled: true
http.cors.allow-origin: "*"
# 启用监控
xpack.monitoring.enabled: true
xpack.monitoring.collection.enabled: true重要:cluster.initial_master_nodes 配置仅在集群首次启动时使用,集群形成后应移除此配置cluster.name: my-es-cluster
node.name: es-node-3
node.master: true
node.data: false
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
path.data: /home/elasticsearch-cluster/elasticsearch-c/data
path.logs: /home/elasticsearch-cluster/elasticsearch-c/logs
discovery.seed_hosts:- 192.168.1.10:9300- 192.168.1.11:9300- 192.168.1.12:9300
cluster.initial_master_nodes:- es-node-1- es-node-2- es-node-3
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.monitoring.enabled: true
xpack.monitoring.collection.enabled: true3. 启动集群
3.1 启动各节点(按顺序执行)
# 切换到 es 用户
su - es# 前台启动(用于观察日志,确认无报错)
/usr/local/elasticsearch/bin/elasticsearch# 确认无报错后,改为后台启动
/usr/local/elasticsearch/bin/elasticsearch -d
/opt/elasticsearch-cluster/elasticsearch/bin/elasticsearch3.2 配置 systemd 服务(可选,三台执行)
如果需要将 ES 配置为系统服务:
# 使用 root 用户
cat > /etc/systemd/system/elasticsearch.service << 'EOF'
[Unit]
Description=Elasticsearch
After=network.target[Service]
Type=forking
User=es
Group=es
ExecStart=/home/elasticsearch-cluster/elasticsearch/bin/elasticsearch -d
ExecStop=/bin/kill -TERM $MAINPID
Restart=always
LimitNOFILE=65535
LimitNPROC=4096[Install]
WantedBy=multi-user.target
EOFsystemctl daemon-reload
systemctl enable elasticsearch
systemctl start elasticsearch4. 验证集群
4.1 检查集群健康状态
bash
curl http://192.168.1.10:9200/_cat/health?v
预期输出(status 应为 green 或 yellow):text
epoch timestamp cluster status node.total node.data shards ...
1699999999 00:00:00 es-ha-cluster green 3 3 5 ...