ElasticSearch集羣部署、優化和中文分詞器ik部署

本節內容:

  • Elasticsearch介紹
  • Elasticsearch集羣安裝部署
  • Elasticsearch優化
  • 安裝插件:中文分詞器ik

 

一、Elasticsearch介紹

Elasticsearch是一個分佈式搜索服務,提供Restful API,底層基於Lucene,採用多shard的方式保證數據安全,並且提供自動resharding的功能,加之github等大型的站點也採用 Elasticsearch作爲其搜索服務。

 

二、Elasticsearch集羣安裝部署

1. 環境信息

主機名 操作系統版本 IP地址 安裝軟件
log1  CentOS 7.0 114.55.29.86 JDK1.7、elasticsearch-2.2.3
log2 CentOS 7.0 114.55.29.241 JDK1.7、elasticsearch-2.2.3
log3 CentOS 7.0 114.55.253.15 JDK1.7、elasticsearch-2.2.3

 

 

 

 

 

2. 安裝JDK1.8

版本是Elasticsearch 2.2.3,官方建議jdk是1.8。3臺機器都需要安裝jdk1.8,添加新用戶es。

[root@log1 local]# mkdir /usr/java
[root@log1 local]# tar zxf jdk-8u73-linux-x64.gz -C /usr/java/

3. 添加用戶

Elasticsearch不能使用root用戶去啓動。

[root@log1 local]# groupadd -g 510 es
[root@log1 local]# useradd -g 510 -u 510 es
[root@log1 local]# echo "wisedu123" | passwd --stdin es &> /dev/null

用新創建的用戶登錄shell,配置PATH環境變量。

[es@log1 ~]$ vim ~/.bashrc
export JAVA_HOME=/usr/java/jdk1.8.0_73
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[es@log1 ~]$ source ~/.bashr
# mkdir /usr/local/elasticsearch
# chown -R es.es elasticsearch

 

4. 下載安裝elasticsearch

es用戶登錄shell,下載安裝elasticsearch。

[es@log1 ~]$ cd /usr/local/elasticsearch/
[es@log1 elasticsearch]$ wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.2.3/elasticsearch-2.2.3.tar.gz 
[es@log1 elasticsearch]$ tar zxf elasticsearch-2.2.3.tar.gz 
[es@log1 elasticsearch]$ mv elasticsearch-2.2.1/* ./
[es@log1 elasticsearch]$ rm -rf elasticsearch-2.2.1
[es@log1 elasticsearch]$ rm -f elasticsearch-2.2.1.tar.gz

5. 配置elasticsearch

(1)配置elasticsearch 堆內存,編輯bin/elasticsearch.in.sh

[es@log1 elasticsearch]$ vim bin/elasticsearch.in.sh

將參數:ES_MIN_MEM、ES_MAX_MEM設置爲當前物理機內存的一半(注意單位,並保證兩個值相等)

 

(2)配置Elasticsearch集羣名稱以及節點名稱、是否爲主節點、path data等信息

[es@log1 elasticsearch]$ vim config/elasticsearch.yml

 

(3)配置保護Elasticsearch使用的內存防止其被swapped

在memory section下,啓用配置:bootstrap.mlockall: true

 

(4)配置network host

【注意】:另外,請在Network段在多加兩個配置,內容如下:

network.bind_host: 114.55.29.86
# Set the address other nodes will use to communicate with this node. If not 
# set, it is automatically derived. It must point to an actual IP address.
network.publish_host: 114.55.29.86

如果不加上如上的配置,程序在連接時會報錯:

^A[2016-03-28 16:18:08.791] [ERROR] [godseye] [godseye] [RMI TCP Connection(2)-127.0.0.1] [com.wisedu.godseye.search.util.SearchUtil] [buildIndex:70] NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{114.55.29.86}{114.55.29.86:9300}]]

 

(5)配置Elasticsearch的自動發現機制

 

另外兩臺也是做如上的安裝配置。只不過在配置中需要修改下面幾處。

 

三、Elasticsearch優化

1. 檢驗配置中的bootstrap.mlockall: true是否生效

啓動Elasticsearch:

[es@log1 elasticsearch]$ bin/elasticsearch -d

在shell終端執行命令:

curl http://114.55.29.86:9200/_nodes/process?pretty

關注這個這個請求返回數據中的mlockall的值,如果爲false,則說明鎖定內存失敗,這可能由於運行elasticsearch的用戶不具備這樣的權限。解決該問題的方法是: 在運行elasticsearch之前,以root身份執行:

ulimit -l unlimited

然後再次重啓elasticsearch。並查看上面的請求中的mlockall的值是否爲true。

【注意】:這時候需要在root執行ulimit -l unlimited的shell終端上su - es,然後重啓elasticsearch。因爲這是命令行設置的ulimit -l unlimited,只對當前會話生效。

[root@log1 ~]# ulimit -l unlimited
[root@log1 ~]# su - es
[es@log1 ~]$ ps -ef|grep elasticsearch
[es@log1 ~]$ kill -9 27189
[es@log1 ~]$ /usr/local/elasticsearch/bin/elasticsearch -d
[es@log1 ~]$ curl http://114.55.29.86:9200/_nodes/process?pretty

如果仍然是false,可能是下面的原因:

Another possible reason why mlockall can fail is that the temporary directory (usually /tmp) is mounted with the noexec option. This can be solved by specifying a new temp directory, by starting Elasticsearch with:
./bin/elasticsearch -Djna.tmpdir=/path/to/new/dir

要想永久修改鎖定內存大小無限制,需修改/etc/security/limits.conf,添加下面的內容,改完不需要重啓系統,但是需要重新打開一個shell建立會話。

es - memlock -1

其中,es代表運行elasticsearch的用戶,-表示同時設置了soft和hard,memlock代表設置的是”鎖定內存”這個類型,-1(unlimited或者infinity)代表沒限制。

 

2. 配置操作系統文件描述符數

查看elasticsearch能打開的最大文件描述符個數:

curl http://114.55.29.86:9200/_nodes/stats/process?pretty

查看參數:max_file_descriptors。推薦設置到32K甚至64K。

或者輸入下面的命令進行查看:

$ ulimit -a

設置需要修改:

vim /etc/security/limits.conf
es               -       nofile          65535

 

3. 增大虛擬內存mmap count配置

備註:如果你以.deb或.rpm包安裝,則默認不需要設置此項,因爲已經被自動設置,查看方式爲:

sysctl vm.max_map_count

如果是手動安裝,以root身份執行如下命令:

sysctl vm.max_map_count=262144

並修改文件使設置永久生效:

[root@log1 ~]# vim /etc/sysctl.conf

添加一行:

vm.max_map_count = 262144

使生效:

[root@log1 ~]# sysctl -p

改完後,重啓elasticsearch。 在瀏覽器輸入http://ip:9200/,查看頁面信息,是否正常啓動。

另外兩臺也需要做這些優化。

 

四、安裝插件:中文分詞器ik

elasticsearch-analysis-ik 是一款中文的分詞插件,支持自定義詞庫。項目地址爲:https://github.com/medcl/elasticsearch-analysis-ik

1. 安裝Maven

由於該項目使用了Maven來管理,源代碼放到github上。所以要先在服務器上面安裝Maven,便可以直接在服務器上面生成項目jar包,部署起來更加方便了。

[root@log1 ~]# yum install -y maven

 

2. 安裝ik

注意分詞插件的版本,2.2.3對應的插件版本是1.9.3。

打包生成ik

 

3. 拷貝和解壓

拷貝和解壓

 

4. 重啓elasticsearch

直接重啓就可以了,不需要在Elasticsearch中添加配置index.analysis.analyzer.ik.type : “ik”  。

重啓Elasticsearch

另外兩臺也需要解壓這個插件進去,重新啓動。

 

5. 分詞測試

(1)創建一個索引,名爲index

[es@log1 elasticsearch]$ curl -XPUT http://114.55.29.86:9200/index
{"acknowledged":true}

 

(2)index some docs

命令行輸入以下內容:

curl -XPOST http://114.55.29.86:9200/index/fulltext/1 -d'
{"content":"美國留給伊拉克的是個爛攤子嗎"}
'

curl -XPOST http://114.55.29.86:9200/index/fulltext/2 -d'
{"content":"公安部:各地校車將享最高路權"}
'

curl -XPOST http:// 114.55.29.86:9200/index/fulltext/3 -d'
{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
'

curl -XPOST http:// 114.55.29.86:9200/index/fulltext/4 -d'
{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}
'

(3)測試

命令行輸入:

curl -XPOST http://114.55.29.86:9200/index/fulltext/_search  -d'
{
        "query" : { "term" : { "content" : "中國" }},
        "highlight" : {
            "pre_tags" : ["<tag1>", "<tag2>"],
            "post_tags" : ["</tag1>", "</tag2>"],
            "fields" : {
                "content" : {}
            }
        }
}
'

結果:

{"took":74,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.5,"hits":[{"_index":"index","_type":"fulltext","_id":"4","_score":1.5,"_source":
{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}
,"highlight":{"content":["<tag1>中國</tag1>駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"]}},{"_index":"index","_type":"fulltext","_id":"3","_score":0.53699243,"_source":
{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
,"highlight":{"content":["中韓漁警衝突調查:韓警平均每天扣1艘<tag1>中國</tag1>漁船"]}}]}}

 

—————————————————————————————————————————

相關優秀文章:

1、https://www.cnblogs.com/leeSmall/p/9189078.html

2、最火搜索引擎:ElasticSearch詳解與優化設計

https://www.tuicool.com/articles/7fueUbb

3、elasticsearch系列一:elasticsearch(ES簡介、安裝&配置、集成Ikanalyzer)

https://www.cnblogs.com/leeSmall/p/9189078.html

4、淺析:ElasticSearch 原理

https://www.sohu.com/a/270448494_100212268

5、es分佈式架構原理

https://blog.csdn.net/better_xf/article/details/81188050

 

 

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