分布式微服务系统架构第128集:elastic-search安装部署

发布于:2025-05-12 ⋅ 阅读:(26) ⋅ 点赞:(0)

加群联系作者vx:xiaoda0423

仓库地址:https://webvueblog.github.io/JavaPlusDoc/

https://1024bat.cn/

✅ 一、环境准备

  • 操作系统:Linux(Ubuntu 20+/CentOS 7+/Rocky)或 macOS

  • 内存要求:建议 4GB+(最小 2GB)

  • Java:Elasticsearch 7.x+ 内置了 OpenJDK,不需额外安装

  • 版本推荐:ES 7.17.x(稳定)或 ES 8.x(安全增强)


✅ 二、单机安装(适合开发)

📦 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

-Xms1g
-Xmx1g

设置为系统可用内存一半或略小(避免 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:

./bin/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


✅ 七、优化建议(生产)

  • 设置专用的数据节点、主节点

  • 关闭自动索引创建(action.auto_create_index: false

  • 使用 HTTPS 和账号密码(ES 8+ 默认开启安全)

  • 配置慢查询日志 + 索引模板

  • 安装 IK 分词器 / Pinyin 分词器(中文搜索)

Elasticsearch 安装与部署指南(2025 年版)

适用人群:需要在生产环境(4 核 / 16 GB RAM 以上)构建高可用、可观测的 Elasticsearch 集群的 DevOps / 后端工程师。


1. 版本选择

主要版本

最新小版本

发布时间

Java 需求

说明

8.13

8.13.2

2025‑04‑18

内置 OpenJDK 21

最新 GA,官方推荐,默认开启安全 (TLS + Basic Auth)

7.17 LTS

7.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_hotdata_warmdata_colddata_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_size 10%

 (默认);大 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 Exporterprometheus/elasticsearch_exporter → Grafana。

  • 关键指标CPU loadHeap UsageJVMMemoryPressureSearch/Index LatencyDisk I/OSegment CountClusterHealth


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. 升级路径

  • 7.17 → 8.x:滚动升级需启用安全,先升级 KibanaLogstash,最后 Beats

  • 重大版本前务必全量快照,阅读 Upgrade Guide。


完成! 如需 ECK Operator、Terraform 自动化部署或具体 ILM/安全脚本示例,请另行告知,我可以继续补充。


网站公告

今日签到

点亮在社区的每一天
去签到