太强了!分布式Elasticsearch集群数据迁移企业案例
太强了!分布式Elasticsearch集群数据迁移企业案例
01—背景介绍在现代企业架构中,数据的可靠性、安全性以及高效迁移成为了运维管理和业务连续性的核心考量因素。Elasticsearch作为一种高性能、分布式、开源的搜索引擎和数据分析引擎,广泛应用于日志分析、实时搜索、商业智能等领域。随着数据的增长和业务需求的变化,数据迁移在生产环境中显得尤为重要,比如进行集群扩容、数据分区调整、跨地域迁移等场景。Elasticsearch自身内置了一套强大的数据备份与恢复机制,即Snapshot(快照)和Restore(恢复)功能。Snapshot API允许管理员在指定的Repository(仓库)中创建数据的只读快照,这个过程对正在运行的集群影响较小,且可以实现增量备份,大大提升了备份效率和资源利用率。Repository可以基于多种存储后端,如共享文件系统(如NFS)、云存储服务(如Amazon S3、Azure Blob Storage等)。当需要进行数据迁移时,Snapshot和Restore功能提供了无缝的数据迁移途径。首先,通过Snapshot API在源集群上创建一个或多个索引的快照,并将其存储在共享的Repository中。然后,在目标集群上注册相同的Repository,并通过Restore API将快照恢复到目标集群。这种方式不仅能保证数据的完整性,还允许灵活选择迁移的数据范围和时间点,极大地方便了运维人员在保持服务连续性的同时进行数据迁移操作。综上所述,Elasticsearch自带的Snapshot和Restore机制是进行数据迁移的理想工具,它既能满足大规模数据迁移的需求,又能确保数据迁移过程中服务的稳定性及数据一致性,从而在实际应用中得到了广泛的采用和好评。02
—
迁移架构
03
—
环境准备
-
操作系统:CentOS7
-
ES版本:7.10
-
NFS版本:2.5.1
yum
install nfs-utils rpcbind
2. 创建共享目录并授权
mkdir /mnt/es
chown -R elastic:elastic /mnt/es
3. 修改NFS配置文件
vi /etc/exports
/mnt/es *(rw,sync,no_root_squash,no_subtree_check)
4. 启动NFS服务并设置开启自启动
systemctl start rpcbind
systemctl start nfs-server
systemctl enable rpcbind
systemctl enable nfs-server
5. 客户端挂载NFS共享目录(源目标ES的所有节点都需要配置)
mkdir /mnt/es
chown -R elastic:elastic /mnt/es
mount -t nfs 10.78.23.111:/mnt/es /mnt/es
showmount -e 10.78.23.111
6. 功能验证,在任意一台es节点的挂载目录上创建文件夹并新建文件,查看是否同步到所有的es节点上,可借助ansible的功能批量检查源和目标上所有的es节点的机器。如下:
mkdir /mnt/es/test/1.txt
echo "123" > /mnt/es/test/.1txt
ls -l /mnt/es/test/1.txt
ansible -i /etc/ansible/hosts es-qy-all -m shell -a 'ls -l /mnt/es/test/1.txt'
备注:须确保源集群和目标集群的共享目录的用户和用户组id保持一致,否则数据恢复会有权限问题。
# 创建用户组并设置组id为1010
groupadd -g 1010 elastic
#常见用户id为1010,并加入到组id为1010的车
useradd -u 1010 -g 1010 -G elastic elastic
二、ES集群和Kibana部署
略(后续会单独出一期ES分布式集群的部署教程)
04
—
操作过程
一、全量备份1. 源端集群插入测试数据(生产环境真实数据迁移可忽略)
1.1 创建索引(测试数据以es_test_index索引为例,生产环境需替换为真实的索引数据)如下:
PUT es_test_index
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"title": {
"type": "text"
},
"content": {
"type": "text"
},
"author": {
"type": "keyword"
}
}
}
}
1.2 插入数据
POST /es_test_index/_bulk
{ "index": { "_id": "1" } }
{ "title": "title1", "content": "a123456", "author": "张三1" }
{ "index": { "_id": "2" } }
{ "title": "title2", "content": "b123456", "author": "张三2" }
{ "index": { "_id": "3" } }
{ "title": "title3", "content": "c123456", "author": "张三3" }
{ "index": { "_id": "4" } }
{ "title": "title4", "content": "d123456", "author": "张三4" }
{ "index": { "_id": "5" } }
{ "title": "title5", "content": "e123456", "author": "张三5" }
1.3 查看数据
GET es_test_index/_search
"query": {
"match_all": {
}
}
}
2. 源端集群创建仓库(Repository)
2.1 在es的配置文件末尾添加此配置
vim elasticsearch.yml
path.repo: ["/mnt/es"]
2.2 注册快照存储库
PUT _snapshot/es_backup_repo
{
"type": "fs",
"settings": {
"location": "/mnt/es",
"compress": true
}
}
2.3 查看快照存储库
GET _snapshot
3. 源端集群创建快照(Snapshot)
3.1 创建快照
PUT _snapshot/es_backup_repo/es_snapshot
{
"indices": "es_test_index",
"ignore_unavailable": true,
"include_global_state": true
}
备注:如需备份多个索引SQL如下:
PUT _snapshot/my_backup_repo/my_snapshot?wait_for_completion=true
{
"indices": "my_index1,my_index2",
"ignore_unavailable": true,
"include_global_state": true
}
-
wait_for_completion=true时会一直阻塞直到快照完成,kibana执行时超时为30秒,超时后快照依然会在后。台执行。
-
ignore_unavailable为创建快照时忽略不存在的索引。
-
indices为指定需要备份的索引(多个用逗号隔开),不指定则会备份所有的es索引库数据。
3.2 查看快照信息
GET _snapshot/es_backup_repo/es_snapshot
3.3 查看所有快照的全部信息
GET _snapshot/es_backup_repo/_all?pretty
4. 目标集群恢复全量数据
4.1 在目标集群查看共享的快照备份数据(可借助ansible批量查看)
ls -l /mnt/es
4.2 目标节点全部添加仓库配置
vim elasticsearch.yml
path.repo: ["/mnt/es"]
4.3 目标ES集群创建快照库
PUT _snapshot/es_backup_repo
{
"type": "fs",
"settings": {
"location": "/mnt/es",
"compress": true
}
}
4.4 执行数据恢复
POST _snapshot/es_backup_repo/es_snapshot/_restore
5. 在目标集群验证数据是恢复
GET es_test_index/_search
{
"query": {
"match_all": {}
}
}
二、增量备份
1. 插入增量数据(生产环境真实数据迁移可忽略)
1.1在上面的es_test_index插入增量数据
POST /es_test_index/_bulk
{ "index": { "_id": "11" } }
{ "title": "title11", "content": "a1123456", "author": "张三11" }
{ "index": { "_id": "12" } }
{ "title": "title12", "content": "b1123456", "author": "张三12" }
{ "index": { "_id": "13" } }
{ "title": "title13", "content": "c1123456", "author": "张三13" }
{ "index": { "_id": "14" } }
{ "title": "title14", "content": "d1123456", "author": "张三14" }
{ "index": { "_id": "15" } }
{ "title": "title15", "content": "e1123456", "author": "张三15" }
1.2 新增一个新的es索引es_test_index_1,并插入增量数据
PUT es_test_index_1
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"title": {
"type": "text"
},
"content": {
"type": "text"
},
"author": {
"type": "keyword"
}
}
}
}
POST /es_test_index_1/_bulk
{ "index": { "_id": "1" } }
{ "title": "title21", "content": "a2123456", "author": "张三21" }
{ "index": { "_id": "2" } }
{ "title": "title22", "content": "b2123456", "author": "张三22" }
{ "index": { "_id": "3" } }
{ "title": "title23", "content": "c2123456", "author": "张三23" }
{ "index": { "_id": "4" } }
{ "title": "title24", "content": "d2123456", "author": "张三24" }
{ "index": { "_id": "5" } }
{ "title": "title25", "content": "e2123456", "author": "张三25" }
2. 源端创建快照(Snapshot)
PUT _snapshot/es_backup_repo/es_snapshot_1
{
"indices": "es_test_index,es_test_index_1",
"ignore_unavailable": true,
"include_global_state": true
}
3. 目标集群恢复增量数据
3.1 查看快照信息
GET _snapshot/es_backup_repo/_all
3.2 关闭目标集群和源集群有重复的es索引,目标集群没有的无需关闭
POST es_test_index/_close
3.3 目标集群执行数据恢复
POST _snapshot/es_backup_repo/es_snapshot_1/_restore
3.4 打开上面关闭的索引
POST es_test_index/_open
4. 目标集群验证es数据是否增量同步完成
GET es_test_index/_search
{
"query": {
"match_all": {}
}
}
GET es_test_index_1/_search
{
"query": {
"match_all": {}
}
}
05
—
企业案例1. 定时恢复增量数据到目标集群
#!/bin/bash
ES_ENDPOINT="http://10.78.23.111:9200"
REPOSITORY_NAME="my_backup_repo"
SNAPSHOT_DATE_FORMAT="daily_snapshot_%Y%m%d"
RESTORE_INDEX_PATTERN=""
# 获取前一天日期作为快照恢复的时间戳
YESTERDAY=$(date --date='1 day ago' +%Y%m%d)
SNAPSHOT_TO_RESTORE="${SNAPSHOT_DATE_FORMAT//%Y%m%d/$YESTERDAY}"
RESTORED_INDICES_PATTERN="^(${SNAPSHOT_TO_RESTORE}_.*|.*$)" # 匹配快照恢复后的索引名称
# 检查昨天的快照是否存在
SNAPSHOT_EXISTS=$(curl -s -u elastic:elastic "${ES_ENDPOINT}/_snapshot/${REPOSITORY_NAME}/${SNAPSHOT_TO_RESTORE}" | jq -e '.snapshots | length > 0')
if [ "$SNAPSHOT_EXISTS" = "true" ]; then
echo "Closing indices that will be affected by the restore process..."
# 获取将要恢复的索引列表并关闭它们
AFFECTED_INDICES=$(curl -s -u elastic:elastic "${ES_ENDPOINT}/_cat/indices?v&h=index" | grep -E $RESTORED_INDICES_PATTERN)
for INDEX_NAME in $AFFECTED_INDICES; do
curl -X POST "${ES_ENDPOINT}/${INDEX_NAME}/_close" -u elastic:elastic
done
echo "Restoring snapshot ${SNAPSHOT_TO_RESTORE}..."
curl -X POST "${ES_ENDPOINT}/_snapshot/${REPOSITORY_NAME}/${SNAPSHOT_TO_RESTORE}/_restore" \
-u elastic:elastic \
-H 'Content-Type: application/json' \
-d'{
"indices": "*",
"ignore_unavailable": true,
"include_global_state": true
}'
echo "Snapshot ${SNAPSHOT_TO_RESTORE} restored successfully."
echo "Opening the restored indices..."
# 重新打开已恢复的索引
for INDEX_NAME in $AFFECTED_INDICES; do
curl -X POST "${ES_ENDPOINT}/${INDEX_NAME}/_open" -u elastic:elastic
done
echo "Affected indices opened successfully."
else
echo "Snapshot ${SNAPSHOT_TO_RESTORE} does not exist. Skipping restore process."
fi
exit 0
脚本获取方式
后台回复:es迁移脚本
后台回复:es迁移脚本
后台回复:es迁移脚本
MUSIC
♬..♩~ ♫. ♪..
END
推荐阅读
Jenkins磁盘空间自动清理
ES集群密码遗忘?看这篇就够了给力!利用Python导出ZABBIX资产指标清单
超实用!利用Nginx实现文件下载,效率翻倍容器化部署最新版ZABBIX监控系统