elasticsearch-6.0.1安裝
0. 介紹:

ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎;是目前全文搜索引擎的首選。
Elastic 的底層是開源庫 Lucene。但是,沒法直接用 Lucene,必須自己寫代碼去調用它的接口。Elastic 是 Lucene 的封裝,提供了 REST API 的操作接口,開箱即用。
Elasticsearch是用Java開發的,並作為Apache許可條款下的開放源碼發布,是當前流行的企業級搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。
1. 環境准備:
系統:CentOS Linux release 7.4.1708 (Core)
Java環境:JDK1.8(若未安裝,需先安裝)
集群環境:
172.16.64.137 (默認master node)
172.16.64.138
172.16.64.147
2.下載elasticsearch-6.0.1:
官網:https://www.elastic.co/downloads/elasticsearch
下載鏈接:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.1.tar.gz
解壓、移動
1
2
|
tar -zxvf elasticsearch-6.0.1.tar.gz
mv elasticsearch-6.0.1.tar.gz /usr/local/elasticsearch
|
3. 配置主配置文件:
vim /usr/local/elasticsearch/config/elasticsearch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
cluster.name: cluster-es
node.name: es-node1
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
network.host: 172.16.64.137
http.port: 9200
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: ["172.16.64.137", "172.16.64.138", "172.16.64.147"]
node.master: true
node.data: false
discovery.zen.fd.ping_timeout: 180s
discovery.zen.fd.ping_retries: 10
discovery.zen.fd.ping_interval: 30s
|
配置文件詳解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
cluster.name: cluster-es
# 集群名稱
node.name: es-node1
# 節點名稱,其余兩台為es-node2、es-node3
path.data: /usr/local/elasticsearch/data
# 數據目錄
path.logs: /usr/local/elasticsearch/logs
# 日志目錄
network.host: 172.16.64.137
# 本機IP
http.port: 9200
# 本機http端口
discovery.zen.minimum_master_nodes: 1
# 指定集群中的節點中有幾個有master資格的節點
discovery.zen.ping.unicast.hosts: ["172.16.64.137", "172.16.64.138", "172.16.64.147"]
# 指定集群中其他節點的IP
node.master: true
# 是否為master
node.data: false
# 是否為數據節點
discovery.zen.fd.ping_timeout: 180s
# 設置集群中自動發現其它節點時ping連接超時時間
discovery.zen.fd.ping_retries: 10
# 集群中節點之間ping的次數
discovery.zen.fd.ping_interval: 30s
# 集群中節點之間ping的時間間隔
|
4. 配置足夠內存
1
2
3
|
vim /usr/local/elasticsearch/config/jvm.options
-Xms2g
-Xmx2g
|
5. 啟動
ES有執行腳本的能力,因安全因素,
不能在root用戶下運行,強行運行會報如下錯誤:
org.elasticsearch.bootstrap.StartupException:
java.lang.RuntimeException: can not run elasticsearch as root
1
2
3
4
5
6
7
8
9
|
# 創建用戶
useradd ela
# 賦予ela用戶所有者權限
chown -R ela:ela /usr/local/elasticsearch
su - ela
[ela@test1 ~]$/usr/local/elasticsearch/bin/elasticsearch -d # -d參數是后台運行
# 建議按以下命令啟動
[ela@test1 ~]$ nohup /usr/local/elasticsearch/bin/elasticsearch &
|
正常情況下,啟動后,網頁訪問172.16.16.206:9200會有以下內容顯示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
{
"name" : "dcV-DRJ",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "N6qGE15TQqq9-RQedQqqEw",
"version" : {
"number" : "6.1.1",
"build_hash" : "bd92e7f",
"build_date" : "2017-12-17T20:23:25.338Z",
"build_snapshot" : false,
"lucene_version" : "7.1.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
|
啟動錯誤收集:
錯誤一:max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
解決:
1
2
3
4
5
6
7
|
vi /etc/security/limits.conf
#添加如下內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
|
錯誤二:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解決:最大虛擬內存太小
1
2
3
4
5
|
vim /etc/sysctl.conf 添加一行
vm.max_map_count=655360
# 執行命令:
sysctl -p
|
7. head插件安裝
安裝head插件前,需要先安裝Node.js,需要手動安裝,yum安裝的版本太低
7.1安裝Node.js
官網:https://nodejs.org/en/download/
下載鏈接:wget https://nodejs.org/dist/v8.9.3/node-v8.9.3.tar.gz
1
2
3
4
5
|
tar node-v8.9.3.tar.gz
cd node-v8.9.3
./configure --prefix=/usr/local/node/
make # make時間較長
make install
|
添加系統變量:
1
2
3
4
5
6
|
vim /etc/profile
export NODEJS_HOME=/usr/local/node/
export PATH=$PATH:$NODEJS_HOME/bin
# 使變量生效
source /etc/profile
|
驗證:
1
2
|
[root@test1 bin]# node -v
v8.9.3
|
在安裝node的同時,會將npm模塊一起安裝
7.2 安裝head插件
下載
1
2
|
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
|
安裝(方法1)
1
|
npm install
|
安裝(方法2)
使用cnpm安裝,因為在npm安裝時,因為有些依賴的問題,速度慢且容易出錯中斷。
1
2
3
4
5
|
#安裝國內鏡像
npm install -g cnpm --registry=https://registry.npm.taobao.org
# 安裝插件(在elasticsearch-head目錄下)
cnpm install
|
安裝成功后,修改配置Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
|
vi Gruntfile.js
connect: {
server: {
options: {
hostname: "0.0.0.0", #新增的一行
port: 9100,
base: '.',
keepalive: true
}
}
}
|
修改_site/app.js配置
1
2
3
4
|
# 搜索
http://localhost:9200
# 修改為本機IP
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.1.138:9200";
|
elasticSearch整合elasticsearch-head插件:
1
2
3
4
5
6
7
|
# 在配置文件的最后加上運行head插件跨域訪問rest接口
vim /usr/local/elasticsearch/config/elasticsearch.yml
# 添加如下內容:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-credentials: true
|
重啟elasticsearch
重啟elasticsearch需要kill掉進程,然后再啟動
運行elasticsearch-head
1
|
npm run start &
|
方法3(離線安裝)
在離線情況下,需要在有網絡的環境里安裝好,然后將整個elasticsearch-head目錄壓縮拷貝過來。
重要:head插件目錄不能放在es的目錄里,需要單獨放(es從版本5以上不支持直接安裝head)
首先,安裝grunt,將整個elasticsearch-head目錄包括目錄下的node_models內容一起拷貝過來
然后,修改方法2中的兩個配置文件
Gruntfile.js 和
_site/app.js
最后,使用../elasticsearch-head/node_models/grunt/bin/grunt server & 來啟動
正常運行elasticsearch-head會有以下結果輸出:
1
2
3
4
5
6
7
8
9
10
11
|
[root@test1 elasticsearch-head]# npm run start
> elasticsearch-head@0.0.0 start /usr/local/elasticsearch-head
> grunt server
>> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
(node:16304) ExperimentalWarning: The http2 module is an experimental API.
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
|
按照屏幕提示通過瀏覽器訪問:http://172.16.64.137:9100/

最后:如果在服務器上安裝Elasticsearch,而想在本地機器上進行開發,這時候就需要在關閉終端的時候,讓Elasticsearch繼續保持運行。
最簡單的方法就是使用nohup。先按Ctrl + C,停止當前運行的Elasticsearch,改用下面的命令運行Elasticsearch
1
|
nohup ./bin/elasticsearch &
|
附:es啟動腳本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#!/bin/sh
#chkconfig: 2345 80 05
#description: es
export JAVA_HOME=/usr/local/jdk1.8.0_151
export JAVA_BIN=/usr/local/jdk1.8.0_151/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
case $1 in
start)
su ela<<!
cd /usr/local/elasticsearch
./bin/elasticsearch -d
exit
!
echo "es startup"
;;
stop)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "es stopup"
;;
restart)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "es stopup"
su ela<<!
cd /usr/local/elasticsearch
./bin/elasticsearch -d
!
echo "es startup"
;;
*)
echo "start|stop|restart"
;;
esac
|
根據實際情況,修改jdk目錄,ela安裝目錄
寫進啟動文件/etc/init.d/ela,給予x權限,添加到啟動菜單:
vim /etc/init.d/ela
chmod 755 /etc/init.d/ela
chkconfig –add ela
chkconfig ela on