欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

elasticsearch6.X 及head插件部署(完整版)

发布时间:2025/3/21 编程问答 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 elasticsearch6.X 及head插件部署(完整版) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

elasticsearch6.X 及head插件部署(完整版)

2018年04月28日 17:22:16 zou79189747 阅读数:3178

 版权声明: https://blog.csdn.net/zou79189747/article/details/80111219

本文介绍了elasticsearch集群及head插件部署流程,包括后台启动脚本、开机自启动,面向生产环境的部署方式供大家参考。

因工作环境问题,几乎所有内容都是手打的,自己边部署边记录问题及步骤,百分百保证能成功部署,若某一步有问题的话,有可能是打错了,欢迎留言指正

 

集群环境

虚拟机(centos6.5)是否可以成为主节点是否为数据节点
100.0.26.217truetrue
100.0.26.218    truetrue
100.0.26.219truetrue

 软件版本

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

  • 到这里整个部署流程已经完成

    总结

    以上是生活随笔为你收集整理的elasticsearch6.X 及head插件部署(完整版)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。