Elasticsearch7.x指定JDK及安装时报错

Elasticsearch7.x指定JDK及安装时报错

  • 环境说明
    • 前篇提要
      • ES安装步骤
        • 报错
          • 注意
          • 浏览器访问
    • 指定JDK
      • 安装OpenJDK11
          • ·········· 下载安装包
          • 解压到指定目录
          • 到此结束
          • 修改配置文件
          • 启动ES
          • 完整配置如下

环境说明

Centos7、Elasticsearch7.2、JAVA_HOME="/usr/local/src/jdk-11.0.1"(指定自己想指定的jdk位置)

前篇提要

ES安装步骤

下载Elasticsearch7.2
1、登录Elasticsearch官网下载7.2版本
*点击可以下载各个版本的es
https://www.elastic.co/cn/
之后利用工具上传到(虚拟机/服务器上)
或者直接复制我的代码

[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
[root@localhost src]# ll
total 328760
-rw-r--r--. 1 root root 336647987 Aug  2 11:39 elasticsearch-7.2.0-linux-x86_64.tar.gz

解压

[work@localhost ~]$ tar -zxvf elasticsearch-7.2.0-linux-x86_64.tar.gz 

[work@localhost ~]$ cd ../
[work@localhost ~]$ ll
总用量 537304
drwxrwxrwx.  3 root root        19 10月 25 11:11 data
drwxrwxrwx. 10 work work       166 10月 25 10:59 elasticsearch-7.2.0


新建用户
es不能用root用户启动,需要自己新建用户,并赋权
(我输的密码比较简单,所以有BAD PASSWORD:这个提示,不用管它)
·························································································
1.在本地虚拟机安装时可以选择新增用户赋予权限
2.也可以以Root 用户去新建用户

## 新建用户,设置密码
[root@localhost src]# adduser work
[root@localhost src]# passwd work
Changing password for user work.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
## 授权
[root@localhost src]# chown -R work elasticsearch/

新建文件夹
等会要配置下elasticsearch.yml,这里先建立两个文件夹,数据文件夹和日志文件夹,并授权给es用户,会在es的配置文件中使用到。

[root@localhost src]# mkdir /home/work/data
[root@localhost src]# mkdir /home/work/log
[root@localhost config]# chown -R esuser /home/work/data
[root@localhost config]# chown -R esuser /home/work/log

编辑elasticsearch.yml
注释的内容就不贴出来了,只保留改动的部分.
(设置0.0.0.0可以让任何人访问到你的es)

[work@localhost config]$ pwd
/usr/local/src/elasticsearch/config
[work@localhost config]$ vi elasticsearch.yml
cluster.name: es-application
node.name: node-1
path.data: /home/work/data
path.logs: /home/work/log
network.host: 0.0.0.0
http.port: 9200
# 下面这两个是默认配置,我先保留等下会说
#discovery.seed_hosts: ["host1", "host2"]
#cluster.initial_master_nodes: ["node-1", "node-2"]

在ES用户下启动Elasticsearch

[work@localhost bin]$ pwd
/home/work/elasticsearch-7.2.0/bin
[work@localhost bin]$ sh elasticsearch

报错

ERROR: [5] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [1024] 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]
[4]: failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[5]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
[2019-04-09T09:51:29,228][INFO ][o.e.n.Node ] [localhost.localdomain] stopping …
[2019-04-09T09:51:29,264][INFO ][o.e.n.Node ] [localhost.localdomain] stopped
[2019-04-09T09:51:29,265][INFO ][o.e.n.Node ] [localhost.localdomain] closing …
[2019-04-09T09:51:29,320][INFO ][o.e.n.Node ] [localhost.localdomain] closed
[2019-04-09T09:51:29,323][INFO ][o.e.x.m.p.NativeController] [localhost.localdomain] Native controller process has stopped - no new native processes can be started

切换root用户
vi /etc/security/limits.conf

在倒数第二行
 
* soft nofile 65536
* hard nofile 65536
# End of file
vi /etc/sysctl.conf
添加

vm.max_map_count=655360

保存后执行
sysctl -p

重启es 异常

ERROR: [3] bootstrap checks failed
[1]: max number of threads [1024] for user [es] is too low, increase to at least [4096]
[2]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[3]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

vi /etc/security/limits.d/90-nproc.conf
修改
*          soft    nproc     1024
为
*          soft    nproc     4096

重启es 异常

ERROR: [2] bootstrap checks failed
[1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[2]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

在 elasticsearch.yml中添加配置项:bootstrap.system_call_filter为false:

bootstrap.memory_lock: false
bootstrap.system_call_filter: false

重新启动依旧报错
ERROR: [1] bootstrap checks failed
[1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

在这里插入图片描述

需要配置cluster.initial_master_nodes,修改 elasticsearch.yml,
由于暂时我们的es是单节点的,所以只要配置一个node节点就行了,
这里node-1必须和 node.name: 名称相同

cluster.initial_master_nodes: ["node-1"]

重启后————》成功
Elasticsearch7.x指定JDK及安装时报错_第1张图片

注意

需要关闭防火墙

浏览器访问

Elasticsearch7.x指定JDK及安装时报错_第2张图片

指定JDK

前面我们说到,我的虚拟机开发环境的jdk是1.8,在启动Elasticsearch7.2的时候,启动日志会有如下信息:

future versions of Elasticsearch will require Java 11; your Java version from [/opt/jdk1.8.0_211/jre] does not meet this requirement

这是由于Elasticsearch依赖于jdk,es和jdk有着对应的依赖关系。具体可见:
https://www.elastic.co/cn/support/matrix
https://www.elastic.co/guide/en/elasticsearch/reference/7.2/setup.html
Elasticsearch7.x指定JDK及安装时报错_第3张图片

这里是说Elasticsearch该版本内置了JDK,而内置的JDK是当前推荐的JDK版本。当然如果你本地配置了JAVA_HOME那么ES就是优先使用配置的JDK启动ES。(言外之意,你不安装JDK一样可以启动,我试了可以的。)
ES推荐使用LTS版本的JDK(这里只是推荐,JDK8就不支持),如果你使用了一些不支持的JDK版本,ES会拒绝启动。
那么哪些版本的JDK支持LTS呢?https://www.oracle.com/technetwork/java/java-se-support-roadmap.html
Elasticsearch7.x指定JDK及安装时报错_第4张图片

根据启动信息我们看到Elasticsearch7.2推荐使用JDK11,并且从刚才的截图得知可以下载openjdk 11.

安装OpenJDK11

·········· 下载安装包
[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# wget https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz
解压到指定目录

/usr/local/src/jdk-11.0.1

[root@localhost src]# tar -xzvf jdk-11.0.4_linux-x64_bin.tar.gz
到此结束

由于我们日常的代码开发都是使用的JDK1.8,所以这里不会把JAVA_HOME配置成JDK11,我们只需更改Elasticsearch的启动文件,使它指向我们下载的JDK11.

修改配置文件
[work@localhost elasticsearch-7.2.0]$ cd bin/
[work@localhost bin]$ vim elasticsearch

添加一下几行内容

#配置自己的jdk11
export JAVA_HOME=/usr/local/src/jdk-11.0.1
export PATH=$JAVA_HOME/bin:$PATH


#添加jdk判断
if [ -x "$JAVA_HOME/bin/java" ]; then
        JAVA="/usr/local/src/jdk-11.0.1/bin/java"
else
        JAVA=`which java`
fi

为了方便大家参考,这里贴上完整的配置文件elasticsearch

[work@localhost bin]$ vim 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=/usr/local/src/jdk-11.0.1
export PATH=$JAVA_HOME/bin:$PATH


#添加jdk判断
if [ -x "$JAVA_HOME/bin/java" ]; then
        JAVA="/usr/local/src/jdk-11.0.1/bin/java"
else
        JAVA=`which java`
fi


source "`dirname "$0"`"/elasticsearch-env

if [ -z "$ES_TMPDIR" ]; then
  ES_TMPDIR=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.TempDirectory`
fi

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}"

# 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 $?

启动ES
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2019-08-02T15:33:27,994][INFO ][o.e.e.NodeEnvironment    ] [node-1] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [32.6gb], net total_space [35.7gb], types [rootfs]
[2019-08-02T15:33:28,014][INFO ][o.e.e.NodeEnvironment    ] [node-1] heap size [1015.6mb], compressed ordinary object pointers [true]
[2019-08-02T15:33:28,017][INFO ][o.e.n.Node               ] [node-1] node name [node-1], node ID [8zGCWQhBS3OpVShqjSgU-w], cluster name [es-application]
[2019-08-02T15:33:28,017][INFO ][o.e.n.Node               ] [node-1] version[7.2.0], pid[1545], build[default/tar/508c38a/2019-06-20T15:54:18.811730Z], OS[Linux/3.10.0-862.el7.x86_64/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/11.0.1/11.0.1+13]
[2019-08-02T15:33:28,017][INFO ][o.e.n.Node               ] [node-1] JVM home [/opt/jdk-11.0.1]
[2019-08-02T15:33:28,018][INFO ][o.e.n.Node               ] [node-1] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -Des.networkaddress.cache.ttl=60, -Des.networkaddress.cache.negative.ttl=10, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.io.tmpdir=/tmp/elasticsearch-5247006010869253587, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m, -Djava.locale.providers=COMPAT, -Dio.netty.allocator.type=unpooled, -XX:MaxDirectMemorySize=536870912, -Des.path.home=/usr/local/src/elasticsearch, -Des.path.conf=/usr/local/src/elasticsearch/config, -Des.distribution.flavor=default, -Des.distribution.type=tar, -Des.bundled_jdk=true]

到这里我们又看到另一个警告

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
这是提醒你 cms 垃圾收集器在 jdk9 就开始被标注为 @deprecated

看下JDK11支持的垃圾回收器

https://docs.oracle.com/en/java/javase/11/gctuning/garbage-first-garbage-collector.html#GUID-0394E76A-1A8F-425E-A0D0-B48A3DC82B42

在这里插入图片描述
修改 jvm.options
将 : -XX:+UseConcMarkSweepGC
改为:-XX:+UseG1GC

完整配置如下
## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g
-Xmx1g

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
-XX:+UseG1GC
-XX:CMSInitiatingOccupancyFraction=75
-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration
# NOTE: G1GC is only supported on JDK version 10 or later.
# To use G1GC uncomment the lines below.
# 10-:-XX:-UseConcMarkSweepGC
# 10-:-XX:-UseCMSInitiatingOccupancyOnly
# 10-:-XX:+UseG1GC
# 10-:-XX:InitiatingHeapOccupancyPercent=75

## DNS cache policy
# cache ttl in seconds for positive DNS lookups noting that this overrides the
# JDK security property networkaddress.cache.ttl; set to -1 to cache forever
-Des.networkaddress.cache.ttl=60
# cache ttl in seconds for negative DNS lookups noting that this overrides the
# JDK security property networkaddress.cache.negative ttl; set to -1 to cache
# forever
-Des.networkaddress.cache.negative.ttl=10

## optimizations

# pre-touch memory pages used by the JVM during initialization
-XX:+AlwaysPreTouch

## basic

# explicitly set the stack size
-Xss1m

# set to headless, just in case
-Djava.awt.headless=true

# ensure UTF-8 encoding by default (e.g. filenames)
-Dfile.encoding=UTF-8

# use our provided JNA always versus the system one
-Djna.nosys=true

# turn off a JDK optimization that throws away stack traces for common
# exceptions because stack traces are important for debugging
-XX:-OmitStackTraceInFastThrow

# flags to configure Netty
-Dio.netty.noUnsafe=true
-Dio.netty.noKeySetOptimization=true
-Dio.netty.recycler.maxCapacityPerThread=0

# log4j 2
-Dlog4j.shutdownHookEnabled=false
-Dlog4j2.disable.jmx=true

-Djava.io.tmpdir=${
     ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## JDK 8 GC logging

8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
# due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise
# time/date parsing will break in an incompatible way for some date patterns and locals
9-:-Djava.locale.providers=COMPAT

你可能感兴趣的:(Elasticsearch,Java)