1, 方案介绍
Logstash 是一个开源的数据收集引擎, 具有近时传输数据的能力. 它可以统一过滤来自不同源的数据, 并按照我们自定义的配置文件将过滤的数据输出到目标数据源中. 本文介绍在腾讯云 CVM 上部署 Logstash 的方法, 并演示如何使用 Logstash 从阿里云 ES 迁移数据到腾讯云 ES 中.
2, 迁移版本匹配说明
由于腾讯云 ES 版本与阿里云 ES 版本不一致, 需要选择兼容的版本进行迁移, 本文支持的版本方案如下 (其他方案不保证兼容):
• 阿里云 ES 5.6 -> CVM(Logstash 6.0.x)-> 腾讯云 ES 5.6.4
• 阿里云 ES 6.3.2 -> CVM(Logstash 6.8.x)-> 腾讯云 ES 6.4.3
• 阿里云 ES 6.7.0/6.8.0 -> CVM(Logstash 6.8.x)-> 腾讯云 ES 6.8.2
• 阿里云 ES 7.4.0-> CVM(Logstash 7.6.x) -> 腾讯云 ES 7.5.1(本文以此为例)
注意: 由于腾讯云 ES 版本分别与阿里云 ES 版本不一致, 所以建议您在大版本内进行数据同步, 不建议跨大版本同步. 关于 Logstash 版本选取详情, 请参照官方说明:
3, 实施步骤
本案例以使用在腾讯云 CVM 上自建 Logstash 方式, 将 Elasticsearch 索引从阿里云 ES 迁移至腾讯云 ES 中为例, 具体实施步骤如下:
3.1, 创建阿里云 Elasticsearch 实例
1) 创建阿里云 ES 实例, 链接地址:
2) 开启公网访问地址, 允许腾讯云 CVM 能够公网访问 (在实际的生产环境中, 可以先通过专线将腾讯云和阿里云的内网打通, 再通过内网同步 ES 数据).
3) 创建模拟数据
在阿里云 ES kibana 控制台, 通过 Dev Tools 进行操作. 以下示例创建了一个名为 product_info 的索引, 指定分片数 和副本数量, 索引映射.
- PUT /product_info
- {
- "settings": {
- "number_of_shards": 5,
- "number_of_replicas": 1
- },
- "mappings": {
- "properties": {
- "productName": {"type": "text","analyzer": "ik_smart"},
- "annual_rate":{"type":"keyword"},
- "describe": {"type": "text","analyzer": "ik_smart"}
- }
- }
- }
在 Kibana 控制台中, 执行以下命令创建文档并插入数据, 使用批量插入数据的形式:
- POST /product_info/_doc/_bulk?pretty
- {
- "index":{
- }
- }
- {
- "productName":"大健康天天理财","annual_rate":"3.2200%","describe":"180 天定期理财, 最低 20000 起投, 收益稳定, 可以自助选择消息推送"
- }
- {
- "index":{
- }
- }
- {
- "productName":"西部通宝","annual_rate":"3.1100%","describe":"90 天定投产品, 最低 10000 起投, 每天收益到账消息推送"
- }
- {
- "index":{
- }
- }
- {
- "productName":"安详畜牧产业","annual_rate":"3.3500%","describe":"270 天定投产品, 最低 40000 起投, 每天收益立即到账消息推送"
- }
- {
- "index":{
- }
- }
- {
- "productName":"5G 设备采购月月盈","annual_rate":"3.1200%","describe":"90 天定投产品, 最低 12000 起投, 每天收益到账消息推送"
- }
- {
- "index":{
- }
- }
- {
- "productName":"新能源动力理财","annual rate":"3.0100%","describe":"30 天定投产品推荐, 最低 8000 起投, 每天收益会消息推送"
- }
- {
- "index":{
- }
- }
- {
- "productName":"微贷赚","annual_rate":"2.7500%","describe":"热门短期产品, 3 天短期, 无须任何手续费用, 最低 500 起投, 通过短信提示获取收益消息"
- }
3.2, 创建腾讯云 Elasticsearch 实例
腾讯云 ES 实例创建链接: https://console.cloud.tencent.com/es
3.3, 在腾讯云 Elasticsearch 上创建索引信息
注意: 在使用 Logstash 上传数据之前, 需要首先在目标集群手动定义 mapping 和 setting 信息, 虽然不自己建立, logstash 会自动建立, 但是自动建立出来的 mappings 里面有些参数可能就不是我们预期想要的, 导致再查询的时候出现问题.
- PUT /product_info
- {
- "settings": {
- "number_of_shards": 5,
- "number_of_replicas": 1
- },
- "mappings": {
- "properties": {
- "productName": {"type": "text","analyzer": "ik_smart"},
- "annual_rate":{"type":"keyword"},
- "describe": {"type": "text","analyzer": "ik_smart"}
- }
- }
- }
3.4, 创建一台用于自建 logstash 的 CVM
在腾讯云控制台购买一台 CVM 并绑定公网 IP.
注意: 需要购买与腾讯云 Elasticsearch 实例在同一 VPC 下的 CVM 实例, 确保 CVM 能够内网访问腾讯云 ES 实例.
3.5, 在 CVM 上安装 Logstash
使用 CVM 自建 Logstash 服务, 然后在 Logstash 的 conf 下配置相应文件, 并启动服务.
注意: 需要安装 JDK(1.8 及以上版本) 以及相应版本的 Logstash
1) 下载 7.6.1 版本的 Logstash
在 Elastic 官网中, 下载与您购买的腾讯云 ES 版本一致的 Logstash, 这里选择 7.6.1 版本
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.6.1.tar.gz
2) 对下载的 Logstash 压缩包进行解压缩到 / usr/local 目录
tar -xzvf logstash-7.6.1.tar.gz -C /usr/local
3) 创建目录
- mkdir /data/logstash # 创建数据存储目录
- mkdir /var/log/logstash # 创建日志存储目录
3.6, 使用 Logstash 同步数据
1) 配置 logstash.YAML 文件
配置数据存储路径, 配置文件目录, 日志输出路径等.
- path.data: /data/logstash #数据存储路径
- path.config: /usr/local/logstash-7.6.1/config/logstash.conf #配置文件目录
- path.logs: /var/log/logstash #日志输出路径
- queue.type: persisted # 基于磁盘 的 ACKed 队列
- path.queue: path.data/queue # 启用持久化队列时存储数据文件的目录路径
2) 配置 logstash.conf 文件
配置 schedule 每分钟触发抽取, 将阿里云 ES 所有索引数据同步到腾讯云 ES 集群中, logstash 配置文件如下:
- input {
- Elasticsearch {
- hosts => "http://es-cn-xxx.public.elasticsearch.aliyuncs.com:9200" # 阿里云 ES 外网地址
- user => "elastic" # 用户名
- index => "*" # 所有索引
- password => "xxxxxx" # 密码
- docinfo => true
- schedule => "* * * * *" #定时任务, 每分钟执行一次
- }
- }
- output {
- Elasticsearch {
- hosts => "http://xxxxxxx:9200" # 腾讯云 ES 内网地址
- user => "elastic" # 用户名
- password => "xxxxxx" # 密码
- index => "%{[@metadata][_index]}"
- document_type => "%{[@metadata][_type]}"
- document_id => "%{[@metadata][_id]}"
- }
- }
参数 | 说明 |
---|---|
hosts | ES 服务的访问地址。input 中为 http://< 阿里云 ES 公网地址 & gt;:< 端口 & gt;:output 中为 http:// 腾讯云 ES 实例 |
user | 访问 ES 服务的用户名 |
password | 访问 ES 服务的密码 |
index | 指定同步索引名,通配符 * 代表所有索引 |
3) 启动 logstash
在实际的生产环境中可以以 service 的方式运行, 以下通过 nohup 方式后台运行 logstash.
nohup ./bin/logstash &
命令执行成功后, 系统会自动通过 Logstash 将阿里云 ES 数据同步到腾讯云 ES 集群. 只要监控到阿里云 ES 有内容更新, 也会自动同步数据到腾讯 ES 集群中.
3.7, 验证数据同步结果
1) 登录目标腾讯云 ES 实例的 Kibana 控制台.
2) 单击左侧导航栏的 Dev Tools(开发工具).
3) 在 Console 中, 执行以下命令.
查看所有的索引信息:
GET _cat/indices
查看索引 product_info 文档内容:
GET /product_info/_search
可以看到在腾讯云 ES 上已经查询到索引 product_info 的文档信息内容了.
来源: https://www.qcloud.com/developer/article/1606495