推荐阅读
Helm3(K8S 资源对象管理工具)视频教程:https://edu.youkuaiyun.com/course/detail/32506
Helm3(K8S 资源对象管理工具)博客专栏:https://blog.youkuaiyun.com/xzk9381/category_10895812.html
本文原文链接:https://blog.youkuaiyun.com/xzk9381/article/details/116592938,转载请注明出处。如有发现文章中的任何问题,欢迎评论区留言。
这两天需要将 ES 集群中的一个索引迁移到另一个集群中,使用 logstash 来完成这次索引数据迁移的任务。两个 ES 集群的版本都是 7.4.2 版本,首先创建一个 logstash 任务文件,名称为 migrate.conf 内容如下:
input{
elasticsearch{
index => "source-index-000095"
hosts => ["10.16.12.206:9202","10.16.12.207:9202","10.16.12.208:9202","10.16.13.214:9202","10.16.13.215:9202"]
size => 1000
}
}
output{
elasticsearch{
hosts => ["10.19.74.41:9202","10.19.74.42:9202","10.19.74.43:9202","10.19.74.44:9202","10.19.74.45:9202"]
index => "dest-index-000001"
}
}
配置的内容很简单,首先是指定要迁移的源 ES 地址,并且指定要迁移的索引,使用 * 代表迁移所有的索引。然后指定迁移的目标地址,并且设置目标索引。这样源索引的数据都会被写入新的索引中。
编写完成后,使用如下命令启动 logstash 开始迁移任务:
/opt/logstash/bin/logstash -f /opt/logstash/pipeconf/migrate.conf --config.reload.automatic --http.port 23153 --path.data /opt/logstash/data/23153 --path.logs /opt/logstash/logs/23153
在成功启动 logstash 后,迁移的过程中如果一切顺利,控制台中不会输出任何信息,所以不要以为是出错误了,直接到 kibana 中查看索引的数据是否在增长就可以了。
如果需要迁移所有的索引,那么就在 input 中将 index 设置为 * 号,并且在 output 中使用 metadata 来获取源索引名称。在迁移一些不带有时间戳的数据时,logstash 默认会给其添加上时间戳,所以我们可以在 filter 中将其删掉,完整的配置文件内容如下:
input{
elasticsearch{
index => "skywalking*"
hosts => ["10.16.12.204:32124"]
size => 1000
scroll => "5m"
docinfo => true
docinfo_fields => "_index", "_id", "_type", "_routing"
}
}
filter {
mutate {
remove_field => ["@timestamp", "@version"]
}
}
output{
elasticsearch{
hosts => ["10.19.74.56:9202"]
index => "%{[@metadata][_index]}"
document_type => "%{[@metadata][_type]}"
document_id => "%{[@metadata][_id]}"
}
}
相关参数说明如下:
- docinfo:设置为true,将会提取ES文档的元信息,例如index、type和id
- docinfo_fields:指定文档元信息,默认不带routing元信息
- %{[@metadata][_index]}:使用源索引名称
- %{[@metadata][_type]}:使用源类型
- %{[@metadata][_id]}:使用源 id
实际的生产环境中,如果用到_routing这个字段,就需要迁移_routing,需要在 logstash 的 output 里指定 routing 字段,值是"%{@metadata}"(意思是保持跟来源索引一致),但同时也要在input中指定docinfo_fields并增加_routing,因为默认元信息只有_index,_type,_id