文章目录
- [openEuler 上部署 Elasticsearch:单节点与集群方式](#openEuler 上部署 Elasticsearch:单节点与集群方式)
-
- 环境准备(所有部署方式都需要)
-
- [1. 系统检查](#1. 系统检查)
- [2. 安装 Java 11(Elasticsearch 7.x 需要)](#2. 安装 Java 11(Elasticsearch 7.x 需要))
- [3. 系统参数配置(重要!)](#3. 系统参数配置(重要!))
- [4. 下载 Elasticsearch 7.17.10(官方推荐版本)](#4. 下载 Elasticsearch 7.17.10(官方推荐版本))
- [5. 解压安装包](#5. 解压安装包)
- 单节点部署
-
- [1. 配置 Elasticsearch](#1. 配置 Elasticsearch)
- [2. 设置环境变量](#2. 设置环境变量)
- [3. 启动 Elasticsearch](#3. 启动 Elasticsearch)
- [4. 验证单节点运行](#4. 验证单节点运行)
- 集群部署(3节点示例)
-
- [1. 准备3个节点](#1. 准备3个节点)
- [2. 配置每个节点的 elasticsearch.yml](#2. 配置每个节点的 elasticsearch.yml)
-
- [节点 1 (192.168.100.160)](#节点 1 (192.168.100.160))
- [节点 2 (192.168.100.161)](#节点 2 (192.168.100.161))
- [节点 3 (192.168.100.162)](#节点 3 (192.168.100.162))
- [3. 启动集群节点](#3. 启动集群节点)
- [4. 验证集群状态](#4. 验证集群状态)
- 安全配置(推荐)
-
- [步骤 1:生成SSL证书(使用elasticsearch-certutil)](#步骤 1:生成SSL证书(使用elasticsearch-certutil))
- [步骤 2:配置elasticsearch.yml](#步骤 2:配置elasticsearch.yml)
- [步骤 3:设置权限](#步骤 3:设置权限)
- [步骤 4:重启Elasticsearch](#步骤 4:重启Elasticsearch)
- [步骤 5:设置管理员密码](#步骤 5:设置管理员密码)
- 验证SSL配置
- 证书配置常见问题
-
- [1. 证书过期问题](#1. 证书过期问题)
- [2. 证书IP地址不匹配](#2. 证书IP地址不匹配)
- [3. 证书路径错误](#3. 证书路径错误)
- [额外建议:创建 systemd 服务(推荐)](#额外建议:创建 systemd 服务(推荐))
-
- [1. 创建服务文件](#1. 创建服务文件)
- [2. 添加以下内容](#2. 添加以下内容)
- [3. 启动并启用服务](#3. 启动并启用服务)
- [**4. 验证服务状态**](#4. 验证服务状态)
- 常见问题解决
-
- [1. 内存不足问题](#1. 内存不足问题)
- [2. 端口冲突](#2. 端口冲突)
- [3. 集群发现失败](#3. 集群发现失败)
- [4. GeoIP 数据库下载失败](#4. GeoIP 数据库下载失败)
- 集群管理命令
-
- [1. 查看集群状态](#1. 查看集群状态)
- [2. 查看节点信息](#2. 查看节点信息)
- [3. 创建索引](#3. 创建索引)
- 集群环境注意事项
- 总结
openEuler 上部署 Elasticsearch:单节点与集群方式
环境准备(所有部署方式都需要)
1. 系统检查
bash
# 确认 openEuler 版本
cat /etc/os-release
# 确认系统架构
uname -m
# 创建用户,Elasticsearch 不允许root启动(不创建家目录,不设置 shell)
sudo useradd -r -s /bin/false elasticsearch
# 确认用户已创建
id elasticsearch
# 预期输出:uid=1001(elasticsearch) gid=1001(elasticsearch) groups=1001(elasticsearch)
2. 安装 Java 11(Elasticsearch 7.x 需要)
bash
# 安装 OpenJDK 11
sudo dnf install -y java-11-openjdk-devel
# 验证 Java 版本
java -version
3. 系统参数配置(重要!)
bash
# 临时设置(立即生效)
sudo sysctl -w vm.max_map_count=262144
# 永久设置
echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# 限制文件描述符
echo "* soft nofile 65536" | sudo tee -a /etc/security/limits.conf
echo "* hard nofile 65536" | sudo tee -a /etc/security/limits.conf
echo "* soft memlock unlimited" | sudo tee -a /etc/security/limits.conf
echo "* hard memlock unlimited" | sudo tee -a /etc/security/limits.conf
# 重启系统使配置生效
sudo reboot
4. 下载 Elasticsearch 7.17.10(官方推荐版本)
bash
# 创建工作目录
mkdir -p /data/elasticsearch
cd /data/elasticsearch
# 下载官方安装包(确保下载完整)
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.10-linux-x86_64.tar.gz
5. 解压安装包
bash
tar -xzf elasticsearch-7.17.10-linux-x86_64.tar.gz
mv elasticsearch-7.17.10 elasticsearch
单节点部署
1. 配置 Elasticsearch
bash
# 编辑配置文件
sudo vim /data/elasticsearch/elasticsearch/config/elasticsearch.yml
添加/修改以下配置:
yaml
# 设置节点名称
node.name: "single-node"
# 设置集群名称(单节点不需要集群名,但需与集群模式一致)
cluster.name: "elasticsearch"
# 设置绑定地址(仅本地访问)
network.host: 0.0.0.0
# 设置 HTTP 端口
http.port: 9200
# 设置集群发现(单节点不需要)
discovery.seed_hosts: []
cluster.initial_master_nodes: []
2. 设置环境变量
bash
# 创建环境变量文件
sudo vim /etc/profile.d/elasticsearch.sh
# 添加以下内容
export ES_HOME=/data/elasticsearch/elasticsearch
export PATH=$PATH:$ES_HOME/bin
3. 启动 Elasticsearch
bash
# 使环境变量生效
source /etc/profile.d/elasticsearch.sh
# 确保 Elasticsearch 安装目录的权限正确
chown -R elasticsearch:elasticsearch /data/elasticsearch
# 以非守护进程方式启动(便于调试)
sudo -u elasticsearch /usr/local/bin/elasticsearch/bin/elasticsearch
# 或以守护进程方式启动(推荐生产环境)
nohup sudo -u elasticsearch /usr/local/bin/elasticsearch/bin/elasticsearch &
4. 验证单节点运行
bash
curl -X GET "localhost:9200/?pretty"
预期输出:
json
{
"name" : "single-node",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "3jXZJ4h2R6qJ3g8QJ4J6gA",
"version" : {
"number" : "7.17.10",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "3c09e4a55b2c5d6d0c3d4b8c2d1d2c3d4b8c2d1d",
"build_date" : "2023-09-11T15:23:59.446814Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
集群部署(3节点示例)
1. 准备3个节点
| IP | 主机名 |
|---|---|
| 192.168.100.160 | node-1 |
| 192.168.100.161 | node-1 |
| 192.168.100.162 | node-3 |
在每个节点上执行以下步骤(假设使用相同安装路径):
bash
# 在所有节点上执行
sudo mkdir -p /data/elasticsearch
cd /data/elasticsearch
sudo wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.10-linux-x86_64.tar.gz
sudo tar -xzf elasticsearch-7.17.10-linux-x86_64.tar.gz
sudo mv elasticsearch-7.17.10 elasticsearch
2. 配置每个节点的 elasticsearch.yml
节点 1 (192.168.100.160)
yaml
# vim /data/elasticsearch/elasticsearch/config/elasticsearch.yml
node.name: "node-1"
cluster.name: "elasticsearch-cluster"
network.host: 192.168.100.160
http.port: 9200
discovery.seed_hosts: ["192.168.100.160", "192.168.100.161", "192.168.100.162"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
# 仅在首次启动时需要 cluster.initial_master_nodes
# 后续启动必须删除此配置或者注释掉:cluster.initial_master_nodes
节点 2 (192.168.100.161)
yaml
# vim /data/elasticsearch/elasticsearch/config/elasticsearch.yml
node.name: "node-2"
cluster.name: "elasticsearch-cluster"
network.host: 192.168.100.161
http.port: 9200
discovery.seed_hosts: ["192.168.100.160", "192.168.100.161", "192.168.100.162"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
# 仅在首次启动时需要 cluster.initial_master_nodes
# 后续启动必须删除此配置或者注释掉:cluster.initial_master_nodes
节点 3 (192.168.100.162)
yaml
# vim /data/elasticsearch/elasticsearch/config/elasticsearch.yml
node.name: "node-3"
cluster.name: "elasticsearch-cluster"
network.host: 192.168.100.162
http.port: 9200
discovery.seed_hosts: ["192.168.100.160", "192.168.100.161", "192.168.100.162"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
# 仅在首次启动时需要 cluster.initial_master_nodes
# 后续启动必须删除此配置或者注释掉:cluster.initial_master_nodes
3. 启动集群节点
在每个节点上启动 Elasticsearch:
bash
# 在每个节点上执行
source /etc/profile.d/elasticsearch.sh
chown -R elasticsearch:elasticsearch /data/elasticsearch
sudo -u elasticsearch /usr/local/bin/elasticsearch/bin/elasticsearch
4. 验证集群状态
在任意一个节点上执行:
bash
curl -X GET "http://192.168.100.160:9200/_cluster/health?pretty"
预期输出:
json
{
"cluster_name" : "elasticsearch-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 0,
"active_shards" : 0,
"recovered_shards" : 0,
"active_shards_percent_as_number" : 100.0
}
安全配置(推荐)
步骤 1:生成SSL证书(使用elasticsearch-certutil)
bash
# 1. 生成CA证书
[root@node-1 ~]# cd /usr/local/bin/elasticsearch/
[root@node-1 elasticsearch]# ./bin/elasticsearch-certutil ca
...省略N
Please enter the desired output file [elastic-stack-ca.p12]: elastic-stack-ca.p12
Enter password for elastic-stack-ca.p12 : # 设置密码:1qazXSW@3edc
# 2. 生成节点证书(使用之前生成的CA)
[root@node-1 elasticsearch]# ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
...省略N
Enter password for CA (elastic-stack-ca.p12) : # 设置密码:1qazXSW@3edc
Please enter the desired output file [elastic-certificates.p12]: elastic-certificates.p12
Enter password for elastic-certificates.p12 : # 设置密码:1qazXSW@3edc
Certificates written to /usr/local/bin/elasticsearch/elastic-certificates.p12
This file should be properly secured as it contains the private key for
your instance.
This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.
For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.
[root@node-1 elasticsearch]#
生成的证书文件将保存在config/certs/目录下(默认位置),包括:
-
elastic-certificates.p12(PKCS#12格式,包含证书、私钥和CA)
-
elastic-stack-ca.p12(CA证书)
bash
[root@node-1 elasticsearch]# pwd
/usr/local/bin/elasticsearch
[root@node-1 elasticsearch]# mkdir -p config/certs
[root@node-1 elasticsearch]# cp elastic-certificates.p12 elastic-stack-ca.p12 config/certs/
注意:生成的证书默认包含localhost和127.0.0.1,您可能需要根据实际IP地址修改。如果使用自定义IP,可在生成证书时指定。
步骤 2:配置elasticsearch.yml
在/usr/local/bin/elasticsearch/config/elasticsearch.yml中添加以下配置:
yaml
# 启用安全功能
xpack.security.enabled: true
# 配置HTTP层SSL(客户端连接)
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.keystore.password: "1qazXSW@3edc" # 替换为实际密码
# 配置传输层SSL(节点间通信)
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.keystore.password: "1qazXSW@3edc" # 与HTTP层相同
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.client_authentication: required
✅ 重要提示:
certs/elastic-certificates.p12是相对路径,相对于Elasticsearch的配置目录(/usr/local/bin/elasticsearch/config)
如果使用绝对路径,应为/usr/local/bin/elasticsearch/config/certs/elastic-certificates.p12
verification_mode: certificate表示验证节点证书
client_authentication: required要求客户端必须提供有效证书
步骤 3:设置权限
bash
# 确保证书文件权限正确
sudo chmod 640 /usr/local/bin/elasticsearch/config/certs/elastic-certificates.p12
sudo chown elasticsearch:elasticsearch /usr/local/bin/elasticsearch/config/certs/elastic-certificates.p12
步骤 4:重启Elasticsearch
bash
systemctl restart elasticsearch
步骤 5:设置管理员密码
bash
sudo /usr/local/bin/elasticsearch/bin/elasticsearch-setup-passwords auto --url https://127.0.0.1:9200
验证SSL配置
bash
# 使用curl验证HTTPS连接(-k忽略证书验证,仅用于测试)
curl -k -u elastic:1qazXSW@3edc https://localhost:9200
- 预期输出应包含Elasticsearch信息,且不显示SSL错误。
证书配置常见问题
1. 证书过期问题
- 症状:节点间通信失败、集群分裂
- 解决方案 :定期更新证书,使用
elasticsearch-certutil重新生成
2. 证书IP地址不匹配
-
症状 :连接时出现
certificate is not trusted错误 -
解决方案:
-
生成证书时指定正确的IP地址
-
在证书生成过程中,使用 -ip 参数指定IP,例如:
bash./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12 --ip 192.168.100.160
-
3. 证书路径错误
-
症状:启动时显示"Unable to load certificate"
-
解决方案:
1.确认证书路径是否正确
2.使用绝对路径(如
/usr/local/bin/elasticsearch/config/certs/elastic-certificates.p12)3.检查文件权限
额外建议:创建 systemd 服务(推荐)
为更规范地管理 Elasticsearch,创建 systemd 服务:
1. 创建服务文件
bash
sudo vi /etc/systemd/system/elasticsearch.service
2. 添加以下内容
bash
[Unit]
Description=Elasticsearch
After=network.target
[Service]
User=elasticsearch
Group=elasticsearch
WorkingDirectory=/data/elasticsearch/elasticsearch
ExecStart=/data/elasticsearch/elasticsearch/bin/elasticsearch
ExecStop=ps -ef | grep -v grep | grep -v awk | grep elasticsearch | awk '$3==1 {print $2}' | xargs kill -9
Restart=on-failure
[Install]
WantedBy=multi-user.target
3. 启动并启用服务
bash
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
4. 验证服务状态
bash
sudo systemctl status elasticsearch
常见问题解决
1. 内存不足问题
bash
# 编辑 jvm.dataions
sudo vim /data/elasticsearch/elasticsearch/config/jvm.dataions
# 修改以下行(根据系统内存调整)
-Xms2g
-Xmx2g
2. 端口冲突
bash
# 检查端口占用
sudo netstat -tuln | grep 9200
# 停止占用端口的进程
sudo kill -9 <PID>
3. 集群发现失败
- 确保所有节点的
discovery.seed_hosts配置正确 - 确保节点间网络互通(
ping和telnet测试) - 检查防火墙设置(开放 9200、9300 端口)
- 权限问题
chown -R elasticsearch:elasticsearch /data/elasticsearch
4. GeoIP 数据库下载失败
[ERROR][o.e.i.g.GeoIpDownloader] error updating geoip database [GeoLite2-City.mmdb]
java.net.SocketTimeoutException: Connect timed out
- 解决方案:
bash
# 手动下载 GeoIP 数据库
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.10-linux-x86_64.tar.gz
tar -xzf elasticsearch-7.17.10-linux-x86_64.tar.gz
cd elasticsearch-7.17.10
# 复制 GeoIP 数据库到 Elasticsearch 目录,也就是程序的目录
find / -name ingest-geoip 2> /dev/null
# 这是查找出来的路径:/usr/local/bin/elasticsearch/modules/ingest-geoip
ls /usr/local/bin/elasticsearch/modules/ingest-geoip
sudo cp config/ingest-geoip/*.mmdb /usr/local/bin/elasticsearch/config/ingest-geoip/
# 重启 Elasticsearch
sudo systemctl restart elasticsearch
集群管理命令
1. 查看集群状态
bash
curl -X GET "http://localhost:9200/_cluster/health?pretty"
2. 查看节点信息
bash
curl -X GET "http://localhost:9200/_nodes?pretty"
3. 创建索引
bash
curl -X PUT "http://localhost:9200/my-index?pretty"
集群环境注意事项
-
所有节点都需要创建相同用户:
bash# 在 master-1, node-1, master-3 上都执行 sudo useradd -r -s /bin/false elasticsearch -
所有节点的目录权限都需要正确设置:
bash# 在每个节点上执行 sudo chown -R elasticsearch:elasticsearch /data/elasticsearch -
启动命令必须使用
sudo -u elasticsearch:bash# 在每个节点上启动 sudo -u elasticsearch /data/elasticsearch/elasticsearch/bin/elasticsearch
总结
| 部署方式 | 配置关键点 | 适用场景 |
|---|---|---|
| 单节点 | node.name, cluster.name, network.host |
开发测试、小型应用 |
| 集群 | cluster.name, discovery.seed_hosts, cluster.initial_master_nodes |
生产环境、高可用需求 |
重要提示:
- 在生产环境中,必须配置安全功能(xpack.security.enabled: true)
- 集群节点数量建议为奇数(3、5、7),避免脑裂问题
- 每个节点应有独立的
node.name和配置文件 - 确保所有节点的
cluster.name相同