centos7安装 ES集群 elasticsearch

avatar
作者
猴君
阅读量:0

这里写自定义目录标题

编写启动脚本 elasticsearch.sh

  • 懒人编写一个用于启动、停止和重启Elasticsearch服务的shell脚本
  • 脚本中设置环境变量,并使用su - es - c命令在es用户下执行操作
  • 通过赋予脚本执行权限,用户可以方便地对Elasticsearch进行控制
#!/bin/sh   export JAVA_HOME=/opt/software/java11/jdk-11.0.11 export PATH=$JAVA_HOME/bin:$PATH export ES_HOME=/opt/software/elasticsearch/elasticsearch-7.6.1 export PATH=$ES_HOME/bin:$PATH  if [ $# -lt 1 ] then 	echo "No Args Input..." 	exit ; fi     case $1 in     start)         su - es -c "$ES_HOME/bin/elasticsearch -d -p pid"         echo "elasticsearch is started"         ;;     stop)         pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`         kill -9 $pid         echo "elasticsearch is stopped"         ;;     restart)         pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`         kill -9 $pid         echo "elasticsearch is stopped"         sleep 1         su - es -c "$ES_HOME/bin/elasticsearch -d -p pid"         echo "elasticsearch is started"         ;;     *)         echo "start|stop|restart"         ;; esac exit 0 

赋予执行权限:

chmod +x elasticsearch.sh 

参数说明:

su - es -c “命令”, 这个表示脚本内切换es用户一次并执行 -c 后面的命令,然后退出es普通用户到当前用户。

启动可能报错:elasticsearch 7.10启动报错 bootstrap checks failed

elasticsearch 7.14启动报错 bootstrap checks failed
现象一
ES使用root用户安装报错如下

Caused by: java.lang.RuntimeException: can not run elasticsearch as root  [root@localhost elasticsearch-7.14.2]# ./bin/elasticsearch [2022-09-18T16:40:41,166][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [localhost.localdomain] uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root         at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.14.2.jar:7.14.2]         at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.14.2.jar:7.14.2]         at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75) ~[elasticsearch-7.14.2.jar:7.14.2]         at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116) ~[elasticsearch-cli-7.14.2.jar:7.14.2]         at org.elasticsearch.cli.Command.main(Command.java:79) ~[elasticsearch-cli-7.14.2.jar:7.14.2]         at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.14.2.jar:7.14.2]         at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81) ~[elasticsearch-7.14.2.jar:7.14.2] Caused by: java.lang.RuntimeException: can not run elasticsearch as root         at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:103) ~[elasticsearch-7.14.2.jar:7.14.2]         at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:170) ~[elasticsearch-7.14.2.jar:7.14.2]         at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:399) ~[elasticsearch-7.14.2.jar:7.14.2]         at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.14.2.jar:7.14.2]         ... 6 more uncaught exception in thread [main] java.lang.RuntimeException: can not run elasticsearch as root         at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:103)         at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:170)         at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:399)         at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159)         at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150)         at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75)         at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116)         at org.elasticsearch.cli.Command.main(Command.java:79)         at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115)         at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81) For complete error details, refer to the log at /opt/ES/elasticsearch-7.14.2/logs/elasticsearch.log 
解决方法

ES官方为了安全起见,禁止启用Root启动ES服务,因此我们需要进行普通用户的创建,才能正常启动ES服务。所以我们需要新建一个普通用户,并将ES安装目录授权给刚新建的用户。

[root@localhost ES]# useradd es

错误信息:

ERROR: [1] bootstrap checks failed [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535] [2]: max number of threads [3795] for user [es] is 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] 
问题原因:

es高版本对资源要求较高,linux系统默认配置不能满足它的要求,所以需要单独配置

解决方法:

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535] 

修改vi /etc/security/limits.conf 文件,加入配置

* hard nofile 65535  # *可以是es启动用户 * soft nofile 65535 

可以指定用户
在这里插入图片描述

[2]: max number of threads [3795] for user [es] is too low, increase to at least [4096] 

修改vi /etc/security/limits.conf 文件,加入配置

es - nproc 4096   # es是我的启动用户 
注意 退出xshell,重新登录: 上面两个配置项改完后,ES启动用户(es 或root) 重新登录就会生效;
[3]:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 

修改/etc/sysctl.config文件,加入下面配置

vi /etc/sysctl.conf

vm.max_map_count=262144 

执行命令,立即生效

/sbin/sysctl -p vm.max_map_count = 262144 

启动成功

    广告一刻

    为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!