ES集群版本升级之基于索引重建策略进行1.x到5.x的跨多个大版本的升级
1 | es只能使用上一个大版本创建的索引。举例来说,es 5.x可以使用es 2.x中的索引,但是不能使用es 1.x中的索引。 |
1 | 同时运行一个es 1.x的集群,同时也运行一个es 5.x的集群,然后用reindex功能,将es 1.x中的所有数据都导入到es 5.x集群中 |
停掉之前elasticsearch的服务
1 | kill -SIGTERM 3448 |
删除掉之前elasticsearch的相关目录
1 | rm -rf /usr/local/elasticsearch |
1 | mkdir -p /etc/elasticsearch/ |
1 | chown -R elasticsearch:elasticsearch /etc/elasticsearch/ |
解压elasticsearch-1.7.4/elasticsearch-5.5.0压缩文件
1 | tar -zxvf elasticsearch-1.7.4.tar.gz -C /usr/local/ |
1 | chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-1.7.4 |
编辑elasticsearch-1.7.4的配置文件
1 | vi /usr/local/elasticsearch-1.7.4/config/elasticsearch.yml |

启动elasticsearch-1.7.4/
1 | cd /usr/local/elasticsearch-1.7.4 |

1 | curl -XGET 'http://localhost:9200/_cat/nodes?v' |

1 | curl -XPUT 'http://localhost:9200/forum/article/1?pretty' -d ' |

1 | curl -XGET 'http://localhost:9200/forum/article/1?pretty' |

编辑elasticsearch-5.5.0压缩文件
1 | vi /usr/local/elasticsearch-5.5.0/config/elasticsearch.yml |
编辑elasticsearch-5.5.0/elasticsearch.yml文件


remote cluster必须显示在elasticsearch.yml中列入白名单中,使用reindex.remote.whitelist属性
1 | reindex.remote.whitelist: ["localhost:9200"] |

编辑elasticsearch-5.5.0/jvm.options文件
1 | -Xms512m |
启动elasticsearch-5.5.0
1 | cd /usr/local/elasticsearch-5.5.0 |
1 | curl -XGET 'http://localhost:9201/_cat/nodes?v' |

reindex in place
1 | 将1.x中的索引reindex最简单的方法,就是用elasticsearch migration plugin去做reindex |
migration plugin中提供的reindex工具会执行以下操作:
1 | 创建新的索引,但是会将es版本号拼接到索引名称上,比如my_index-2.4.1,从旧的索引中拷贝mapping和setting。禁止新索引的refresh,并且将replica数量设置为0.主要是为了更高效的reindex |
将旧索引设置为只读,不允许新的数据写入旧索引中
从旧索引中,将所有的数据reindex到新索引中
对新索引的refresh_interval和number_of_replicas的值重新设置为旧索引中的值,并且等待索引变成green
将旧索引中存在的alias添加到新索引中
删除旧的索引
给新索引添加一个alia,使用旧索引的名称,比如将my_index设置为my_index-2.4.1的alia
此时,就可以有一份新的2.x的索引,可以在5.x中使用
(2)upgrading with reindex-from-remote
如果在运行1.x cluster,并且想要直接迁移到5.x,而不是先迁移到2.x,那么需要进行reindex-from-remote操作
es包含了向后兼容性的代码,从而允许上一个大版本的索引可以直接在这个版本中使用。如果要直接从1.x升级到5.x,我们就需要自己解决向后兼容性的问题。
首先我们需要先建立一个新的5.x的集群。5.x集群需要能够访问1.x集群的rest api接口。
对于每个我们想要迁移到5.x集群的1.x的索引,需要做下面这些事情:
在5.x中创建新的索引,以及使用合适的mapping和setting,将refresh_interval设置为-1,并且设置number_of_replica为0,主要是为了更快的reindex。
用reindex from remote的方式,在两个集群之间迁移index数据
1 | curl -XPOST 'http://localhost:9201/_reindex?pretty' -d ' |

remote cluster必须显示在elasticsearch.yml中列入白名单中,使用reindex.remote.whitelist属性
reinde过程中会使用的默认的on-heap buffer最大大小是100mb,如果要迁移的数据量很大,需要将batch size设置的很小,这样每次同步的数据就很少,使用size参数。还可以设置socket_timeout和connect_timeout,比如下面:
1 | curl -XGET 'http://localhost:9201/forum/article/1?pretty' |

1 | POST _reindex |
1 | 如果在后台运行reindex job,就是将wait_for_completion设置为false,那么reindex请求会返回一个task_id,后面可以用来监控这个reindex progress的进度,GET _tasks/TASK_ID |
本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/10/29/ES集群版本升级之基于索引重建策略进行1.x到5.x的跨多个大版本的升级/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
知识 & 情怀 | 二者兼得