Elastic 需要 Java 8 環(huán)境。如果你的機(jī)器還沒安裝 Java,可以參考JDK安裝
單機(jī)節(jié)點(diǎn)安裝
- 下載安裝包.zip
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
$ unzip elasticsearch-5.5.1.zip
$ cd elasticsearch-5.5.1/
- 進(jìn)入解壓后的目錄,運(yùn)行下面的命令,啟動(dòng) Elastic。
$ ./bin/elasticsearch
如果報(bào)錯(cuò)"max virtual memory areas vm.maxmapcount [65530] is too low",要運(yùn)行下面的命令。
$ sudo sysctl -w vm.max_map_count=262144
- Elastic就會(huì)在默認(rèn)的9200端口運(yùn)行。
請(qǐng)求9200端口,Elastic 返回一個(gè) JSON 對(duì)象,包含當(dāng)前節(jié)點(diǎn)、集群、版本等信息
{
"name" : "MuuucZm",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Fejq3vkgQN-tUGHt_8Mxvg",
"version" : {
"number" : "5.5.1",
"build_hash" : "19c13d0",
"build_date" : "2017-07-18T20:44:24.823Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
創(chuàng)建數(shù)據(jù)存放路徑(建議設(shè)置配置為在Elasticsearch主目錄之外定位數(shù)據(jù)目錄,以便在不刪除數(shù)據(jù)的情況下刪除主目錄!)
./config/elasticsearch.yml配置path.data創(chuàng)建日志存放路徑
./config/elasticsearch.yml配置path.logs
集群安裝
采用三臺(tái)服務(wù)器部署Elasticsearch集群,部署ES集群就不得不提索引分片,以下是索引分片的簡(jiǎn)單介紹。
ES集群中索引可能由多個(gè)分片構(gòu)成,并且每個(gè)分片可以擁有多個(gè)副本。通過將一個(gè)單獨(dú)的索引分為多個(gè)分片,我們可以處理不能在一個(gè)單一的服務(wù)器上面運(yùn)行的大型索引,簡(jiǎn)單的說就是索引的大小過大,導(dǎo)致效率問題。不能運(yùn)行的原因可能是內(nèi)存也可能是存儲(chǔ)。由于每個(gè)分片可以有多個(gè)副本,通過將副本分配到多個(gè)服務(wù)器,可以提高查詢的負(fù)載能力。
| Node |
|---|
| es01.com |
| es02.com |
| es03.com |
- 幾個(gè)是最主要的配置文件路徑
../elasticsearch-5.5.1/config/elasticsearch.yml # els的配置文件
../elasticsearch-5.5.1/config/jvm.options # JVM相關(guān)的配置,內(nèi)存大小等等
../elasticsearch-5.5.1/config/log4j2.properties # 日志系統(tǒng)定義
/var/lib/elasticsearch # 數(shù)據(jù)的默認(rèn)存放位置
修改elasticsearch配置文件
./config/elasticsearch.yml
#集群的名稱
#日志文件會(huì)以集群名稱命名
cluster.name: esCluster
#節(jié)點(diǎn)名稱,其余兩個(gè)節(jié)點(diǎn)分別為node-2 和node-3
node.name: node-1
#指定該節(jié)點(diǎn)是否有資格被選舉成為master節(jié)點(diǎn),默認(rèn)是true,es是默認(rèn)集群中的第一臺(tái)機(jī)器為master,如果這臺(tái)機(jī)掛了就會(huì)重新選舉master
node.master: true
#允許該節(jié)點(diǎn)存儲(chǔ)數(shù)據(jù)(默認(rèn)開啟)
node.data: true
#索引數(shù)據(jù)的存儲(chǔ)路徑
path.data: /usr/local/elasticsearch/data
#日志文件的存儲(chǔ)路徑
path.logs: /usr/local/elasticsearch/logs
#設(shè)置為true來鎖住內(nèi)存。因?yàn)閮?nèi)存交換到磁盤對(duì)服務(wù)器性能來說是致命的,當(dāng)jvm開始swapping時(shí)es的效率會(huì)降低,所以要保證它不swap
bootstrap.memory_lock: true
#綁定的ip地址
network.host: 0.0.0.0
#設(shè)置對(duì)外服務(wù)的http端口,默認(rèn)為9200
http.port: 9200
# 設(shè)置節(jié)點(diǎn)間交互的tcp端口,默認(rèn)是9300
transport.tcp.port: 9300
#Elasticsearch將綁定到可用的環(huán)回地址,并將掃描端口9300到9305以嘗試連接到運(yùn)行在同一臺(tái)服務(wù)器上的其他節(jié)點(diǎn)。
#這提供了自動(dòng)集群體驗(yàn),而無需進(jìn)行任何配置。數(shù)組設(shè)置或逗號(hào)分隔的設(shè)置。每個(gè)值的形式應(yīng)該是host:port或host
discovery.zen.ping.unicast.hosts: ["192.168.8.101:9300", "192.168.8.103:9300", "192.168.8.104:9300"]
#這個(gè)參數(shù)控制的是,一個(gè)節(jié)點(diǎn)需要看到的具有master節(jié)點(diǎn)資格的最小數(shù)量,然后才能在集群中做操作。官方的推薦值是(N/2)+1,其中N是具有master資格的節(jié)點(diǎn)的數(shù)量
discovery.zen.minimum_master_nodes: 2
調(diào)整jvm內(nèi)存
./config/jvm.options
#建議對(duì)jvm進(jìn)行一些修改,不然很容易出現(xiàn)OOM,參考官網(wǎng)改參數(shù)配置最好不要超過內(nèi)存的50%
-Xms2g
-Xmx2g
- 分別啟動(dòng)三臺(tái)Elasticsearch
$ bin/elasticsearch -d # -d 后臺(tái)運(yùn)行
- 測(cè)試啟動(dòng)
ps -ef | grep elasticsearc查看進(jìn)程是否啟動(dòng)
curl -XGET 'http://es01:9200/_cat/nodes?pretty'
ip01 18 68 0 0.07 0.06 0.05 mdi - els2
ip02 25 67 0 0.01 0.02 0.05 mdi * els1 # *號(hào)表示為當(dāng)前節(jié)點(diǎn)為主節(jié)點(diǎn)的意思
ip03 7 95 0 0.02 0.04 0.05 mdi - els3
集群安裝踩坑
- 啟動(dòng)失敗
查看日志,日志文件會(huì)以集群名稱命名
[ERROR][o.e.b.Bootstrap ] [node-1] node validation exception
[3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: memory locking requested for elasticsearch process but memory is not locked
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解決方法:(root用戶)
$ vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 2048
* hard nproc 4096
#選擇鎖住swapping因此需要在這個(gè)配置文件下再增加兩行代碼
es soft memlock unlimited
es hard memlock unlimited
$ vim /etc/sysctl.conf
vm.max_map_count=655360
fs.file-max=655360
# 使系統(tǒng)配置生效(使用root用戶)
$ sysctl -p
參數(shù)參考 https://my.oschina.net/987openlab/blog/94634
- 節(jié)點(diǎn)啟動(dòng)成功,無法形成集群
[2018-02-15T21:15:06,352][INFO ][rest.suppressed ] /_cat/health Params: {h=node.total}
MasterNotDiscoveredException[waited for [30s]]
at org.elasticsearch.action.support.master.TransportMasterNodeAction$4.onTimeout(TransportMasterNodeAction.java:160)
at org.elasticsearch.cluster.ClusterStateObserver$ObserverClusterStateListener.onTimeout(ClusterStateObserver.java:239)
at org.elasticsearch.cluster.service.InternalClusterService$NotifyTimeout.run(InternalClusterService.java:630)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
原因:將discovery.zen.minimum_master_nodes的值設(shè)置為了3,總共3個(gè)節(jié)點(diǎn),都充當(dāng)主節(jié)點(diǎn)是不行的,將discovery.zen.minimum_master_nodes將這個(gè)配置改為2。
- 只有主節(jié)點(diǎn)啟動(dòng)成功,其他節(jié)點(diǎn)沒有連接集群
[2018-02-15T21:53:58,084][INFO ][o.e.d.z.ZenDiscovery ] [node-3] failed to send join request to master [{node-1}{SVrW6URqRsi3SShc1PBJkQ}{y2eFQNQ_TRenpAPyv-EnVg}{192.168.8.101}{192.168.8.101:9300}], reason [RemoteTransportException[[node-1][192.168.8.101:9300][internal:discovery/zen/join]]; nested: IllegalArgumentException[can't add node {node-3}{SVrW6URqRsi3SShc1PBJkQ}{uqoktM6XTgOnhh5r27L5Xg}{192.168.8.104}{192.168.8.104:9300}, found existing node {node-1}{SVrW6URqRsi3SShc1PBJkQ}{y2eFQNQ_TRenpAPyv-EnVg}{192.168.8.101}{192.168.8.101:9300} with the same id but is a different node instance]; ]
之前啟動(dòng)的時(shí)候報(bào)錯(cuò),沒有啟動(dòng)成功,但是data文件中生成了其他節(jié)點(diǎn)的數(shù)據(jù)。將三個(gè)節(jié)點(diǎn)的data目錄清空即可。