主题
Elasticsearch 02 · 部署方案
四篇制:01 理论 · 02 部署 · 03 功能 · 04 实战 本篇装 ES 单节点与集群。原理见 01 理论。日志链路整体见 ELK。
一、系统准备(硬性要求)
bash
echo 'vm.max_map_count=262144' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
sudo swapoff -a # ES 要求关 swap(或 bootstrap.memory_lock)
# /etc/security/limits.conf:nofile 65536 / nproc 4096 / memlock unlimitedvm.max_map_count 不够 ES 启动直接报错;swap 会让 GC 停顿飙升,生产必关或锁内存。
二、单节点部署
bash
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-9.5.4-x86_64.rpm
sudo yum localinstall elasticsearch-9.5.4-x86_64.rpm -y/etc/elasticsearch/elasticsearch.yml:
yaml
cluster.name: my-es
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-nodebash
sudo systemctl enable --now elasticsearch
curl -k -u elastic:密码 https://127.0.0.1:92009.x 默认启用认证 + TLS,安装时生成 elastic 初始密码务必保存,忘了用 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic 重置。
生产别关安全
纯内网实验可设 xpack.security.enabled: false 免认证用 http,但只适用隔离学习环境,生产保留认证和 TLS。
三、集群部署
三节点为例,各节点 elasticsearch.yml:
yaml
cluster.name: my-es
node.name: node-1 # 各节点不同
node.roles: [ master, data ] # 按角色规划
network.host: 10.0.0.61
discovery.seed_hosts: ['10.0.0.61','10.0.0.62','10.0.0.63']
cluster.initial_master_nodes: ['node-1','node-2','node-3'] # 仅首次启动用discovery.seed_hosts:节点互相发现的种子列表。cluster.initial_master_nodes:仅集群首次引导用,选出初始 master,集群成型后应移除(避免重启时脑裂)。- 生产建议 3 个专用 master(
node.roles: [master])+ 若干 data 节点,master 不存数据专心管集群。
bash
curl -k -u elastic:密码 https://10.0.0.61:9200/_cluster/health?pretty # 看集群健康四、JVM 堆设置原则
/etc/elasticsearch/jvm.options.d/heap.options:
text
-Xms8g
-Xmx8g两条铁律:Xms=Xmx(避免运行时扩堆停顿);不超物理内存一半(留另一半给 OS cache 加速 Lucene),且不超 31G(超过会失去压缩指针优化,反而更费内存)。剩下内存留给文件系统缓存,ES 检索极依赖它。
五、官方文档
- 安装:https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html
- 集群发现:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html
- 堆设置:https://www.elastic.co/guide/en/elasticsearch/reference/current/advanced-configuration.html#set-jvm-heap-size
黑公网安备23022102000118号