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 bluebird@3.5.1

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

npm install bluebird@3.5.0

 

npm install

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

 

npm install core-js@2.5.0

npm install

3、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,此時發現頁面能正常打開,但是提示集群健康值:未連接,這個問題由兩個地方的配置導致的,網上查資料基本只說一種情況,可能他們只基於本地測試,不是面向生產環境,所有有些問題並未發現。

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

到這里整個部署流程已經完成
---------------------
作者:zou79189747
來源:CSDN
原文:https://blog.csdn.net/zou79189747/article/details/80111219
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM