
[root@es-node1 ~]# mkdir /usr/java
[root@es-node1 ~]# tar zxvf jdk1.8.0_131.tar.gz -C /usr/java/
[root@es-node1 ~]# cp /etc/profile /etc/profile.back
[root@es-node1 ~]# vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_131/
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
[root@es-node1 ~]# source /etc/profile
[root@es-node1 ~]# java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
[Elasticsearch]
[root@es-node1 ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.tar.gz
[root@es-node1 ~]# tar zxvf elasticsearch-6.3.2.tar.gz -C /usr/src/
[root@es-node1 ~]# mv /usr/src/elasticsearch-6.3.2/ /usr/local/elasticsearch
[root@es-node1 ~]# useradd elasticsearch
[root@es-node1 ~]# chown -R elasticsearch:elasticsearch /usr/local/elasticsearch/
【Elasticsearch優化】
為了使得Elasticsearch獲得高效穩定的性能,需要對系統和JVM兩個方面進行優化
[root@es-node1 ~]# vim /etc/sysctl.conf
fs.file-max=655360 #最大打開文件描述符數,建議修改為655360或者更高
vm.max_map_count=262144 #直接影響java線程數量,用來限制一個進程可以用於VM(虛擬內存區域大小)默認為65530,建議修改為262144或者更高
[root@es-node1 ~]#vim /etc/security/limits.conf
* soft nproc 20480 * hard nproc 20480 * soft nofile 65536 * hard nofile 65536 * soft memlock unlimited * hard memlock unlimited
[root@es-node1 ~]# sed -i '5s#4096#20480#g' /etc/security/limits.d/20-nproc.conf
#JVM調優
JVM調優主要是針對Elasticsearch的JVM內存資源進行優化,elasticsearch的內存資源配置文件為jvm.options
[root@es-node1 ~]# vim /usr/local/elasticsearch/config/jvm.options #根據服務器內存大小,進行修改合適的值,建議修改服務器物理內存的一半最佳
-Xms1g
-Xmx1g
[root@es-node1 ~]# egrep -v "#|^$" /usr/local/elasticsearch/config/elasticsearch.yml
cluster.name: es_data #配置集群名稱
node.name: es_node1 #節點名稱
node.master: true #指定了該節點是否有資格選舉master,默認為true
node.data: true #指定該節點是否存儲索引數據,默認為true,表示為數據存儲
path.data: /usr/local/elasticsearch/data #設置索引數據存儲的位置
path.logs: /usr/local/elasticsearch/logs #設置日志文件存儲的位置
bootstrap.memory_lock: true #設置true用來鎖住物理內存
indices.fielddata.cache.size: 50mb #索引字段緩存大小
network.host: 0.0.0.0 #監聽地址
http.port: 9200 #Elasticsearch對外提供的http端口
discovery.zen.ping.unicast.hosts: ["192.168.37.134:9300","192.168.37.135:9300","192.168.37.136:9300"] #設置集群中master節點初始列表,可通過這些
節點自動發現新加入集群的節點,這里的9330端口,即為集群交互通信端口
discovery.zen.minimum_master_nodes: 1 #配置當前集群最少的master節點數,默認為1,也就是說,elasticsearch集群中master節點數不能低於次值>,
http.cors.enabled: true #表示開啟跨域訪問支持,默認為false
http.cors.allow-origin: "*" #表示跨域訪問允許的域名地址,,可支持正則表達式,這里“*”表示允許所有域名訪問
http.cors.allow-headers: "X-Requested-With,Content-Type, Content-Length, Authorization" #允許跨域訪問頭部信息
[root@es-node1 config]# vim /usr/local/elasticsearch/config/jvm.options
-Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError
[root@es-node1 ~]#mkdir /usr/local/elasticsearch/data/ -p
[root@es-node1 ~]#chown -R elasticsearch:elasticsearch /usr/local/elasticsearch/
[root@es-node1 ~]# su - elasticsearch
[elasticsearch@es-node1 ~]$ /usr/local/elasticsearch/bin/elasticsearch -d

ps:當你的elasticsearch啟動的時候,什么錯誤都沒有報,但是就是端口進程沒起來·,此時查看日志信息,輸出如下,
這是因為目錄權限屬主屬組是root賬號,我們只需chown elasticsearch:elasticsearch -R /usr/local/elasticsearch/即可
tail /usr/local/elasticsearch/logs/es_cluster.log
at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781) ~[?:1.8.0_131] at java.nio.file.Files.createDirectories(Files.java:767) ~[?:1.8.0_131] at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:203) ~[elasticsearch-6.3.2.jar:6.3.2] at org.elasticsearch.node.Node.<init>(Node.java:270) ~[elasticsearch-6.3.2.jar:6.3.2] at org.elasticsearch.node.Node.<init>(Node.java:252) ~[elasticsearch-6.3.2.jar:6.3.2] at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:213) ~[elasticsearch-6.3.2.jar:6.3.2] at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:213) ~[elasticsearch-6.3.2.jar:6.3.2] at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:326) ~[elasticsearch-6.3.2.jar:6.3.2] at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.3.2.jar:6.3.2] ... 6 more
【Elasticsearch圖形化工具】
root@es-node1 ~]# yum install -y nodejs npm git (ps:如果yum不能正常安裝npm和nodejs的話,采用源碼吧)
源碼安裝
wget https://nodejs.org/dist/v9.8.0/node-v9.8.0-linux-x64.tar.xz xz -d node-v9.8.0-linux-x64.tar.xz tar xvf node-v9.8.0-linux-x64.tar vim /etc/profile export NODE_HOME=/root/node-v9.8.0-linux-x64/ export PATH=$PATH:$NODE_HOME/bin [root@es-node1 ~]# npm -v 5.6.0 [root@es-node1 ~]# node -v v9.8.0
[root@es-node1 ~]# cd /usr/local/
[root@es-node1 local]# git clone git://github.com/mobz/elasticsearch-head.git #安裝head插件
[root@es-node1 local]# npm config set registry http://registry.npm.taobao.rog #采用淘寶源
[root@es-node1 elasticsearch]# npm install #安裝過程中,有點小意外,糾結了好久才找到辦法,確實是一個坑
npm ERR! npm ERR! If you are behind a proxy, please make sure that the npm ERR! 'proxy' config is set properly. See: 'npm help config' npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2018-08-22T06_36_25_382Z-debug.log
或者
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to http://registry.npm.taobao.rog/phantomjs-prebuilt failed, reason: getaddrinfo ENOTFOUND registry.npm.taobao.rog registry.npm.taobao.rog:80
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-08-25T09_04_38_243Z-debug.log
[root@localhost elasticsearch-head]# npm install
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to http://registry.npm.taobao.rog/grunt-contrib-jasmine failed, reason: getaddrinfo ENOTFOUND registry.npm.taobao.rog registry.npm.taobao.rog:80
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-08-25T09_04_44_521Z-debug.log
解決方法:
npm config get proxy
npm config get https-proxy
npm config set registry http://registry.cnpmjs.org/
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! phantomjs-prebuilt@2.1.16 install: `node install.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the phantomjs-prebuilt@2.1.16 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2018-08-12T07_42_48_334Z-debug.log
解決辦法:
# npm install phantomjs-prebuilt@2.1.14 --ignore-scripts
npm install即可
[root@es-node1 elasticsearch]# vim /usr/local/elasticsearch-head/_site/app.js

[root@es-node1 elasticsearch-head]# nohup npm run start & #后台啟動head插件服務,默認端口為9100

web瀏覽器http://192.168.37.134:9100/驗證
【其他節點配置】
【192.168.37.135】es-node2
node2節點值作為數據存儲的節點角色,不參與主節點選舉
#vim /usr/local/elasticsearch/config/elasticsearch.yml
cluster.name: es_data node.name: es.node2 node.master: false node.data: true path.data: /usr/local/elasticsearch/data/ path.logs: /usr/local/elasticsearch/logs bootstrap.memory_lock: true indices.fielddata.cache.size: 50mb network.host: 0.0.0.0 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.37.134:9300","192.168.37.135:9300","192.168.37.136:9300"] discovery.zen.minimum_master_nodes: 1 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: "X-Requested-With,Content-Type, Content-Length, Authorization"
vim /usr/local/elasticsearch/config/jvm.options
-Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError
【192.168.37.136】es-node3節點配置(ps:這里的node3與node1配置都是一樣的,都有被選舉成為master主節點的可能)
cluster.name: es_data node.name: es.node3 node.master: true node.data: true path.data: /usr/local/elasticsearch/data/ path.logs: /usr/local/elasticsearch/logs bootstrap.memory_lock: true indices.fielddata.cache.size: 50mb network.host: 0.0.0.0 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.37.134:9300","192.168.37.135:9300","192.168.37.136:9300"] discovery.zen.minimum_master_nodes: 1 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: "X-Requested-With,Content-Type, Content-Length, Authorization"
vim /usr/local/elasticsearch/config/jvm.options -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError
【總結】
Elasticsearch節點角色類型
在生產中,高並發的場景容易出現腦裂問題,而在Elasticsearch集群亦是如此,Elasticsearch集群中的每個節點都可以成為主節點,存儲數據,提供查詢服務
而這些事由兩個屬性參數控制的也就是node,master和node.data
1:這兩種組合表示這個節點有資格成為主節點,又要存儲數據,如果該節點真的成為主節點的話,那么自己還要存儲數據,壓力是相當大的,生產不建議這樣配置
node.master: true
node.data:true
2:這種組合是數據存儲節點,既不參與選舉,也不會成為成為主節點,因為沒有成為主節點的資格,只能是數據存儲節點
node.master:false
node.data:true
3:擁有參與選舉主節點的資格,不會存儲節點數據,該節點我們稱之為master主節點
node.master:true
node.data: false
4:該節點為客戶端節點,數據存儲和主節點資格兩項都沒有,只能處理客戶端請求,對海量的請求進行均衡
node.master:false
node.data:false
在一個生產集群中我們可以對這些節點的職責進行划分。
建議集群中設置3台以上的節點作為master節點【node.master: true node.data: false】
這些節點只負責成為主節點,維護整個集群的狀態。
再根據數據量設置一批data節點【node.master: false node.data: true】
這些節點只負責存儲數據,后期提供建立索引和查詢索引的服務,這樣的話如果用戶請求比較頻繁,這些節點的壓力也會比較大
所以在集群中建議再設置一批client節點【node.master: false node.data: true】
這些節點只負責處理用戶請求,實現請求轉發,負載均衡等功能。
