Elasticsearch 单节点迁移实战指南:从旧服务器到新环境的完整流程

Elasticsearch迁移方案

完整迁移步骤

1. 准备新服务器环境(10.x.x.3)

首先在新服务器上创建必要的目录结构:

bash 复制代码
# 在10.x.x.3服务器上执行
sudo mkdir -p /home/soft
cd /home/soft

# 确保有足够的磁盘空间
df -h /home/soft

# 检查Java环境(Elasticsearch 8.x内置JDK,但建议检查系统Java)
java -version

2. 检查和配置系统参数

检查和设置Elasticsearch必需的系统参数:

bash 复制代码
# 检查当前vm.max_map_count值
cat /proc/sys/vm/max_map_count

# 检查/etc/sysctl.conf中是否已配置
grep "vm.max_map_count" /etc/sysctl.conf

# 如果没有配置或值小于262144,需要修改
if [ $(cat /proc/sys/vm/max_map_count) -lt 262144 ]; then
    echo "需要修改vm.max_map_count参数"
    
    # 临时设置(重启后失效)
    sudo sysctl -w vm.max_map_count=262144
    
    # 永久设置
    if ! grep -q "vm.max_map_count" /etc/sysctl.conf; then
        echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.conf
        echo "已添加vm.max_map_count=262144到/etc/sysctl.conf"
    else
        sudo sed -i 's/vm.max_map_count=.*/vm.max_map_count=262144/' /etc/sysctl.conf
        echo "已更新/etc/sysctl.conf中的vm.max_map_count值"
    fi
    
    # 应用配置
    sudo sysctl -p
else
    echo "vm.max_map_count已正确配置: $(cat /proc/sys/vm/max_map_count)"
fi

# 验证配置
echo "当前vm.max_map_count值: $(cat /proc/sys/vm/max_map_count)"

# 检查其他重要的系统限制
ulimit -n  # 文件描述符限制
ulimit -u  # 进程数限制
ulimit -l  # 内存锁定限制

3. 停止源服务器的Elasticsearch服务

在172.x.x.147服务器上:

bash 复制代码
# 停止Elasticsearch服务
cd /opt/elasticsearch-8.17.3
./bin/elasticsearch-service-stop

# 或者如果是systemd服务
sudo systemctl stop elasticsearch

# 确认服务已停止
ps aux | grep elasticsearch

4. 备份源服务器数据

在172.x.x.147服务器上:

bash 复制代码
cd /opt

# 创建完整备份
sudo tar -czf elasticsearch-8.17.3-backup-$(date +%Y%m%d_%H%M%S).tar.gz elasticsearch-8.17.3/

# 验证备份文件
ls -lh elasticsearch-8.17.3-backup-*.tar.gz

# 单独备份重要配置文件
sudo cp -r elasticsearch-8.17.3/config /tmp/es-config-backup
sudo cp -r elasticsearch-8.17.3/data /tmp/es-data-backup

5. 传输文件到新服务器

有几种传输方式,选择其中一种:

方式A:使用scp直接传输
bash 复制代码
# 在172.x.x.147服务器上执行
scp elasticsearch-8.17.3-backup-*.tar.gz username@10.*.*.3:/home/soft/

# 或者直接传输整个目录
scp -r /opt/elasticsearch-8.17.3/ username@10.*.*.3:/home/soft/
方式B:使用rsync(推荐,支持断点续传)
bash 复制代码
# 在172.*.*.147服务器上执行
rsync -avz --progress /opt/elasticsearch-8.17.3/ username@10.*.*.3:/home/soft/elasticsearch-8.17.3/

6. 在新服务器上解压和配置

在10.x.x.3服务器上:

bash 复制代码
cd /home/soft

# 如果传输的是tar.gz文件,解压
tar -xzf elasticsearch-8.17.3-backup-*.tar.gz

# 设置正确的权限
sudo chown -R elasticsearch:elasticsearch elasticsearch-8.17.3/

# 如果elasticsearch用户不存在,创建
sudo useradd -r -m -s /bin/bash elasticsearch
sudo chown -R elasticsearch:elasticsearch elasticsearch-8.17.3/

# 设置目录权限
chmod 755 elasticsearch-8.17.3/
chmod -R 755 elasticsearch-8.17.3/bin/
chmod -R 644 elasticsearch-8.17.3/config/
chmod 600 elasticsearch-8.17.3/config/elasticsearch.yml

7. 修改配置文件

在新服务器上编辑配置文件:

bash 复制代码
cd /home/soft/elasticsearch-8.17.3
sudo vim config/elasticsearch.yml

主要需要修改的配置项:

yaml 复制代码
# 修改集群名称(如果需要)
cluster.name: your-cluster-name

# 修改节点名称
node.name: node-10.x.x.3

# 修改网络配置
network.host: 10.x.x.3
http.port: 9200

# 修改路径配置
path.data: /home/soft/elasticsearch-8.17.3/data
path.logs: /home/soft/elasticsearch-8.17.3/logs

# 检查其他配置是否需要调整

8. 修改JVM配置(如需要)

bash 复制代码
sudo vim config/jvm.options

# 根据新服务器内存情况调整堆内存
# -Xms4g
# -Xmx4g

9. 创建系统服务(可选)

创建systemd服务文件:

bash 复制代码
sudo vim /etc/systemd/system/elasticsearch.service

内容:

ini 复制代码
[Unit]
Description=Elasticsearch
Documentation=https://www.elastic.co
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/home/soft/elasticsearch-8.17.3

[Service]
Type=exec
RuntimeDirectory=elasticsearch
PrivateTmp=true
Environment=ES_HOME=/home/soft/elasticsearch-8.17.3
Environment=ES_PATH_CONF=/home/soft/elasticsearch-8.17.3/config
Environment=PID_DIR=/var/run/elasticsearch
Environment=ES_SD_NOTIFY=true

WorkingDirectory=/home/soft/elasticsearch-8.17.3
User=elasticsearch
Group=elasticsearch

ExecStart=/home/soft/elasticsearch-8.17.3/bin/elasticsearch
Restart=on-failure
RestartSec=5

# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65535
# Specifies the maximum number of processes
LimitNPROC=4096
# Specifies the maximum size of virtual memory
LimitAS=infinity
# Specifies the maximum file size
LimitFSIZE=infinity
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0
# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM
# Send the signal only to the JVM rather than its control group
KillMode=process
# Java process is never killed
SendSIGKILL=no
# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143
# Allow a slow startup before the systemd notifier module kicks in to extend the timeout
TimeoutStartSec=180

[Install]
WantedBy=multi-user.target

启用服务:

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch

10. 启动Elasticsearch

bash 复制代码
cd /home/soft/elasticsearch-8.17.3

# 方式1:直接启动(前台运行,用于测试)
sudo -u elasticsearch ./bin/elasticsearch

# 方式2:后台启动
sudo -u elasticsearch ./bin/elasticsearch -d

# 方式3:使用systemd服务
sudo systemctl start elasticsearch

11. 验证迁移结果

bash 复制代码
# 检查服务状态
sudo systemctl status elasticsearch

# 或者
ps aux | grep elasticsearch

# 检查端口监听
netstat -tulpn | grep 9200

# 或者
ss -tulpn | grep 9200

# 测试API访问
# 如果ES没有启用安全验证:
curl -X GET "10.x.x.3:9200/"
curl -X GET "10.x.x.3:9200/_cluster/health"
curl -X GET "10.x.x.3:9200/_cat/indices?v"

# 如果ES启用了安全验证,使用用户名密码:
curl -u username:password -X GET "10.x.x.3:9200/"
curl -u username:password -X GET "10.x.x.3:9200/_cluster/health"
curl -u username:password -X GET "10.x.x.3:9200/_cat/indices?v"

# 检查日志
tail -f /home/soft/elasticsearch-8.17.3/logs/elasticsearch.log

12. 验证数据完整性

bash 复制代码
# 检查索引列表
# 如果ES没有启用安全验证:
curl -X GET "10.x.x.3:9200/_cat/indices?v&s=index"

# 如果ES启用了安全验证:
curl -u username:password -X GET "10.x.x.3:9200/_cat/indices?v&s=index"

# 检查集群状态
# 如果ES没有启用安全验证:
curl -X GET "10.x.x.3:9200/_cluster/health?pretty"

# 如果ES启用了安全验证:
curl -u username:password -X GET "10.x.x.3:9200/_cluster/health?pretty"

# 检查节点信息
# 如果ES没有启用安全验证:
curl -X GET "10.x.x.3:9200/_cat/nodes?v"

# 如果ES启用了安全验证:
curl -u username:password -X GET "10.x.x.3:9200/_cat/nodes?v"

# 检查具体索引的文档数量(数据完整性验证)
# 如果ES没有启用安全验证:
curl -X GET "10.x.x.3:9200/_cat/count?v"
curl -X GET "10.x.x.3:9200/your_index_name/_count"

# 如果ES启用了安全验证:
curl -u username:password -X GET "10.x.x.3:9200/_cat/count?v"
curl -u username:password -X GET "10.x.x.3:9200/your_index_name/_count"

# 验证数据目录
ls -la /home/soft/elasticsearch-8.17.3/data/

# 比较源服务器和新服务器的数据(在源服务器上执行对比)
echo "=== 源服务器数据统计 ==="
curl -u username:password -X GET "172.x.x.147:9200/_cat/indices?v&s=index"
curl -u username:password -X GET "172.x.x.147:9200/_cat/count?v"

echo "=== 新服务器数据统计 ==="
curl -u username:password -X GET "10.x.x.3:9200/_cat/indices?v&s=index"
curl -u username:password -X GET "10.x.x.3:9200/_cat/count?v"

13. 清理和安全检查

bash 复制代码
# 检查防火墙设置
sudo firewall-cmd --list-all

# 如需开放端口
sudo firewall-cmd --permanent --add-port=9200/tcp
sudo firewall-cmd --reload

# 如果启用了HTTPS,还需要开放9300端口(集群通信)
sudo firewall-cmd --permanent --add-port=9300/tcp

# 检查SELinux(如果启用)
getenforce

# 如果SELinux启用,可能需要设置上下文
sudo setsebool -P httpd_can_network_connect 1

注意事项

  1. 数据一致性:确保在停止服务前完成所有写入操作

  2. 磁盘空间:确保新服务器有足够空间存储所有数据

  3. 网络配置:更新相关应用的Elasticsearch连接地址

  4. 安全设置:如果原配置有安全认证,确保证书和密钥正确迁移

  5. 版本兼容性:确保新服务器环境与Elasticsearch 8.17.3兼容

回滚方案

如果迁移出现问题,可以:

  1. 在原服务器172.x.x.147上重新启动Elasticsearch

  2. 使用备份文件恢复原始配置

  3. 更新应用连接回原服务器

相关推荐
nxb5567 小时前
haproxy算法,ip透传,自定义haproxy错误页面
运维·服务器
1名持续学习的码农9 小时前
GPT Plus、GPT Pro用户第一次用Codex,项目权限和Git分支怎么设置?
人工智能·git·gpt·elasticsearch·ai编程·codex
小罗水11 小时前
附录A 各微服务完整 application.yml 配置汇总
数据库·elasticsearch·微服务
便利店102412 小时前
TCP 为什么有时故意跑不快?拥塞控制与滑动窗口一次讲清
服务器·网络协议·tcp·拥塞控制
Elasticsearch13 小时前
使用 Gemma、Hugging Face 和 Elasticsearch 构建 RAG 系统
elasticsearch
杨某不才13 小时前
如何能让Linux服务器对shell 终端 + sftp 文件传输长期保活
linux·运维·服务器
Elasticsearch13 小时前
使用 NVIDIA NeMo Retriever、Unstructured 和 Elasticsearch 处理非结构化数据
elasticsearch
上海安当技术13 小时前
统一身份认证平台怎么落地?11 个异构业务系统接入 ASP 的完整实施路径
数据库·servlet·架构·kubernetes·jenkins
三言老师14 小时前
文本工具组合统计服务器日志数据
linux·运维·服务器