1. 程式人生 > >elasticsearch6.X 及head外掛部署(完整版)-開機自啟動指令碼

elasticsearch6.X 及head外掛部署(完整版)-開機自啟動指令碼

本文介紹了elasticsearch叢集及head外掛部署流程,包括後臺啟動指令碼、開機自啟動,面向生產環境的部署方式供大家參考。

因工作環境問題,幾乎所有內容都是手打的,自己邊部署邊記錄問題及步驟,百分百保證能成功部署,若某一步有問題的話,有可能是打錯了,歡迎留言指正

 

叢集環境

虛擬機器(centos6.5)    是否可以成為主節點    是否為資料節點
100.0.26.217    true    true
100.0.26.218        true    true
100.0.26.219    true    true
 軟體版本

jdk1.8.0_144.tar.gz

elasticsearch-6.2.4.tar.gz

node-v8.11.1-linux-x64.tar.xz

elasticsearch-head-master.zip(https://github.com/mobz/elasticsearch-head)

1、安裝JDK
tar -zxvf jdk1.8.0_144.tar.gz

配置環境變數

vi /etc/profile   

在檔案末尾新增如下配置:

export JAVA_HOME=/home/soft/jdk1.8.0_144
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile

使用java、javac確定環境變數配置正確

2、安裝ElasticSearch(單節點)
tar -zxvf elasticsearch-6.2.4.tar.gz
vi elasticsearch-6.2.4/config/elasticsearch.yml
將配置設定為如下:

cluster.name: es6.2  
node.name: node-1  
node.master: true  
node.data: true   
network.host: 0.0.0.0  
因為elasticsearch不能使用root使用者執行,建立一個es使用者

adduser es
chown -R es:es elasticsearch-6.2.4
su es
cd elasticsearch-6.2.4
./bin/elasticsearch
此時報錯資訊如下:

[2018-02-14T23:40:16,908][ERROR][o.e.b.Bootstrap          ] [node-1] node validation exception  
[4] bootstrap checks failed  
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]  
[2]: max number of threads [1024] for user [elsearch] likely too low, increase to at least [4096]  
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 
[4]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk   
[1]、[2] 解決辦法:

vi /etc/security/limits.d/90-nproc.conf
修改配置如下:

 

* soft nproc 4096
root soft nproc unlimited
es soft nofile 65536
es hard nofile 65536
[3]解決辦法:

 

vi /etc/sysctl.conf
新增如下配置:

 

vm.max_map_count = 262144
使配置生效

sysctl -p
[4]解決辦法:

Centos 6.5不支援SecComp,而ES6.2.4預設bootstrap.system_call_filter為true, 在elasticsearch.yml增加如下配置:

bootstrap.memory_lock: false 
bootstrap.system_call_filter: false
啟動ES

./bin/elasticsearch
使用http://100.0.26.117:9200檢視節點資訊,若正常訪問則表明服務啟動成功

3、搭建叢集
在elasticsearch.yml增加配置:

discovery.zen.ping.unicast.hosts: ["100.0.26.117", "100.0.26.118", "100.0.26.119"]  
discovery.zen.minimum_master_nodes: 2
最終第一個節點的配置如下:

cluster.name: es6.2  
node.name: node-1  
node.master: true  
node.data: true   
network.host: 0.0.0.0  
bootstrap.memory_lock: false 
bootstrap.system_call_filter: false
discovery.zen.ping.unicast.hosts: ["100.0.26.117", "100.0.26.118", "100.0.26.119"]  
discovery.zen.minimum_master_nodes: 2  
其他節點配置cluster.name必須一致且node.name不能一樣,其他可以根據需求做改動

按照相同的步驟啟動各個節點,控制檯顯示啟動成功之後,訪問http://100.0.26.117:9200/_cat/nodes,若配置的節點都在,則叢集部署成功,有問題則具體問題具體解決。

這是我們搭的測試環境,在生產環境肯定需要後臺啟動elasticsearch,使用如下命令

./bin/elasticsearch -d
顯然這種方式在機器重啟之後服務就沒了,因此我們需要配置機器重啟後elasticsearch服務自啟動,切換root使用者,在/etc/init.d/目錄下建立一個es_run檔案配置如下:

su root
vi /etc/init.d/es_run
#!/bin/sh
#chkconfig: 2345 80 05
#description: es
 
export JAVA_HOME=/home/soft/jdk1.8.0_144
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
 
case "$1" in
start)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    if [ "$es_pid" == "" ]; then
        echo "elasticsearch stoped, prepare to start..."
        su es<<!
        /home/soft/elasticsearch-6.2.4/bin/elasticsearch -d
!
        while true
        do
            es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
            if [ "$es_pid" == "" ]; then
                sleep 1;
                echo "elasticsearch starting..."
            else
                echo "elasticsearch started,pid is $es_pid"
                break
            fi
        done
    else
        echo "elasticsearch exist,pid is $es_pid"
    fi
    ;;
stop)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    if [ "$es_pid" == "" ]; then
        echo "elasticsearch not started"
    else
        kill -9 $es_pid
        echo "elasticsearch stoped"
    fi
    ;;
restart)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    if [ "$es_pid" == "" ]; then
        echo "elasticsearch stoped, prepare to start..."
        su es<<!
        /home/soft/elasticsearch-6.2.4/bin/elasticsearch -d
!
        while true
        do
            es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
            if [ "$es_pid" == "" ]; then
                sleep 1;
                echo "elasticsearch starting..."
            else
                echo "elasticsearch started,pid is $es_pid"
                break
            fi
        done
    else
        kill -9 $es_pid
        echo "elasticsearch stoped"
        su es<<!
        /home/soft/elasticsearch-6.2.4/bin/elasticsearch -d
!
        while true
        do
            es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
            if [ "$es_pid" == "" ]; then
                sleep 1;
                echo "elasticsearch starting..."
            else
                echo "elasticsearch started,pid is $es_pid"
                break
            fi
        done
    fi
    ;;
*)
    echo "start|stop|restart"
    ;;  
esac
exit $?
注意腳步檔案的前兩行不可缺少

給腳步賦予可執行許可權,並新增到開機啟動項中。此時服務並沒有啟動,重啟機器才會啟動。當前需手動啟動一次服務。

chmod +x /etc/init.d/es_run
chkconfig --add /etc/init.d/es_run
service es_run start
每個節點按照同樣的方式操作,完成機器重啟後elasticsearch服務自啟動

4、安裝head外掛
解壓node-v8.11.1-linux-x64.tar.xz 之前確保系統已安裝xz,若無則先安裝

yum install xz
tar xvf node-v8.11.1-linux-x64.tar.xz
配置node環境變數

 

vi /etc/profile
export JAVA_HOME=/home/soft/jdk1.8.0_144
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export NODE_PATH=/home/soft/node-v8.11.1-linux-x64
export PATH=$PATH:$JAVA_HOME/bin:$NODE_PATH/bin
source /etc/profile
可以在控制檯輸入node或npm在驗證node是否配置正確

配置node映象源

npm set registry http://ip:port
下載head外掛需要的依賴

cd /home/soft/elasticsearch-head-master
npm install
由於每個人的映象源不一致可能會導致依賴不能完完整整下下來,這時可以考慮提示下載不下來的依賴,單獨下載。像我使用的內網映象源,碰到了三個問題:

1、bluebird依賴下載失敗,

npm info bluebird
檢視映象源中所有bluebird所有版本資訊,最新版本為3.5.1,npm install 預設下載的是映象源中的最新版本

手動測試:

npm install [email protected]
發現3.5.1版本下載不下來,而換成3.5.0 就ok了,具體原因沒有去深究,有了解的朋友歡迎分享。下載好後繼續全量下載

npm install [email protected]
 

npm install 
2、core-js也碰到同樣的問題,最終下載的2.5.0。下載好後繼續全量下載

 

npm install [email protected]
npm install
3、phantomjs-prebuilt下載失敗,錯誤資訊如下:

npm ERR! [email protected] install: `node install.js`  
npm ERR! Exit status 1  
npm ERR!  
npm ERR! Failed at the [email protected] install script 'node   install.js'.  
網上找到解決辦法,原文地址:https://stackoverflow.com/questions/40992231/failed-at-the-phantomjs-prebuilt2-1-13-install-script-node-install-js

npm install [email protected] --ignore-scripts
繼續下載其他依賴:

 

npm install
直至沒有錯誤資訊,表明所有依賴已下載完成

上述3個問題前兩個應該跟我的環境有關,但第三個應該大家都會碰到

修改Gruntfile.js配置,在keepalive: true下增加hostname:'*'

vi Gruntfile.js
connect: {
          server: {
                   options: {
                        port: 9100,
                        base: '.',
                        keepalive: true,
                        hostname: '*'
                    }
          }
  }
修改儲存後啟動head 服務

npm run start
網上有些說使用grunt 啟動,這種方式你得先全域性安裝一下grunt-cli,個人覺得多此一舉,方式如下:

npm -g install grunt-cli
grunt server
瀏覽器開啟http://100.0.26.117:9100,此時發現頁面能正常開啟,但是提示叢集健康值:未連線,這個問題由兩個地方的配置導致的,網上查資料基本只說一種情況,可能他們只基於本地測試,不是面向生產環境,所有有些問題並未發現。

1、修改elasticsearch.yml,增加如下配置並重啟ES:

http.cors.enabled: true
http.cors.allow-origin: "*"
service es_run restart
2、再次開啟http://100.0.26.117:9100,顯示還是未連線,如下圖:

注意圖片上用紅框標註的,生產環境中客戶端訪問的時候, 連線localhost肯定是訪問不了的,這時把localhost改成100.0.26.117就可以了,也可以修改app.js的一個配置:

vi /home/soft/elasticsearch-head-master/_site/app.js
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://100.0.26.117:9200";
重啟head 服務就OK了。

很容易想到接下來就是後臺啟動以及開機自啟動,配置過程跟elasticsearch相似

vi /etc/init.d/es_head_run
配置如下:

 

#!/bin/sh
#chkconfig: 2345 80 05
#description: es_head_run
 
export NODE_PATH=/home/soft/node-v8.11.1-linux-x64
export PATH=$PATH:$NODE_PATH/bin
cd /home/soft/elasticsearch-head-master
nohup npm run start >/home/soft/elasticsearch-head-master/nohup.out 2>&1 &
賦許可權及新增到開機啟動項

 

chmod +x /etc/init.d/es_head_run
chkconfig -add /etc/init.d/es_head_run
service es_head_run
到這裡整個部署流程已經完成