文章目录
前言
版本:Elasticsearch 8.18.2
下载地址:https://www.elastic.co/downloads/elasticsearch
3台机器:
·192.168.0.133
·192.168.0.134
·192.168.0.135
一、参考另一篇文章完成单机安装
单机能启动后看以下内容。
二、修改各节点的elasticsearch.yml
修改为以下内容:
cluster.name: bztc-elasticsearch
node.name: bztc-es-node-1
path.data: /opt/elasticsearch/data
path.logs: /opt/elasticsearch/logs
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["192.168.0.133", "192.168.0.134","192.168.0.135"]
cluster.initial_master_nodes: ["bztc-es-node-1", "bztc-es-node-2", "bztc-es-node-3"]
xpack.security.enabled: false
三、修改JVM 内存(可选)
编辑 jvm.options,修改:
-Xms8g
-Xmx8g
建议设为物理内存的一半(最大不超过 32GB)。
四、3个节点同时启动(用elasticsearch用户启动)
执行:
curl 192.168.0.133:9200
显示:
{
"name" : "bztc-es-node-1",
"cluster_name" : "bztc-elasticsearch",
"cluster_uuid" : "C_SZA6cRTcSvvevECT9k0A",
"version" : {
"number" : "8.18.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "c6b8d8d951c631db715485edc1a74190cdce4189",
"build_date" : "2025-05-23T10:07:06.210694702Z",
"build_snapshot" : false,
"lucene_version" : "9.12.1",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
执行:
curl http://192.168.0.133:9200/_cluster/health?pretty
显示:
{
"cluster_name" : "bztc-elasticsearch",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"unassigned_primary_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
执行:
curl http://192.168.0.133:9200/_cat/nodes?v
显示:
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.0.135 11 84 15 1.26 1.37 0.90 cdfhilmrstw * bztc-es-node-3
192.168.0.134 8 84 12 1.26 1.37 0.90 cdfhilmrstw - bztc-es-node-2
192.168.0.133 7 84 6 1.26 1.37 0.90 cdfhilmrstw - bztc-es-node-1
显示以上信息表示集群启动成功。
五、开启用户认证(安全模式)
5.1 开启 xpack.security(所有节点)
修改每台节点的配置文件
编辑elasticsearch.yml
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true # 集群节点之间通信启用 TLS
# xpack.security.http.ssl.enabled: false # HTTP 不强制 TLS(你也可以设为 true,下面说)
如果你希望 HTTP 层(浏览器、curl)也必须使用 HTTPS,请将 http.ssl.enabled 设置为 true,并配置证书。(见章节六)
5.2 生成默认证书(推荐方式)
Elasticsearch 自带自动 TLS 工具,可为 transport 层 自动生成证书。
你可以执行一次:
cd /opt/elasticsearch-8.18.2
./bin/elasticsearch-certutil ca
./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
会生成 .p12 格式的证书(默认叫 elastic-certificates.p12),你可以将它复制到所有节点,并配置在 elasticsearch.yml 中。
添加配置:
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.keystore.password: <密码>
xpack.security.transport.ssl.truststore.password: <密码>
也可以用 --keep-ca-key 让它保留私钥,以便之后签发更多证书。
5.3 初始化内置用户密码(重要)
必须在所有节点开启 xpack 后执行,且集群状态为 green。
只需要在任意一台节点执行:
cd /opt/elasticsearch-8.18.2
./bin/elasticsearch-reset-password -u elastic
你会看到如下提示:
This tool will reset the password of the [elastic] user to a random value or one you specify.
跟着提示输入你要设的密码,或者按回车生成随机密码。
也可以用这个命令批量初始化所有内置用户(elastic、kibana_system 等):
./bin/elasticsearch-setup-passwords interactive
5.4 重启所有节点
验证是否生效:
curl -u elastic:你的密码 http://localhost:9200
你应该看到返回的 JSON,否则会返回:
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials"}]}}
5.5 问题
config/certs/目录及elastic-certificates.p12、elastic-certificates.p12两个文件一定要给elasticsearch用户权限:
chown -R elasticsearch:elasticsearch /opt/elasticsearch-8.18.2/config/certs
chmod -R 700 /opt/elasticsearch-8.18.2/config/certs
六、开启https
6.1 生成 HTTP 层 CA(PEM 格式)
在bztc-es-node-1上执行:
# 1. 生成 HTTP 层 CA(PEM 格式)
./bin/elasticsearch-certutil ca --pem -out config/certs/http-ca.zip
unzip config/certs/http-ca.zip -d config/certs/http-ca/
scp /opt/elasticsearch-8.18.2/config/certs/http-ca.zip root@192.168.0.134:/opt/elasticsearch-8.18.2/config/certs/
scp /opt/elasticsearch-8.18.2/config/certs/http-ca.zip root@192.168.0.135:/opt/elasticsearch-8.18.2/config/certs/
6.2 签发证书
在bztc-es-node-1上执行:
# 2. 使用上面 CA 签发每个节点自己的 HTTP 证书
# 你可以针对每个节点单独签,确保各自 CN 和 SAN(IP、域名)正确
./bin/elasticsearch-certutil cert \
--ca-cert config/certs/http-ca/ca/ca.crt \
--ca-key config/certs/http-ca/ca/ca.key \
--pem --name bztc-es-node-1 \
--dns localhost \
--ip 192.168.0.133 \
-out config/certs/bztc-es-node-1-http.zip
unzip config/certs/bztc-es-node-1-http.zip -d config/certs/bztc-es-node-1-http/
对于其他节点(如 192.168.0.134、192.168.0.135):
重复执行上面签发命令,只是 --name 和 --dns 改为目标节点的名字和 IP。
在bztc-es-node-1上执行:
./bin/elasticsearch-certutil cert \
--ca-cert config/certs/http-ca/ca/ca.crt \
--ca-key config/certs/http-ca/ca/ca.key \
--pem --name bztc-es-node-2 \
--dns localhost \
--ip 192.168.0.134 \
-out config/certs/bztc-es-node-2-http.zip
scp /opt/elasticsearch-8.18.2/config/certs/bztc-es-node-2-http.zip root@192.168.0.134:/opt/elasticsearch-8.18.2/config/certs/
在bztc-es-node-1上执行:
./bin/elasticsearch-certutil cert \
--ca-cert config/certs/http-ca/ca/ca.crt \
--ca-key config/certs/http-ca/ca/ca.key \
--pem --name bztc-es-node-3 \
--dns localhost \
--ip 192.168.0.135 \
-out config/certs/bztc-es-node-3-http.zip
scp /opt/elasticsearch-8.18.2/config/certs/bztc-es-node-3-http.zip root@192.168.0.135:/opt/elasticsearch-8.18.2/config/certs/
在bztc-es-node-2上执行:
unzip config/certs/http-ca.zip -d config/certs/http-ca/
unzip config/certs/bztc-es-node-2-http.zip -d config/certs/bztc-es-node-2-http/
在bztc-es-node-3上执行:
unzip config/certs/http-ca.zip -d config/certs/http-ca/
unzip config/certs/bztc-es-node-3-http.zip -d config/certs/bztc-es-node-3-http/
权限非常重要,在每个节点执行:
chown -R elasticsearch:elasticsearch /opt/elasticsearch-8.18.2/config/certs
chmod 600 /opt/elasticsearch-8.18.2/config/certs/**/**/*.key
chmod 644 /opt/elasticsearch-8.18.2/config/certs/**/**/*.crt
6.3 每个节点配置自己的 http 层证书
示例 elasticsearch.yml:
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.key: certs/bztc-es-node-1-http/bztc-es-node-1/bztc-es-node-1.key
xpack.security.http.ssl.certificate: certs/bztc-es-node-1-http/bztc-es-node-1/bztc-es-node-1.crt
xpack.security.http.ssl.certificate_authorities: [ "certs/http-ca/ca/ca.crt" ]
6.4 重启验证
所有节点重启
6.4.1 验证1
[root@es logs]# curl -k -u elastic:uWj2HAOy8vadkgetOxHK https://192.168.0.133:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.0.133 3 85 6 0.38 1.03 1.99 cdfhilmrstw - bztc-es-node-1
192.168.0.134 2 85 5 0.38 1.03 1.99 cdfhilmrstw * bztc-es-node-2
192.168.0.135 1 85 5 0.38 1.03 1.99 cdfhilmrstw - bztc-es-node-3
• -k 是 --insecure,表示不校验证书链。
• 适合测试,不推荐生产用。
6.4.2 验证2
如果你有 ca.crt,建议这样写:
curl --cacert /opt/elasticsearch-8.18.2/config/certs/http-ca/ca/ca.crt -u elastic:你的密码 https://192.168.0.133:9200/_cat/nodes
[root@es elasticsearch-8.18.2]# curl --cacert /opt/elasticsearch-8.18.2/config/certs/http-ca/ca/ca.crt -u elastic:你的密码 https://192.168.0.133:9200/_cat/nodes
192.168.0.133 1 85 1 1.51 1.28 1.37 cdfhilmrstw - bztc-es-node-1
192.168.0.134 1 85 2 1.51 1.28 1.37 cdfhilmrstw * bztc-es-node-2
192.168.0.135 9 85 1 1.51 1.28 1.37 cdfhilmrstw - bztc-es-node-3
[root@es elasticsearch-8.18.2]# curl --cacert /opt/elasticsearch-8.18.2/config/certs/http-ca/ca/ca.crt -u elastic:你的密码 https://192.168.0.134:9200/_cat/nodes
192.168.0.134 2 85 1 1.16 1.20 1.33 cdfhilmrstw * bztc-es-node-2
192.168.0.133 2 85 1 1.16 1.20 1.33 cdfhilmrstw - bztc-es-node-1
192.168.0.135 10 85 1 1.16 1.20 1.33 cdfhilmrstw - bztc-es-node-3
[root@es elasticsearch-8.18.2]# curl --cacert /opt/elasticsearch-8.18.2/config/certs/http-ca/ca/ca.crt -u elastic:你的密码 https://192.168.0.135:9200/_cat/nodes
192.168.0.133 2 85 2 1.23 1.22 1.34 cdfhilmrstw - bztc-es-node-1
192.168.0.135 10 85 2 1.23 1.22 1.34 cdfhilmrstw - bztc-es-node-3
192.168.0.134 2 85 2 1.23 1.22 1.34 cdfhilmrstw * bztc-es-node-2
6.4.3 验证3
使用浏览器或 Postman 请求测试
浏览器输入:https://192.168.0.133:9200
但会提示“不受信任的证书”,你可导入 ca.crt 进浏览器即可解决。
总结
elasticsearch集群部署