马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
加群接洽作者vx:xiaoda0423
仓库地址:https://webvueblog.github.io/JavaPlusDoc/
https://1024bat.cn/
✅ 一、情况准备
✅ 二、单机安装(得当开发)
📦 1. 下载并解压 Elasticsearch
- # 官方源下载
- wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.14-linux-x86_64.tar.gz
- tar -zxvf elasticsearch-7.17.14-linux-x86_64.tar.gz
- cd elasticsearch-7.17.14
复制代码 🛠 2. 修改设置文件(config/elasticsearch.yml)
- cluster.name: my-es-cluster
- node.name: node-1
- network.host: 0.0.0.0
- http.port: 9200
- discovery.seed_hosts: ["127.0.0.1"]
- cluster.initial_master_nodes: ["node-1"]
复制代码 🔧 3. 设置 JVM 内存(可选:config/jvm.options)
设置为系统可用内存一半或略小(制止 OOM)
🚀 4. 启动 Elasticsearch
- # 建议用非 root 用户
- ./bin/elasticsearch
复制代码另起窗口测试: - curl http://localhost:9200
复制代码 ✅ 三、集群摆设(2~3 节点生产情况)
假设摆设两台节点:
node-1:192.168.1.101
node-2:192.168.1.102
🧩 设置文件(每台机器修改 config/elasticsearch.yml)
📍 node-1:
- cluster.name: my-es-cluster
- node.name: node-1
- network.host: 192.168.1.101
- http.port: 9200
- discovery.seed_hosts: ["192.168.1.101", "192.168.1.102"]
- cluster.initial_master_nodes: ["node-1", "node-2"]
复制代码 📍 node-2:
- cluster.name: my-es-cluster
- node.name: node-2
- network.host: 192.168.1.102
- http.port: 9200
- discovery.seed_hosts: ["192.168.1.101", "192.168.1.102"]
- cluster.initial_master_nodes: ["node-1", "node-2"]
复制代码 🚀 启动两台节点,并验证集群:
- curl http://192.168.1.101:9200/_cluster/health?pretty
- curl http://192.168.1.101:9200/_cat/nodes?v
复制代码返回包含两个节点,则阐明集群 OK。
✅ 四、可选增强:安装 Kibana(可视化控制台)
- wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.14-linux-x86_64.tar.gz
- tar -zxvf kibana-7.17.14-linux-x86_64.tar.gz
- cd kibana-7.17.14/config
- # 修改 kibana.yml
- server.port: 5601
- server.host: "0.0.0.0"
- elasticsearch.hosts: ["http://192.168.1.101:9200"]
复制代码启动 Kibana: 访问地址:http://localhost:5601
✅ 五、常用 Elasticsearch 命令
命令
阐明
GET /
查看 ES 基本信息
GET _cat/nodes?v
查看全部节点
GET _cat/indices?v
查看全部索引
GET /my-index/_search
查询索引数据
PUT /my-index
创建索引
DELETE /my-index
删除索引
✅ 六、常见问题
问题
原因与办理方案
bootstrap checks failed
克制用 root 启动,设置 vm.max_map_count=262144
cluster.name 不同等
全部节点必须同等
master not discovered 报错
discovery.seed_hosts 配错
9200端口无法访问
防火墙未开、未设置 network.host 为公网 IP 或 0.0.0.0
✅ 七、优化建议(生产)
Elasticsearch 安装与摆设指南(2025 年版)
适用人群:需要在 生产情况(4 核 / 16 GB RAM 以上)构建高可用、 可观测 的 Elasticsearch 集群的 DevOps / 后端工程师。
1. 版本选择
重要版本
最新小版本
发布时间
Java 需求
阐明
8.138.13.2
2025‑04‑18
内置 OpenJDK 21
最新 GA,官方推荐,默认开启安全 (TLS + Basic Auth)
7.17 LTS7.17.18
2025‑03‑28
JDK 11
恒久维护分支,制止 Feature 更新,仅安全补丁
新集群 建议直接使用 8.x 分支;若与旧版兼容或 Logstash/Beats 仍停顿在 7.x,可暂用 7.17 并规划升级。
2. 基础情况要求
资源
最低
建议生产设置
CPU
4 vCPU
≥8 vCPU (高 IO 任务 ≥16 核)
内存
16 GB
32 GB (Heap 16 GB + OS Cache 16 GB)
磁盘
SSD/NVMe ≥ 200 GB
多块 NVMe RAID‑0/10;关闭 swap;ext4/xfs noatime
OS
Ubuntu 22.04 / AlmaLinux 9 / Debian 12
内核 ≥ 5.15, vm.max_map_count=262144
系统调优: - # /etc/sysctl.d/99-es.conf
- vm.swappiness = 1
- vm.max_map_count = 262144
- fs.file-max = 2097152
- # 生效
- sysctl -p /etc/sysctl.d/99-es.conf
- # ulimit
- * soft nofile 65536
- * hard nofile 65536
复制代码 3. 安装方式对比
方式
优点
缺点
rpm/apt 包一条命令自动 systemd,快捷
版本更新节奏依赖仓库;多版本共存困难
tar.gz 二进制完全控制目次与版本,便于滚动升级
需手动设置 systemd & 用户
Docker / K8s情况隔离、易横向扩展
恒久化卷、内核调优需要额外关注
生产建议:物理机 / VM 优选 tar.gz + systemd; 云原生可使用 Elastic Cloud on K8s (ECK) 。
4. 单节点快速安装(tar.gz)
- # 4.1 下载 (以 8.13.2 为例)
- wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.13.2-linux-x86_64.tar.gz
- # 4.2 创建用户与目录
- useradd --system --no-create-home --shell /sbin/nologin elasticsearch
- mkdir -p /opt/elastic/{data,logs}
- # 4.3 解压并软链
- tar -xzf elasticsearch-8.13.2-linux-x86_64.tar.gz -C /opt/elastic
- ln -s /opt/elastic/elasticsearch-8.13.2 /opt/elastic/current
- chown -R elasticsearch:elasticsearch /opt/elastic
- # 4.4 修改 elasticsearch.yml
- vim /opt/elastic/current/config/elasticsearch.yml
- # 关键字段示例
- cluster.name: demo-cluster
- node.name: node-1
- path.data: /opt/elastic/data
- path.logs: /opt/elastic/logs
- network.host: 0.0.0.0
- http.port: 9200
- xpack.security.enabled: true
- xpack.security.http.ssl:
- enabled: true
- keystore.path: certs/http.p12 # 8.x 安装时会自动生成自签证书 (elastic-certificates.p12)
- # 如需关闭 TLS 仅测试:
- # xpack.security.transport.ssl.enabled: false
- # 4.5 初始化密码(仅 8.x 首次启动)
- /opt/elastic/current/bin/elasticsearch-reset-password -u elastic -b
- # 4.6 创建 systemd
- cat >/etc/systemd/system/elasticsearch.service <<'EOF'
- [Unit]
- Description=Elasticsearch
- Wants=network-online.target
- After=network-online.target
- LimitNOFILE=65536
- [Service]
- Type=simple
- User=elasticsearch
- Group=elasticsearch
- ExecStart=/opt/elastic/current/bin/elasticsearch -p /var/run/elasticsearch.pid
- Restart=on-failure
- TimeoutStopSec=300
- [Install]
- WantedBy=multi-user.target
- EOF
- systemctl daemon-reload
- systemctl enable --now elasticsearch
- # 4.7 验证
- curl --cacert /opt/elastic/current/config/certs/http_ca.crt -u elastic:<密码> https://localhost:9200
复制代码 5. 三节点高可用集群摆设
5.1 架构规划
3 个 master/data 热节点,副本因子 1 (总 2 份数据)
需要更多层次可引入 warm/cold/node roles: data_hot, data_warm, data_cold, data_frozen。
5.2 设置示例(node-1)
- cluster.name: prod-cluster
- node.name: node-1
- node.roles: [ master, data_hot ]
- network.host: 192.168.10.11
- seed_hosts: ["192.168.10.11","192.168.10.12","192.168.10.13"]
- cluster.initial_master_nodes: ["node-1","node-2","node-3"]
- path.data: /data/es
- xpack.security.enabled: true
复制代码其余节点修改 node.name 与 network.host 即可。
5.3 TLS 与认证
bin/elasticsearch-certutil cert --pem --in instance.yml --out certs.zip
将生成的 cert 分发到每节点 config/certs,yml 指向对应 p12/PEM。
使用 elasticsearch-users 或 Kibana Stack Management → Users/Roles 管理权限。
6. 系统与 JVM 调优
参数
建议
Heap Size
-Xms -Xmx 设相同,≤ 32 GB (压缩指针);生产常见 50% 内存
GC
G1GC (8.x 默认),Heap ≤ 16 GB 时 CMS 也可
indices.memory.index_buffer_size10% (默认);大 bulk 写可临时调高
thread_pool.write.queue_size写入高峰得当加大,如 1000
Shard 大小
单 shard 10–50 GB;过大影响规复,过小浪费资源
ILM
热→温→冷→冻结;定期 rollover、shrink、delete
7. 监控 与告警
Elastic Stack:Metricbeat + Filebeat + Kibana Stack Monitoring。
Prometheus Exporter:prometheus/elasticsearch_exporter → Grafana。
关键指标:CPU load, Heap Usage, JVMMemoryPressure, Search/Index Latency, Disk I/O, Segment Count, ClusterHealth。
8. 备份(快照)与规复
- # 创建 S3 仓库示例
- PUT _snapshot/s3_repo
- {
- "type": "s3",
- "settings": {
- "bucket": "es-backup-prod",
- "endpoint": "s3.ap-southeast-1.amazonaws.com",
- "region": "ap-southeast-1"
- }
- }
- # 触发每日快照
- PUT _snapshot/s3_repo/daily-<date>
复制代码规复:POST _snapshot/s3_repo/snap-2025.05.10/_restore,可指定 index。
9. 常见问题排查
现象
排查点
Yellow 状态
副本未分配 → 查看磁盘阈值、水位,确认节点 roles
索引写入慢
/_nodes/hot_threads, bulk queue,查抄 refresh 隔断、replica 数
CircuitBreaker trip
调大 heap 或优化查询;indices.breaker.request.limit
高 GC
查抄 fielddata、聚合内存,使用 doc_values 或升级硬件
10. 升级路径
完成! 如需 ECK Operator、Terraform 自动化摆设或详细 ILM/安全脚本示例,请另行告知,我可以继承补充。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|