elasticsearch6.X 及head插件部署(完整版)-開機自啓動腳本

本文介紹了elasticsearch集羣及head插件部署流程,包括後臺啓動腳本、開機自啓動,面向生產環境的部署方式供你們參考。java

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

 

集羣環境linux

虛擬機(centos6.5)    是否能夠成爲主節點    是否爲數據節點
100.0.26.217    true    true
100.0.26.218        true    true
100.0.26.219    true    true
 軟件版本git

jdk1.8.0_144.tar.gzes6

elasticsearch-6.2.4.tar.gzgithub

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

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

一、安裝JDK
tar -zxvf jdk1.8.0_144.tar.gzcentos

配置環境變量瀏覽器

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肯定環境變量配置正確

二、安裝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查看節點信息,若正常訪問則代表服務啓動成功

三、搭建集羣
在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服務自啓動

四、安裝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
因爲每一個人的鏡像源不一致可能會致使依賴不能完完整整下下來,這時能夠考慮提示下載不下來的依賴,單獨下載。像我使用的內網鏡像源,碰到了三個問題:

一、bluebird依賴下載失敗,

npm info bluebird
查看鏡像源中全部bluebird全部版本信息,最新版本爲3.5.1,npm install 默認下載的是鏡像源中的最新版本

手動測試:

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

npm install bluebird@3.5.0
 

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

 

npm install core-js@2.5.0
npm install
三、phantomjs-prebuilt下載失敗,錯誤信息以下:

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

npm install phantomjs-prebuilt@2.1.14 --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,此時發現頁面能正常打開,可是提示集羣健康值:未鏈接,這個問題由兩個地方的配置致使的,網上查資料基本只說一種狀況,可能他們只基於本地測試,不是面向生產環境,全部有些問題並未發現。

一、修改elasticsearch.yml,增長以下配置並重啓ES:

http.cors.enabled: true
http.cors.allow-origin: "*"
service es_run restart
二、再次打開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 到這裏整個部署流程已經完成

相關文章
相關標籤/搜索