【云上ELK系列】Logstash迁移Elasticsearch数据方法解读
Elasticsearch中數據搬遷是工程師們經常會做的,有時是為了集群遷移、有時是為了數據備份、有時是為了升級等等,遷移的方式也有很多種,比如說通過elasticsearch-dump、通過snapshot、甚至是通過reindex的方式來做。今天為大家介紹另一種方案:用Logstash實現Elasticsearch集群快速遷移
我們希望通過logstash來做數據遷移本身的原理很容易理解,通過logstash從源elasticsearch Cluster讀數據,寫入到目標elasticsearh
Cluster中,詳細操作如下:
在logstash的目錄下創建一個logstash的用于數據同步的conf文件
vim ./logstash-5.5.3/es-es.conf配置conf文件,由于我們只需要做index搬遷,所以目標Cluster和源Cluster的index命名相同即可。
input {elasticsearch {hosts => ["********your host**********"]user => "*******"password => "*********"index => "logstash-2017.11.07"size => 1000scroll => "1m"} } # 該部分被注釋,表示filter是可選的 filter { } output {elasticsearch {hosts => ["***********your host**************"]user => "********"password => "**********"index => "logstash-2017.11.07"} }conf文件配置完成后執行logstash
bin/logstash -f es-es.conf執行這句指令時,有時會遇到如下的報錯信息
[FATAL][logstash.runner] Logstash could not be started because there is already another instance using the configured data directory. If you wish to run multiple instances, you must change the "path.data" setting.這是因為當前的logstash版本不支持多個instance共享一個path.data,所以需要在啟動時,命令行里增加"--path.data PATH ",為不同實例指定不同的路徑
bin/logstash -f es-es.conf --path.data ./logs/如果執行順利,執行下面這個命令就可以在目標的elasticsearch中看到對應的index
curl -u username:password host:port/_cat/indices以上介紹了如何通過logstash來遷移elasticsearch中指定的index,下面介紹一個實用的場景:
**很多自建了Elasticsearch客戶最近都會關注到阿里云Elasticsearch這款產品。想要使用時卻遇到了一個如何把自建中的數據遷移到阿里云Elasticsearch的困惑。下面介紹一下如何通過logstash快速的搬遷云上自建的Elasticsearch中的index數據。
**
這個方案的邏輯很簡單,拆解開就是配置N個es-to-es的conf文件,但這樣做很繁瑣。其實logstash提供了批量做這件事情的能力,為此需要提前介紹三個重要概念:
- metadata:logstash 1.5版本之后,使用了metadata的概念,來描述一次event,并且允許被用戶修改,但是不會寫到event的結果中,對event的結果產生影響。除此之外,metadata將作為event的元數據描述信息,可以在input、filter、output三種插件的全執行周期內存活;
參考文檔《Make Your Config Cleaner and your Log Processing Faster with Logstash Metadata》
- docinfo:elasticsearch input插件中的一個參數,默認是false,官網上描述的原文是“If set, include Elasticsearch document information such as index, type, and the id in the event.”也就意味著設置了這個字段生效,會將index、type、id等信息全部記錄到event中去,也就是metadata中去,這也就意味著可以在整個event執行周期內,使用者可以隨意的使用index、type、id這些參數了;
- elasticsearch input插件中的index參數,支持通配符,可以用“*”這樣的模糊匹配通配符來表示所有對象;
由于metadata的特性,我們可以在output中直接“繼承”input中的index、type信息,并在目標Cluster中直接創建和源Cluster一摸一樣的index和type,甚至是id。
在整個過程中如果希望可以看到metadata信息,并且對其進行類debug的操作,需要在output中添加一個配置:
stdout { codec => rubydebug { metadata => true } }示例配置代碼如下:
input {elasticsearch {hosts => ["yourhost"]user => "**********"password => "*********"index => "*"#該通配符代表需要讀取所有index信息size => 1000scroll => "1m"codec => "json"docinfo => true} } # 該部分被注釋,表示filter是可選的 filter { }output {elasticsearch {hosts => ["yourhost"]user => "********"password => "********"index => "%{[@metadata][_index]}"}stdout { codec => rubydebug { metadata => true } }}執行后,logstash會將源Cluster中所有的index全部copy到目標Cluster中去,并將mapping信息攜帶過去,隨后開始逐步做index內的數據遷移。
建議:正式執行的時候
stdout { codec => rubydebug { metadata => true } }這個配置項建議去掉,否則會被滿屏的刷metadata信息。
加入釘釘技術討論群
阿里云Elasticsearch已正式發布啦,阿里云攜手Elastic開源官方聯合開發,集成5.5.3商業版本X-Pack功能,歡迎開通使用。
點擊了解更多產品信息
總結
以上是生活随笔為你收集整理的【云上ELK系列】Logstash迁移Elasticsearch数据方法解读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET 动态输出Javascri
- 下一篇: LINUX系统服务与管理(Service