ElasticSearch僞集羣搭建之漸行漸遠

業務背景:

生產環境原先爲Solr,因爲Solr的性能問題,故現在拋棄它,團隊決定使用ElasticSearch。先說說ElasticSearch和Solr的對比。

Solr的優缺點:

優點:

  • Solr有一個更大、更成熟的用戶、開發和貢獻者社區。.
  • 支持添加多種格式的索引,如:HTML、PDF、微軟 Office 系列軟件格式以及 JSON、XML、CSV 等純文本格式。
  • Solr比較成熟、穩定。
  • 不考慮建索引的同時進行搜索,速度更快。

    缺點:

  • 建立索引時,搜索效率下降,實時索引搜索效率不高。

    ElasticSearch的優缺點:

    優點:

  • Elasticsearch是分佈式的。不需要其他組件,分發是實時的,被叫做”Push replication”。
  • Elasticsearch 完全支持 Apache Lucene 的接近實時的搜索。
  • 處理多租戶(multitenancy)不需要特殊配置,而Solr則需要更多的高級設置。
  • Elasticsearch 採用 Gateway 的概念,使得完備份更加簡單。
  • 各節點組成對等的網絡結構,某些節點出現故障時會自動分配其他節點代替其進行工作。

    缺點:

  • 只有一名開發者(當前Elasticsearch GitHub組織已經不只如此,已經有了相當活躍的維護者)
  • 還不夠自動(不適合當前新的Index Warmup API)

    ElasticSearch簡介

    以下內容引用自:http://i.zhcy.tk/blog/elasticsearchyu-solr/
    Elasticsearch是一個實時的分佈式搜索和分析引擎。它可以幫助你用前所未有的速度去處理大規模數據。
    它可以用於全文搜索,結構化搜索以及分析,當然你也可以將這三者進行組合。
    Elasticsearch是一個建立在全文搜索引擎 Apache Lucene™ 基礎上的搜索引擎,可以說Lucene是當今最先進,最高效的全功能開源搜索引擎框架。
    但是Lucene只是一個框架,要充分利用它的功能,需要使用JAVA,並且在程序中集成Lucene。需要很多的學習瞭解,才能明白它是如何運行的,Lucene確實非常複雜。
    Elasticsearch使用Lucene作爲內部引擎,但是在使用它做全文搜索時,只需要使用統一開發好的API即可,而不需要了解其背後複雜的Lucene的運行原理。
    當然Elasticsearch並不僅僅是Lucene這麼簡單,它不但包括了全文搜索功能,還可以進行以下工作:

    1. 分佈式實時文件存儲,並將每一個字段都編入索引,使其可以被搜索。
    2. 實時分析的分佈式搜索引擎。
    3. 可以擴展到上百臺服務器,處理PB級別的結構化或非結構化數據。
      這麼多的功能被集成到一臺服務器上,你可以輕鬆地通過客戶端或者任何你喜歡的程序語言與ES的RESTful API進行交流。
      Elasticsearch的上手是非常簡單的。它附帶了很多非常合理的默認值,這讓初學者很好地避免一上手就要面對複雜的理論,
      它安裝好了就可以使用了,用很小的學習成本就可以變得很有生產力。
      隨着越學越深入,還可以利用Elasticsearch更多高級的功能,整個引擎可以很靈活地進行配置。可以根據自身需求來定製屬於自己的Elasticsearch。
      使用案例:
  • 維基百科使用Elasticsearch來進行全文搜做並高亮顯示關鍵詞,以及提供search-as-you-type、did-you-mean等搜索建議功能。
  • 英國衛報使用Elasticsearch來處理訪客日誌,以便能將公衆對不同文章的反應實時地反饋給各位編輯。
  • StackOverflow將全文搜索與地理位置和相關信息進行結合,以提供more-like-this相關問題的展現。
  • GitHub使用Elasticsearch來檢索超過1300億行代碼。
  • 每天,Goldman Sachs使用它來處理5TB數據的索引,還有很多投行使用它來分析股票市場的變動。
    但是Elasticsearch並不只是面向大型企業的,它還幫助了很多類似DataDog以及Klout的創業公司進行了功能的擴展。

    廢話不多說來看看這個是怎麼搭建的----搭建篇

    先說說搭建過程中需要注意到的坑,-------很坑人的。

    坑①:啓動Elasticsearch的時候不能用root啓動
    坑②:從Elasticsearch目錄開始後面所有的目錄屬主和屬組都要是你啓動Elasticsearch時候用的用戶和用戶所在的組
    坑③:jdk版本要使用8以及以上
    坑④:系統允許打開的最大文件數設置成65536,如果設置成65535還會提醒你設置的。
    坑⑤:系統允許打開的最大線程數設置成65536,如果設置成65535還會提醒你設置的。有點囉嗦。沒辦法小編就是一個仔細的人。
    坑⑥:當你買過了坑④和坑⑤的時候,你會發現,切換到啓動用戶後,還是會報錯,沒錯,就是會報錯,因爲你在root下設置的系統參數在非root下不生效,要想一次性生效,自己百度吧,時間關係,小編沒有弄。
    坑⑦:因爲這個東西絕對是有錢人玩的東西,所以默認jvm的內存給的很大,默認1G。所以我們要修改這個值。這裏先不說如何修改,後面說,先記着。
    所有的坑都說過了,接下來說說怎麼安裝,以及安裝的過程和步驟。

    搭建過程

    首先要下載ElasticeSearch源程序,感謝各位開源的大神,收下小弟的膝蓋。

    下載地址:
    https://www.elastic.co/downloads 下載的時候看清楚了哦,要下載Elasticsearch(小編開始的時候問老大這個行不行,老大給了我一個眼神,叫我自己體會)
    點擊這裏下載
    ElasticSearch僞集羣搭建之漸行漸遠

    配置jdk環境變量

    怎麼配置環境就不說了,很簡單。

    解壓程序,並分別修改每個節點的配置文件

    目錄結構是這樣的

    
    root@bogon:/Disk/ES/elasticsearch01# tree
    .
    ├── bin
    │   ├── elasticsearch
    │   ├── elasticsearch.bat
    │   ├── elasticsearch-env
    │   ├── elasticsearch-env.bat
    │   ├── elasticsearch-keystore
    │   ├── elasticsearch-keystore.bat
    │   ├── elasticsearch-plugin
    │   ├── elasticsearch-plugin.bat
    │   ├── elasticsearch-service.bat
    │   ├── elasticsearch-service-mgr.exe
    │   ├── elasticsearch-service-x64.exe
    │   ├── elasticsearch-translog
    │   ├── elasticsearch-translog.bat
    │   ├── hs_err_pid1167.log
    │   ├── hs_err_pid1209.log
    │   ├── hs_err_pid5775.log
    │   └── hs_err_pid5816.log
    ├── config
    │   ├── elasticsearch.yml   # ES主要配置文件
    │   ├── jvm.options # 該文件設置jvm的相應參數,包括垃圾回收策略和虛擬機內存。
                                      #一般可以設置Xms和Xmx
    │   └── log4j2.properties
    ├── data
    │   └── nodes
    │       └── 0
    │           ├── indices
    │           │   └── wUOA5MkQSL-YRUP6jKx1uQ
    │           │       ├── 1
    │           │       │   ├── index
    │           │       │   │   ├── segments_1
    │           │       │   │   └── write.lock
    │           │       │   ├── _state
    │           │       │   │   └── state-0.st
    │           │       │   └── translog
    │           │       │       ├── translog-1.tlog
    │           │       │       └── translog.ckp
    │           │       ├── 3
    │           │       │   ├── index
    │           │       │   │   ├── segments_2
    │           │       │   │   └── write.lock
    │           │       │   ├── _state
    │           │       │   │   └── state-0.st
    │           │       │   └── translog
    │           │       │       ├── translog-1.tlog
    │           │       │       └── translog.ckp
    │           │       ├── 4
    │           │       │   ├── index
    │           │       │   │   ├── segments_1
    │           │       │   │   └── write.lock
    │           │       │   ├── _state
    │           │       │   │   └── state-0.st
    │           │       │   └── translog
    │           │       │       ├── translog-1.tlog
    │           │       │       └── translog.ckp
    │           │       └── _state
    │           │           └── state-5.st
    │           ├── node.lock
    │           └── _state
    │               ├── global-1.st
    │               └── node-9.st
    ├── lib
    │   ├── elasticsearch-6.1.0.jar
    │   ├── elasticsearch-cli-6.1.0.jar
    │   ├── HdrHistogram-2.1.9.jar
    │   ├── hppc-0.7.1.jar
    │   ├── jackson-core-2.8.10.jar
    │   ├── jackson-dataformat-cbor-2.8.10.jar
    │   ├── jackson-dataformat-smile-2.8.10.jar
    │   ├── jackson-dataformat-yaml-2.8.10.jar
    │   ├── java-version-checker-6.1.0.jar
    │   ├── jna-4.4.0-1.jar
    │   ├── joda-time-2.9.5.jar
    │   ├── jopt-simple-5.0.2.jar
    │   ├── jts-1.13.jar
    │   ├── log4j-1.2-api-2.9.1.jar
    │   ├── log4j-api-2.9.1.jar
    │   ├── log4j-core-2.9.1.jar
    │   ├── lucene-analyzers-common-7.1.0.jar
    │   ├── lucene-backward-codecs-7.1.0.jar
    │   ├── lucene-core-7.1.0.jar
    │   ├── lucene-grouping-7.1.0.jar
    │   ├── lucene-highlighter-7.1.0.jar
    │   ├── lucene-join-7.1.0.jar
    │   ├── lucene-memory-7.1.0.jar
    │   ├── lucene-misc-7.1.0.jar
    │   ├── lucene-queries-7.1.0.jar
    │   ├── lucene-queryparser-7.1.0.jar
    │   ├── lucene-sandbox-7.1.0.jar
    │   ├── lucene-spatial3d-7.1.0.jar
    │   ├── lucene-spatial-7.1.0.jar
    │   ├── lucene-spatial-extras-7.1.0.jar
    │   ├── lucene-suggest-7.1.0.jar
    │   ├── plugin-cli-6.1.0.jar
    │   ├── securesm-1.2.jar
    │   ├── snakeyaml-1.17.jar
    │   ├── spatial4j-0.6.jar
    │   └── t-digest-3.0.jar
    ├── LICENSE.txt
    ├── logs
    │   ├── elasticsearch_deprecation.log
    │   ├── elasticsearch_index_indexing_slowlog.log
    │   ├── elasticsearch_index_search_slowlog.log
    │   ├── elasticsearch.log
    │   ├── jlj-es_deprecation.log
    │   ├── jlj-es_index_indexing_slowlog.log
    │   ├── jlj-es_index_search_slowlog.log
    │   └── jlj-es.log
    ├── modules
    │   ├── aggs-matrix-stats
    │   │   ├── aggs-matrix-stats-6.1.0.jar
    │   │   └── plugin-descriptor.properties
    │   ├── analysis-common
    │   │   ├── analysis-common-6.1.0.jar
    │   │   └── plugin-descriptor.properties
    │   ├── ingest-common
    │   │   ├── ingest-common-6.1.0.jar
    │   │   ├── jcodings-1.0.12.jar
    │   │   ├── joni-2.1.6.jar
    │   │   └── plugin-descriptor.properties
    │   ├── lang-expression
    │   │   ├── antlr4-runtime-4.5.1-1.jar
    │   │   ├── asm-5.0.4.jar
    │   │   ├── asm-commons-5.0.4.jar
    │   │   ├── asm-tree-5.0.4.jar
    │   │   ├── lang-expression-6.1.0.jar
    │   │   ├── lucene-expressions-7.1.0.jar
    │   │   ├── plugin-descriptor.properties
    │   │   └── plugin-security.policy
    │   ├── lang-mustache
    │   │   ├── compiler-0.9.3.jar
    │   │   ├── lang-mustache-6.1.0.jar
    │   │   ├── plugin-descriptor.properties
    │   │   └── plugin-security.policy
    │   ├── lang-painless
    │   │   ├── antlr4-runtime-4.5.3.jar
    │   │   ├── asm-debug-all-5.1.jar
    │   │   ├── lang-painless-6.1.0.jar
    │   │   ├── plugin-descriptor.properties
    │   │   └── plugin-security.policy
    │   ├── mapper-extras
    │   │   ├── mapper-extras-6.1.0.jar
    │   │   └── plugin-descriptor.properties
    │   ├── parent-join
    │   │   ├── parent-join-6.1.0.jar
    │   │   └── plugin-descriptor.properties
    │   ├── percolator
    │   │   ├── mapper-extras-6.1.0.jar
    │   │   ├── percolator-6.1.0.jar
    │   │   └── plugin-descriptor.properties
    │   ├── reindex
    │   │   ├── commons-codec-1.10.jar
    │   │   ├── commons-logging-1.1.3.jar
    │   │   ├── elasticsearch-rest-client-6.1.0.jar
    │   │   ├── httpasyncclient-4.1.2.jar
    │   │   ├── httpclient-4.5.2.jar
    │   │   ├── httpcore-4.4.5.jar
    │   │   ├── httpcore-nio-4.4.5.jar
    │   │   ├── plugin-descriptor.properties
    │   │   ├── plugin-security.policy
    │   │   └── reindex-6.1.0.jar
    │   ├── repository-url
    │   │   ├── plugin-descriptor.properties
    │   │   ├── plugin-security.policy
    │   │   └── repository-url-6.1.0.jar
    │   ├── transport-netty4
    │   │   ├── netty-buffer-4.1.13.Final.jar
    │   │   ├── netty-codec-4.1.13.Final.jar
    │   │   ├── netty-codec-http-4.1.13.Final.jar
    │   │   ├── netty-common-4.1.13.Final.jar
    │   │   ├── netty-handler-4.1.13.Final.jar
    │   │   ├── netty-resolver-4.1.13.Final.jar
    │   │   ├── netty-transport-4.1.13.Final.jar
    │   │   ├── plugin-descriptor.properties
    │   │   ├── plugin-security.policy
    │   │   └── transport-netty4-6.1.0.jar
    │   └── tribe
    │       ├── plugin-descriptor.properties
    │       └── tribe-6.1.0.jar
    ├── NOTICE.txt
    ├── plugins
    └── README.textile

38 directories, 143 files

##### 看看主配置文件都怎麼修改的吧

root@bogon:/Disk/ES/elasticsearch01/config# cat elasticsearch.yml
#集羣名稱
cluster.name: jlj-es
#節點名稱
node.name: jlj-es01
#是不是爲主節點
node.master: true
#數據路徑
#path.data: /path/to/data
#log路徑
#path.logs: /path/to/logs
#bootstrap.memory_lock: true
#綁定地址,默認的地址是127.0.0.1,要修改不然http訪問不到的哦
network.host: 192.168.1.202
#監聽端口,默認9200
#http.port: 9200

用於集羣通訊的端口,此端口和9200端口是兩個概念; 給Java或者其它節點的服務端口,代碼裏面用這個。

transport.tcp.port: 9300
#集羣地址
discovery.zen.ping.unicast.hosts: ["192.168.1.202:9300","192.168.1.202:9310", "192.168.1.202:9320"]
#列腦值
discovery.zen.minimum_master_nodes: 2
#多少個節點啓動後集羣才能正常啓動
gateway.recover_after_nodes: 1
#允許跨域請求
http.cors.enabled: true
http.cors.allow-origin: "*"

##### 修改默認jvm內存大小
首先註釋掉配置文件 jvm.options中的內存大小。

17 ################################################################
18
19 # Xms represents the initial size of total heap space
20 # Xmx represents the maximum size of total heap space
21
22 #-Xms1g
23 #-Xmx1g 註釋掉這兩行
24
25 ################################################################

修改啓動腳本的jvm配置: bin/elasticsearch
`ES_JAVA_OPTS="-Xms128m -Xmx128m"`
##### 好了,到這裏一個單點的ES就配置完了。
因爲俺配置的僞集羣所以這裏建議直接cp這個文件夾,然後修改端口和節點名稱就可以了。
注意:前方高能----->需要修改的都在配置文件裏,這裏分別列出三個節點的配置文件內容:
ES1

#集羣名稱
cluster.name: jlj-es
#節點名稱
node.name: jlj-es01
node.master: true
#node.attr.rack: r1
#數據路徑
#path.data: /path/to/data
#log路徑
#path.logs: /path/to/logs
#bootstrap.memory_lock: true
#綁定地址
network.host: 192.168.1.202
#監聽端口
#http.port: 9200
transport.tcp.port: 9300
#集羣地址
discovery.zen.ping.unicast.hosts: ["192.168.1.202:9300","192.168.1.202:9310", "192.168.1.202:9320"]
#列腦值
discovery.zen.minimum_master_nodes: 2
#多少個節點啓動後集羣才能正常啓動
gateway.recover_after_nodes: 1
#action.destructive_requires_name: true
#允許跨域請求
http.cors.enabled: true
http.cors.allow-origin: "*"

ES2

#集羣名稱
cluster.name: jlj-es
#節點名稱
node.name: jlj-es02
#node.attr.rack: r2
#數據路徑
#path.data: /path/to/data
#log路徑
#path.logs: /path/to/logs
#bootstrap.memory_lock: true
#綁定地址
network.host: 192.168.1.202
#監聽端口
http.port: 9201
transport.tcp.port: 9310
#集羣地址
discovery.zen.ping.unicast.hosts: ["192.168.1.202:9300","192.168.1.202:9310", "192.168.1.202:9320"]
#列腦值
discovery.zen.minimum_master_nodes: 2
#多少個節點啓動後集羣才能正常啓動
gateway.recover_after_nodes: 1
#action.destructive_requires_name: true
#允許跨域請求
http.cors.enabled: true
http.cors.allow-origin: "*"

ES3

#集羣名稱
cluster.name: jlj-es
#節點名稱
node.name: jlj-es03
#node.attr.rack: r3
#數據路徑
#path.data: /path/to/data
#log路徑
#path.logs: /path/to/logs
#bootstrap.memory_lock: true
#綁定地址
network.host: 192.168.1.202
#監聽端口
http.port: 9202
transport.tcp.port: 9320
#集羣地址
discovery.zen.ping.unicast.hosts: ["192.168.1.202:9300","192.168.1.202:9310", "192.168.1.202:9320"]
#列腦值
discovery.zen.minimum_master_nodes: 2
#多少個節點啓動後集羣才能正常啓動
gateway.recover_after_nodes: 1
#action.destructive_requires_name: true
#允許跨域請求
http.cors.enabled: true
http.cors.allow-origin: "*"

#### 創建用戶、修改文件夾以及文件屬性這裏就不闡述了
修改系統可打開最大文件數和最大進程數命令,至於如何永久生效,自己google吧!

#修改可打開的最大文件數
ulimit -n 65536
#修改可打開的最大進程數
ulimit -u 65536

#### 可視化,安裝head插件:
小編使用的系統是ubuntu,只在此係統中實操過具體過程可以保證沒有問題,看其他博友在centos中也有安裝過的,不保證不報錯哦。
##### ubuntu 14.04安裝nmp插件過程
安裝過程借鑑:https://www.howtoing.com/install-latest-nodejs-npm-on-ubuntu

$ apt-get update
$ sudo apt-get install nodejs

驗證

root@bogon:/Disk/ES/elasticsearch03/config# nodejs -v
v6.12.2
root@bogon:/Disk/ES/elasticsearch03/config# npm -v
3.10.10

安裝git命令
`apt-get install git`
從git上克隆head代碼

git clone git://github.com/mobz/elasticsearch-head.git

進入到克隆下來的head目錄,使用nodejs安裝head插件

npm install
#大概過程如下
root@bogon:/Disk/ES/aa/elasticsearch-head# npm install
npm WARN deprecated [email protected]: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
npm WARN deprecated [email protected]: Use the built-in module in node 9.0.0 or newer, instead
[ ............] / fetchMetadata: WARN deprecated [email protected]: Use the built-in module in node 9.0.0 or newer, instead
#修改Gruntfile.js文件
90 connect: {
91 server: {
92 options: {
93 hostname: '192.168.1.202', #添加這行代碼,切記最後又逗號,結尾不要有空格。
94 port: 9100,
95 base: '.',
96 keepalive: true
97 }
98 }
99 }
100
101 });
#修改_site/app.js 文件
4353 this.prefs = services.Preferences.instance();
4354 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.1.202:9200"; # 這裏寫上本機的ip地址和ES master的端口號
4355 if( this.base_uri.charAt( this.base_uri.length - 1 ) !== "/" ) {
4356 // XHR request fails if the URL is not ending with a "/"
4357 this.base_uri += "/";
4358 }


# OK,完事具備,諸葛亮之啓動三個ES節點和head插件
啓動ES節點
分別進入到bin目錄下執行./elasticsearch,當然你也可以使用-d叫他後臺運行。
啓動head插件
npm run start
如果一切順利的話,訪問head插件中Gruntfile.js配置的ip和接口就能看到想要的界面了。
**~~這個圖片爲盜圖~~**
![](https://s1.51cto.com/images/blog/201712/20/b648b3e469f9807a98073fd85d6b7c82.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

未完,待續----
head插件的漢化版本
所有接口的調用關係
遇到的報錯截圖以及處理
Centos 安裝head插件過程

本文的成功編寫,離不開廣大博友的支持:
友情連接如下:
 ElasticSearch 安裝 (單機單節點/僞集羣) http://chenzhijun.me/2017/12/01/elasticsearch-install/
 elasticsearch集羣搭建  http://blog.csdn.net/guduyishuai/article/details/78702871
 elasticsearch head插件安裝 http://blog.csdn.net/guduyishuai/article/details/78680881
 elasticsearch單點搭建 http://blog.csdn.net/guduyishuai/article/details/78649376
 在Ubuntu 14.04上設置生產環境可用的Node.js http://blog.csdn.net/zstack_org/article/details/53100478
 如何在Ubuntu 16.04和14.04安裝最新NodeJS與Npm https://www.howtoing.com/install-latest-nodejs-npm-on-ubuntu
 linux下進程的進程最大數、最大線程數、進程打開的文件數和ulimit命令修改硬件資源限制 http://blog.csdn.net/gatieme/article/details/51058797
 Elasticsearch 服務配置屬性 https://www.ibm.com/support/knowledgecenter/zh/SSFPJS_8.5.7/com.ibm.wbpm.main.doc/topics/rfps_esearch_configoptions.html

                                                                                                                                 2017年12月20日 深夜
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章