1.启动Elasticsearch之前需要更改一下启动脚本
#!/bin/bash
# CONTROLLING STARTUP:
#
# This script relies on a few environment variables to determine startup
# behavior, those variables are:
#
# ES_PATH_CONF -- Path to config directory
# ES_JAVA_OPTS -- External Java Opts on top of the defaults set
#
# Optionally, exact memory values can be set using the `ES_JAVA_OPTS`. Note that
# the Xms and Xmx lines in the JVM options file must be commented out. Example
# values are "512m", and "10g".
#
# ES_JAVA_OPTS="-Xms8g -Xmx8g" ./bin/elasticsearch
#配置自己的jdk11
export JAVA_HOME=/opt/jdk-11.0.5
export PATH=$JAVA_HOME/bin:$PATH
source "`dirname "$0"`"/elasticsearch-env
if [ -z "$ES_TMPDIR" ]; then
ES_TMPDIR=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.TempDirectory`
fi
export ES_PATH_CONF=/etc/elasticsearch
ES_JVM_OPTIONS="$ES_PATH_CONF"/jvm.options
JVM_OPTIONS=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.JvmOptionsParser "$ES_JVM_OPTIONS"`
ES_JAVA_OPTS="${JVM_OPTIONS//\$\{ES_TMPDIR\}/$ES_TMPDIR}"
#添加jdk判断
if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA="/opt/jdk-11.0.5/bin/java"
else
JAVA=`which java`
fi
# manual parsing to find out, if process should be detached
if ! echo $* | grep -E '(^-d |-d$| -d |--daemonize$|--daemonize )' > /dev/null; then
exec \
"$JAVA" \
$ES_JAVA_OPTS \
-Des.path.home="$ES_HOME" \
-Des.path.conf="$ES_PATH_CONF" \
-Des.distribution.flavor="$ES_DISTRIBUTION_FLAVOR" \
-Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
-Des.bundled_jdk="$ES_BUNDLED_JDK" \
-cp "$ES_CLASSPATH" \
org.elasticsearch.bootstrap.Elasticsearch \
"$@"
else
exec \
"$JAVA" \
$ES_JAVA_OPTS \
-Des.path.home="$ES_HOME" \
-Des.path.conf="$ES_PATH_CONF" \
-Des.distribution.flavor="$ES_DISTRIBUTION_FLAVOR" \
-Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
-Des.bundled_jdk="$ES_BUNDLED_JDK" \
-cp "$ES_CLASSPATH" \
org.elasticsearch.bootstrap.Elasticsearch \
"$@" \
<&- &
retval=$?
pid=$!
[ $retval -eq 0 ] || exit $retval
if [ ! -z "$ES_STARTUP_SLEEP_TIME" ]; then
sleep $ES_STARTUP_SLEEP_TIME
fi
if ! ps -p $pid > /dev/null ; then
exit 1
fi
exit 0
fi
exit $?
[root@elasticsearch01 ~]# cd /usr/local/elasticsearch/bin
[root@elasticsearch01 bin]# ./elasticsearch
2.在root权限下启动会报错,这是出于系统安全考虑设置的条件,由于ElasticSearch可以接收用户输入的脚本并且执行,为了系统安全考虑,建议创建一个单独的用户用来运行ElasticSearch
[2019-11-07T07:45:50,648][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-elasticsearch-01] 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.4.0.jar:7.4.0]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.4.0.jar:7.4.0]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.4.0.jar:7.4.0]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.4.0.jar:7.4.0]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.4.0.jar:7.4.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.4.0.jar:7.4.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.4.0.jar:7.4.0]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.4.0.jar:7.4.0]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.4.0.jar:7.4.0]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.4.0.jar:7.4.0]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.4.0.jar:7.4.0]
... 6 more
3.创建用户组和用户
[root@elasticsearch01 bin]# groupadd esgroup
[root@elasticsearch01 bin]# useradd esuser -g esgroup -p espassword
4.更改elasticsearch文件夹及内部文件的所属用户及组:
[root@elasticsearch01 bin]# cd /usr/local/
[root@elasticsearch01 local]# chown -R esuser:esgroup elasticsearch
[root@elasticsearch02 local]# chown -R esuser:esgroup /etc/elasticsearch/
[root@elasticsearch02 local]# chown -R esuser:esgroup /var/log/elasticsearch/
[root@elasticsearch02 local]# chown -R esuser:esgroup /var/data/elasticsearch/
5.切换用户并运行:
[root@elasticsearch01 local]# su esuser
[esuser@elasticsearch01 local]# cd elasticsearch/bin/
[esuser@elasticsearch01 bin]$ ./elasticsearch
6.再次启动后会报错:
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [3818] for user [esuser] 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]
- 处理第一个错误:
[esuser@elasticsearch01 bin]$ su root
[root@elasticsearch01 bin]$ vi /etc/security/limits.conf
文件最后加入
esuser soft nofile 65536
esuser hard nofile 65536
esuser soft nproc 4096
esuser hard nproc 4096
- 处理第二个错误:
进入limits.d目录下修改配置文件
[root@elasticsearch01 bin]# vi /etc/security/limits.d/20-nproc.conf
文件最后加入
esuser soft nproc 4096
- 处理第三个错误:
[root@elasticsearch01 bin]# vi /etc/sysctl.conf
文件最后加入
vm.max_map_count=655360
执行以下命令生效:
[root@elasticsearch01 bin]# sysctl -p
7.再次启动Elasticsearch,就没有问题了
[root@elasticsearch01 bin]# su esuser
[esuser@elasticsearch01 bin]$ ./elasticsearch
8.停止es
优雅的关闭es,可以确保es关闭的很干净,并且优雅关闭资源。举例来说,如果node在一个合理的顺序下关闭了,首先会将自己从cluster中优雅移除,fsync translog日志到磁盘中去,然后执行其他相关的cleanup活动。
如果我们将es用service的方式来运行,那么可以通过server管理功能来停止es。
如果我们是直接启动es的,可以control-C停止es,或者是发送SEGTERM信号给es进程
jps | grep Elasticsearch
kill -SIGTERM 15516
如果es发生了fatal error,类似out of memory error,代码bug,或者io error,等等
当es发现jvm有一个fatal error,就会尝试记录在log里面,然后尝试去停止jvm。此时es是不会按照优雅关闭的模式去执行的,而是会直接关闭,并且返回一个错误码
JVM internal error 128
Out of memory error 127
Stack overflow error 126
Unknown virtual machine error 125
Serious I/O error 124
Unknown fatal error 1