Elasticsearch8.4.1升级Elasticsearch9.1.5
本次升级为 跨大版本升级(8.x → 9.x),需重点关注 兼容性适配、数据安全备份、业务零中断 三大核心目标。教程覆盖 测试环境单节点 与 生产环境多节点集群 场景,包含完整的 “准备 - 执行 - 验证 - 回退” 流程,确保升级可控。
一、升级前核心准备(必做!规避 90% 风险)
升级前需完成兼容性检查、数据备份、服务暂停,避免因配置冲突或数据丢失导致升级失败。
1. 兼容性深度检查(9.x 关键变更适配)
9.x 相比 8.x 存在 Breaking Changes,需提前确认以下内容,否则会导致服务启动失败或业务异常:
| 检查项 | 8.4.1 现状 | 9.1.5 要求与适配方案 |
|---|---|---|
| JDK 版本 | 内置 JDK 17 | 9.1.5 同样内置 JDK 17,无需额外安装 / 升级,直接复用内置 JDK 即可 |
| 插件兼容性 | 如 analysis-ik、自定义插件 | 1. 官方插件(analysis-ik、analysis-stconvert)需升级至 9.1.5 版本,旧版本插件会触发启动报错;2. 第三方插件 / 自定义插件:需联系开发者确认 9.x 适配性,无适配版本需 暂时卸载,升级后再评估替代方案 |
| API 与配置变更 | 依赖旧 API(如部分查询参数) | 1. 9.x 移除废弃 API(如 indices:admin/template/get 接口),需通过 ES 9.x 官方变更日志 确认业务代码无依赖;2. 废弃配置项(如单节点的 cluster.initial_master_nodes)需从 elasticsearch.yml 中删除 |
| Kibana 版本 | 8.4.1 版本 | Kibana 必须与 ES 版本 完全一致(9.1.5),需同步升级 Kibana,否则会因协议不兼容导致连接失败 |
| 磁盘空间 | 剩余空间 ≥ 数据量 50% | 升级过程会生成临时索引数据,需确保磁盘剩余空间 ≥ 当前数据量的 100%(避免分片分配失败) |
2. 数据全量备份(生产环境必须双备份)
升级前必须备份所有索引数据,防止升级失败后数据无法恢复。推荐 “Snapshot 备份 + 数据目录复制” 双备份方案:
方案 1:Snapshot API 备份(生产环境推荐,支持增量恢复)
适用于所有环境,备份后可直接恢复到 9.1.5 集群,步骤如下:
# 1. 创建本地备份仓库(生产环境建议用 S3/OSS/MinIO 等分布式存储,避免单点故障)
curl -X PUT "http://<ES节点IP>:9200/_snapshot/es_upgrade_backup" \-H "Content-Type: application/json" \-u elastic:<8.4.1的elastic密码> \-d '{"type": "fs","settings": {"location": "/data/es_backup/8.4.1_snapshot", # 备份目录(需提前创建,ES 账户有读写权限)"compress": true, # 启用数据压缩,节省磁盘空间"max_restore_bytes_per_sec": "50mb", # 恢复速率限制(避免影响业务)"max_snapshot_bytes_per_sec": "50mb" # 备份速率限制}}'# 2. 全量备份所有索引(添加 wait_for_completion=true 确保备份完成后再返回)
curl -X PUT "http://<ES节点IP>:9200/_snapshot/es_upgrade_backup/backup_8.4.1_full?wait_for_completion=true" \-u elastic:<8.4.1的elastic密码># 3. 验证备份完整性(确保备份状态为 SUCCESS)
curl -X GET "http://<ES节点IP>:9200/_snapshot/es_upgrade_backup/backup_8.4.1_full" \-u elastic:<8.4.1的elastic密码>
- 备份成功标志:返回结果中
state字段为SUCCESS,且shards下successful等于总分片数。
方案 2:数据目录冷备份(测试环境快速备份)
适用于单节点测试环境,直接复制 ES 数据目录(需先停止 ES 服务,避免数据写入导致损坏):
# Windows 环境(8.4.1 数据目录默认路径)
# 1. 停止 8.4.1 服务(通过“服务”面板停止 Elasticsearch 8.4.1)
# 2. 复制数据目录
xcopy "D:\Program Files\Elastic\Elasticsearch\8.4.1\data" "D:\ES_Backup\8.4.1_data_dir" /E /H /R
3. 暂停数据写入服务(避免数据不一致)
升级期间需停止所有向 ES 写入数据的服务,防止升级中数据写入中断导致不一致:
- 数据采集工具:Logstash、Elastic Agent、Fluentd、Filebeat 等;
- 业务系统:直接调用 ES API 写入数据的应用(如订单系统、日志查询系统);
- 定时任务:如索引清理、数据同步脚本等。
二、升级执行步骤(分场景详细操作)
根据集群架构分为 单节点升级(测试环境) 和 多节点滚动升级(生产环境),生产环境需严格按 “从节点→主节点” 顺序升级,确保集群不中断。
场景 1:单节点集群升级(测试环境)
单节点无集群选举问题,步骤相对简单,核心是 “停止旧服务→安装新服务→迁移配置→验证启动”。
步骤 1:停止 Elasticsearch 8.4.1 服务
- Windows(服务面板):
- 按下
Win+R,输入services.msc打开 “服务”; - 找到 “Elasticsearch 8.4.1” 服务,右键选择 “停止”;
- 确认 “状态” 变为 “已停止”。
- 按下
步骤 2:安装 Elasticsearch 9.1.5(推荐 ZIP 包,避免权限冲突)
选择 ZIP 包安装可灵活控制路径,避免 RPM/DEB 包自动覆盖配置的问题,步骤如下:
2.1 下载 9.1.5 安装包
从 Elastic 官方下载对应系统的 ZIP 包(需登录 Elastic 账号,免费注册即可):
- 官方下载地址:Elasticsearch 9.1.5 历史版本
- 选择对应系统:Linux(
elasticsearch-9.1.5-linux-x86_64.tar.gz)、Windows(elasticsearch-9.1.5-windows-x86_64.zip)
2.2 解压安装包(路径避免空格和中文)
- Windows 环境:
- 右键解压
elasticsearch-9.1.5-windows-x86_64.zip到D:\Program Files\Elastic\elasticsearch-9.1.5(路径无空格); - 右键文件夹→“属性”→“安全”→给 “NETWORK SERVICE” 账户添加 “读取和执行”“列出文件夹内容”“读取” 权限(避免启动时权限不足)。
- 右键解压
步骤 3:迁移核心配置(关键!避免直接复制旧配置)
禁止直接复制 8.4.1 的 elasticsearch.yml 到 9.1.5,需手动迁移核心配置,删除 9.x 废弃参数:
3.1 核心配置迁移示例(elasticsearch.yml)
yaml
# 1. 集群与节点基础配置(保持与 8.4.1 一致)
cluster.name: my_test_cluster # 集群名,必须与旧集群一致(否则无法识别数据)
node.name: test_node_1 # 节点名,保持一致
node.roles: [master, data, ingest] # 节点角色,单节点需包含 master 和 data 角色# 2. 数据与日志路径(复用 8.4.1 的数据目录,避免重新导入数据)
path.data: /var/lib/elasticsearch/data # Linux 路径(与 8.4.1 一致)
# path.data: "D:\\Program Files\\Elastic\\Elasticsearch\\8.4.1\\data" # Windows 路径
path.logs: /var/log/elasticsearch-9.1.5 # 日志路径单独设置,避免与旧版本冲突# 3. 网络配置(保持与 8.4.1 一致,确保业务能正常访问)
network.host: 0.0.0.0 # 允许所有IP访问(生产环境需限制具体IP)
http.port: 9200 # HTTP 端口,保持 9200
transport.port: 9300 # 集群通信端口,保持 9300# 4. 安全配置(保持与 8.4.1 一致,确保认证正常)
xpack.security.enabled: true # 启用安全认证(与旧版本一致)
xpack.security.transport.ssl.enabled: true # 启用传输层 SSL(若旧版本启用则保留)
# 注意:9.x 移除了 `cluster.initial_master_nodes`(单节点无需配置),需删除旧配置中的该参数# 5. 资源限制(根据服务器配置调整,避免内存溢出)
-Xms2g # JVM 初始内存(建议为服务器内存的 50%,不超过 31GB)
-Xmx2g # JVM 最大内存(与初始内存一致,避免频繁 GC)
3.2 拷贝 SSL 证书(若启用了 HTTPS/SSL)
若 8.4.1 启用了 SSL 认证(xpack.security.transport.ssl.enabled: true),需将旧版本的证书目录拷贝到 9.1.5 配置中指定的路径:
并在 elasticsearch.yml 中补充证书配置(与 8.4.1 一致):
yaml
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
步骤 4:重新安装适配 9.1.5 的插件
8.4.1 的插件无法在 9.1.5 中使用,需卸载旧插件并安装 9.1.5 版本:
# 进入 9.1.5 的 bin 目录
# Linux:
cd /usr/local/elasticsearch-9.1.5/bin
# Windows(管理员 CMD):
# cd "D:\Program Files\Elastic\elasticsearch-9.1.5\bin"# 1. 安装官方插件(analysis-ik 中文分词为例)
# Linux:
sudo -u elasticsearch ./elasticsearch-plugin install analysis-ik
# Windows:
elasticsearch-plugin install analysis-ik# 2. 安装其他所需插件(如 analysis-stconvert 简繁体转换)
# Linux:
sudo -u elasticsearch ./elasticsearch-plugin install analysis-stconvert
# Windows:
elasticsearch-plugin install analysis-stconvert# 3. 查看已安装插件(确认插件版本为 9.1.5)
# Linux:
sudo -u elasticsearch ./elasticsearch-plugin list
# Windows:
elasticsearch-plugin list
- 安装成功标志:插件列表中显示
analysis-ik、analysis-stconvert,无版本冲突提示。
步骤 5:启动 Elasticsearch 9.1.5 并检查日志
-
Windows(服务启动):
-
cmd
# 管理员 CMD 进入 bin 目录 cd "D:\Program Files\Elastic\elasticsearch-9.1.5\bin" # 安装为 Windows 服务(仅首次执行) elasticsearch-service.bat install # 启动服务 elasticsearch-service.bat start # 查看服务状态(确认“状态”为“已启动”) elasticsearch-service.bat status -
关键日志检查(确认启动成功):日志路径:
- Linux:
/var/log/elasticsearch-9.1.5/my_test_cluster.log - Windows:
D:\Program Files\Elastic\elasticsearch-9.1.5\logs\my_test_cluster.log搜索关键词started,若出现[2025-10-26T10:00:00,123][INFO ][o.e.n.Node ] [test_node_1] started,说明节点启动成功。
- Linux:
场景 2:多节点集群滚动升级(生产环境)
多节点集群需采用 滚动升级(逐节点升级,避免集群 downtime),核心原则:先升级从节点(data/ingest 节点),最后升级主节点(master 节点),确保升级期间集群始终可用。
假设集群架构:3 节点(node-1:主节点 + 数据节点,node-2:数据节点,node-3:数据节点),升级顺序:node-2 → node-3 → node-1。
步骤 1:升级前集群状态检查(确保集群健康)
# 1. 查看集群健康状态(必须为 green,否则先修复分片问题)
curl -X GET "http://node-1:9200/_cluster/health?pretty&wait_for_status=green&timeout=30s" \-u elastic:<生产环境elastic密码># 2. 确认主节点(带 * 标记的为当前主节点,本例中 node-1 为主节点)
curl -X GET "http://node-1:9200/_cat/nodes?v&h=ip,node.name,node.role,version" \-u elastic:<生产环境elastic密码>
# 输出示例(node-1 为主节点):
# ip node.name node.role version
# 10.0.0.1 node-1 mdi 8.4.1
# 10.0.0.2 node-2 di 8.4.1
# 10.0.0.3 node-3 di 8.4.1
- 若集群状态为
yellow,需先执行curl -X POST "http://node-1:9200/_cluster/reroute?retry_failed=true"强制分片分配,待状态变为green再升级。
步骤 2:升级从节点 node-2(数据节点)
2.1 停止 node-2 的 8.4.1 服务
# 在 node-2 节点执行
sudo systemctl stop elasticsearch
# 确认服务已停止
sudo systemctl status elasticsearch
2.2 安装 9.1.5 并迁移配置
同 “单节点场景” 的步骤 2-4:
- 下载并解压 9.1.5 ZIP 包;
- 迁移
elasticsearch.yml核心配置(复用数据目录、保持网络 / 安全配置一致,删除废弃参数); - 拷贝 SSL 证书(若启用);
- 安装 9.1.5 版本插件(analysis-ik 等)。
2.3 启动 node-2 的 9.1.5 服务
# 在 node-2 节点执行
sudo systemctl start elasticsearch-9.1.5
# 查看启动日志,确认无报错
sudo journalctl -u elasticsearch-9.1.5 -f
2.4 验证 node-2 加入集群
# 在任意节点执行,确认 node-2 版本变为 9.1.5,且集群状态为 green
curl -X GET "http://node-1:9200/_cat/nodes?v&h=ip,node.name,node.role,version" \-u elastic:<生产环境elastic密码>
# 输出应包含:10.0.0.2 node-2 di 9.1.5
步骤 3:升级从节点 node-3(数据节点)
完全重复 “步骤 2” 的操作,确保 node-3 升级为 9.1.5 并正常加入集群,且集群状态保持 green。
步骤 4:升级主节点 node-1(最后升级)
主节点升级需先触发 主节点切换,避免手动停止主节点导致集群无主节点:
4.1 触发主节点切换(让集群自动选举新主节点)
ES 集群会自动检测主节点状态,当停止当前主节点(node-1)后,会从可用的从节点(node-2、node-3)中选举新主节点(需确保 discovery.seed_hosts 配置包含 node-2、node-3 的 IP):
# 在 node-1 节点执行,停止 8.4.1 服务
sudo systemctl stop elasticsearch
# 等待 30 秒,让集群选举新主节点
sleep 30
4.2 确认新主节点已选举成功
# 在 node-2 节点执行,查看新主节点(本例中 node-2 会成为新主节点,带 * 标记)
curl -X GET "http://node-2:9200/_cat/nodes?v&h=ip,node.name,node.role,version" \-u elastic:<生产环境elastic密码>
# 输出示例(node-2 成为新主节点):
# ip node.name node.role version
# 10.0.0.1 node-1 mdi (down)
# 10.0.0.2 node-2 *mdi 9.1.5
# 10.0.0.3 node-3 di 9.1.5
4.3 升级 node-1 为 9.1.5
同 “步骤 2” 的操作:安装 9.1.5、迁移配置、安装插件、启动服务。
4.4 验证 node-1 加入集群(集群全节点 9.1.5)
bash
# 在 node-2 节点执行,确认所有节点版本均为 9.1.5,集群状态为 green
curl -X GET "http://node-2:9200/_cluster/health?pretty" \-u elastic:<生产环境elastic密码>
curl -X GET "http://node-2:9200/_cat/nodes?v&h=ip,node.name,node.role,version" \-u elastic:<生产环境elastic密码>
- 成功标志:所有节点版本为 9.1.5,集群状态
green,无unassigned分片。
三、升级后全维度验证(确保业务正常)
升级完成后需从 基础功能、业务功能、性能 三个维度验证,确认无异常后再恢复业务。
1. 基础功能验证(确认 ES 核心能力正常)
bash
# 1. 验证 ES 版本(返回 9.1.5)
curl -X GET "http://<任意节点IP>:9200/" \-u elastic:<elastic密码>
# 成功标志:"version" : { "number" : "9.1.5" }# 2. 验证索引状态(所有索引状态为 open,无 red/yellow 索引)
curl -X GET "http://<任意节点IP>:9200/_cat/indices?v&health=green" \-u elastic:<elastic密码># 3. 验证插件功能(以 analysis-ik 为例,测试中文分词)
curl -X POST "http://<任意节点IP>:9200/_analyze" \-H "Content-Type: application/json" \-u elastic:<elastic密码> \-d '{"analyzer": "ik_max_word","text": "Elasticsearch 8.4.1 升级到 9.1.5 成功"}'
# 成功标志:返回分词结果包含 ["Elasticsearch", "8.4.1", "升级", "到", "9.1.5", "成功"]# 4. 验证数据读写(插入并查询测试数据)
# 插入数据
curl -X POST "http://<任意节点IP>:9200/test_upgrade/_doc/1" \-H "Content-Type: application/json" \-u elastic:<elastic密码> \-d '{"test_key": "upgrade_success","test_value": "ES 9.1.5 读写正常","create_time": "2025-10-26T12:00:00"}'
# 查询数据
curl -X GET "http://<任意节点IP>:9200/test_upgrade/_doc/1" \-u elastic:<elastic密码>
# 成功标志:返回插入的测试数据,无报错
2. 业务功能验证(模拟实际业务场景)
- 核心查询验证:执行业务中常用的查询语句(如聚合查询、过滤查询、高亮查询),确认返回结果与升级前一致;
- 数据采集验证:恢复 Logstash/Elastic Agent 服务,观察 10 分钟,确认数据能正常写入 ES,且无数据丢失;
- 业务系统验证:启动业务应用,测试依赖 ES 的功能(如商品搜索、日志查询),确认业务流程无异常。
3. 性能验证(生产环境必做)
- JVM 内存监控:通过
curl -X GET "http://<节点IP>:9200/_cat/jvm?v"查看 JVM 堆内存使用情况,确保无频繁 GC 或内存溢出; - 分片恢复速度:通过
curl -X GET "http://<节点IP>:9200/_cat/recovery?v"查看分片恢复进度,确保所有分片已完成恢复; - 查询延迟测试:执行业务中耗时较长的查询,对比升级前后的响应时间,确认性能无明显下降。
四、回退方案(升级失败时紧急恢复)
若升级后出现严重问题(如数据损坏、业务不可用),需立即回退到 8.4.1 版本,步骤如下:
1. 停止 9.1.5 服务
- Linux:
sudo systemctl stop elasticsearch-9.1.5 - Windows:
elasticsearch-service.bat stop
2. 恢复 8.4.1 服务
- 方案 1:基于 Snapshot 恢复(推荐):
bash
# 1. 启动 8.4.1 服务 sudo systemctl start elasticsearch # 2. 从备份恢复数据(覆盖升级后可能损坏的数据) curl -X POST "http://<节点IP>:9200/_snapshot/es_upgrade_backup/backup_8.4.1_full/_restore?wait_for_completion=true" \-u elastic:<8.4.1的elastic密码> \-d '{"indices": "*", # 恢复所有索引"include_global_state": true # 恢复集群全局状态}' - 方案 2:基于数据目录恢复(测试环境):
- 删除 8.4.1 数据目录下的临时文件(
/var/lib/elasticsearch/data/nodes); - 将备份的 8.4.1 数据目录(
/data/es_backup/8.4.1_data_dir)复制回原路径; - 启动 8.4.1 服务。
- 删除 8.4.1 数据目录下的临时文件(
3. 验证回退成功
确认 8.4.1 服务启动正常,数据完整,业务功能恢复后,再排查升级失败原因。
五、生产环境升级额外建议
- 灰度验证:先在 测试环境 完成升级验证(至少运行 24 小时),确认无兼容性问题后,再在生产环境执行;
- 错峰升级:选择业务低峰期(如凌晨 2-4 点)执行升级,减少对业务的影响;
- 资源监控:升级期间通过 Prometheus+Grafana 或 ES 自带的
_cat接口监控节点 CPU、内存、磁盘使用率,避免资源过载; - 文档记录:详细记录升级过程中的配置修改、插件版本、遇到的问题及解决方案,便于后续复盘;
- 多团队协同:提前协调运维、开发、业务团队,明确升级时间、职责分工、应急联系方式,确保出现问题时能快速响应。
六、自己经验的总结
--安装和运行 D:\Program Files\elasticsearch-9.1.5\bin
elasticsearch-service.bat install:安装Elasticsearch服务。
elasticsearch-service.bat remove:删除已安装的Elasticsearch服务(如果启动则停止服务)。
elasticsearch-service.bat start:启动Elasticsearch服务(如果已安装)。
elasticsearch-service.bat stop:停止服务(如果启动)。
elasticsearch-service.bat manager:启动GUI来管理已安装的服务。
--修改配置 D:\Program Files\elasticsearch-9.1.5\config\elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
# 计算机名称
node.name: WIN-HRDBR404POR
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
# 本机IP地址
network.host: 10.10.90.137
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
# 端口
http.port: 9200
#
# For more information, consult the network module documentation.
#http.host: 0.0.0.0
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
# 本地名称
cluster.initial_master_nodes: ["WIN-HRDBR404POR"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
# 启用加密
#action.destructive_requires_name: false
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.password: "自己的密码"
xpack.security.transport.ssl.truststore.password: "自己的密码"
xpack.security.transport.ssl.keystore.type: PKCS12
xpack.security.transport.ssl.keystore.path: certs/elastic-stack-ca.p12
xpack.security.transport.ssl.truststore.path: certs/es1-certificates.p12
xpack.security.transport.ssl.truststore.type: PKCS12
--重置elastic的密码
elasticsearch-setup-passwords interactive
2564562541
--下载插件
https://release.infinilabs.com/
--将下载的plugin的插件放到D:\Program Files\elastic-plugins 目录下
--安装elasticsearch-plugin
elasticsearch-plugin install "file:///D:\Program Files\elastic-plugins\elasticsearch-analysis-ik-9.1.5.zip"
elasticsearch-plugin install "file:///D:\Program Files\elastic-plugins\elasticsearch-analysis-pinyin-9.1.5.zip"
elasticsearch-plugin install "file:///D:\Program Files\elastic-plugins\elasticsearch-analysis-stconvert-9.1.5.zip"
---自定义词典配置
---编辑 config/analysis-ik/IKAnalyzer.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict"></entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
---elasticView可视软件
https://www.elastic-view.cn/deploy/binary.html
