阅读 295

Centos7安装配置Elasticsearch

Elasticsearch下载地址

1. 解压之后移动到 /usr/local/ 目录下

[root@localhost software]# tar -zxvf elasticsearch-7.10.2-linux-x86_64.tar.gz 
[root@localhost software]# mv elasticsearch-7.10.2 /usr/local/
复制代码

2. 配置 config/elasticsearch.yml

# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
# 改个名字
cluster.name: test-elasticsearch
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
# 节点名称
node.name: es-node1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
# 数据存储路径
path.data: /usr/local/elasticsearch-7.10.2/data
#
# Path to log files:
# 日志存储路径
path.logs: /usr/local/elasticsearch-7.10.2/logs
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
# 
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
# 默认9200端口
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
# 对应上面配置的node.name
cluster.initial_master_nodes: ["es-node1"]
#
# For more information, consult the discovery and cluster formation module documentation.
复制代码

3. 配置 config/jvm.options

如果内存大,可以不修改

## JVM configuration

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
# 将原来的1g修改成128m
-Xms128m
-Xmx128m
复制代码

4. 启动Elasticsearch

4.1 第一个问题:Elasticsearch不允许使用root用户启动
解决方法:
  • 第1步. 先添加一个普通用户
[root@localhost elasticsearch-7.10.2]# useradd esuser
复制代码
  • 第2步. 对添加的用户授权,授权elasticsearch目录
[root@localhost elasticsearch-7.10.2]# pwd
/usr/local/elasticsearch-7.10.2
[root@localhost elasticsearch-7.10.2]# chown -R esuser /usr/local/elasticsearch-7.10.2
复制代码
  • 第3步. 查看elasticsearch目录当前用户
[root@localhost elasticsearch-7.10.2]# ll
总用量 568
drwxr-xr-x.  2 esuser root   4096 1月  13 08:46 bin
drwxr-xr-x.  3 esuser root    169 2月   2 11:34 config
drwxr-xr-x.  2 esuser root      6 2月   2 11:25 data
drwxr-xr-x.  9 esuser root    107 1月  13 08:46 jdk
drwxr-xr-x.  3 esuser root   4096 1月  13 08:46 lib
-rw-r--r--.  1 esuser root  13675 1月  13 08:39 LICENSE.txt
drwxr-xr-x.  2 esuser root      6 1月  13 08:44 logs
drwxr-xr-x. 53 esuser root   4096 1月  13 08:47 modules
-rw-r--r--.  1 esuser root 544318 1月  13 08:44 NOTICE.txt
drwxr-xr-x.  2 esuser root      6 1月  13 08:44 plugins
-rw-r--r--.  1 esuser root   7313 1月  13 08:39 README.asciidoc
[root@localhost elasticsearch-7.10.2]# 
复制代码

现在再看elasticsearch目录下的用户都是esuser了

整个操作命令

[root@localhost elasticsearch-7.10.2]# useradd esuser
[root@localhost elasticsearch-7.10.2]# pwd
/usr/local/elasticsearch-7.10.2
[root@localhost elasticsearch-7.10.2]# chown -R esuser /usr/local/elasticsearch-7.10.2
[root@localhost elasticsearch-7.10.2]# ll
总用量 568
drwxr-xr-x.  2 esuser root   4096 1月  13 08:46 bin
drwxr-xr-x.  3 esuser root    169 2月   2 11:34 config
drwxr-xr-x.  2 esuser root      6 2月   2 11:25 data
drwxr-xr-x.  9 esuser root    107 1月  13 08:46 jdk
drwxr-xr-x.  3 esuser root   4096 1月  13 08:46 lib
-rw-r--r--.  1 esuser root  13675 1月  13 08:39 LICENSE.txt
drwxr-xr-x.  2 esuser root      6 1月  13 08:44 logs
drwxr-xr-x. 53 esuser root   4096 1月  13 08:47 modules
-rw-r--r--.  1 esuser root 544318 1月  13 08:44 NOTICE.txt
drwxr-xr-x.  2 esuser root      6 1月  13 08:44 plugins
-rw-r--r--.  1 esuser root   7313 1月  13 08:39 README.asciidoc
复制代码

这个时候进入到 bin 目录下启动es,还是会报错 can not run elasticsearch as root

[root@localhost elasticsearch-7.10.2]# cd bin/
[root@localhost bin]# ./elasticsearch
[2021-02-02T13:34:16,921][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [es-node1] 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:174) ~[elasticsearch-7.10.2.jar:7.10.2]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.10.2.jar:7.10.2]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.10.2.jar:7.10.2]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127) ~[elasticsearch-cli-7.10.2.jar:7.10.2]
        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.10.2.jar:7.10.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.10.2.jar:7.10.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.10.2.jar:7.10.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:111) ~[elasticsearch-7.10.2.jar:7.10.2]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178) ~[elasticsearch-7.10.2.jar:7.10.2]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.10.2.jar:7.10.2]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.10.2.jar:7.10.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:111)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /usr/local/elasticsearch-7.10.2/logs/imooc-elasticsearch.log
[root@localhost bin]# 2021-02-02 05:34:17,281350 UTC [118575] INFO  Main.cc@103 Parent process died - ML controller exiting
复制代码
  • 第4步. 使用 su 命令切换到 esuser 用户 ,再进行启动
[root@localhost bin]# su esuser
[esuser@localhost bin]$ ./elasticsearch
Exception in thread "main" java.nio.file.AccessDeniedException: /usr/local/elasticsearch-7.10.2/config/elasticsearch.keystore
        at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
        at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:218)
        at java.base/java.nio.file.Files.newByteChannel(Files.java:375)
        at java.base/java.nio.file.Files.newByteChannel(Files.java:426)
        at org.apache.lucene.store.SimpleFSDirectory.openInput(SimpleFSDirectory.java:79)
        at org.elasticsearch.common.settings.KeyStoreWrapper.load(KeyStoreWrapper.java:220)
        at org.elasticsearch.common.settings.HasPasswordKeyStoreCommand.execute(HasPasswordKeyStoreCommand.java:42)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
        at org.elasticsearch.cli.MultiCommand.execute(MultiCommand.java:91)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.common.settings.KeyStoreCli.main(KeyStoreCli.java:43)
Exception in thread "main" org.elasticsearch.bootstrap.BootstrapException: java.nio.file.AccessDeniedException: /usr/local/elasticsearch-7.10.2/config/elasticsearch.keystore
Likely root cause: java.nio.file.AccessDeniedException: /usr/local/elasticsearch-7.10.2/config/elasticsearch.keystore
        at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
        at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:218)
        at java.base/java.nio.file.Files.newByteChannel(Files.java:375)
        at java.base/java.nio.file.Files.newByteChannel(Files.java:426)
        at org.apache.lucene.store.SimpleFSDirectory.openInput(SimpleFSDirectory.java:79)
        at org.elasticsearch.common.settings.KeyStoreWrapper.load(KeyStoreWrapper.java:220)
        at org.elasticsearch.bootstrap.Bootstrap.loadSecureSettings(Bootstrap.java:240)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
[esuser@localhost bin]$ 
复制代码

还是报错:java.nio.file.AccessDeniedException: /usr/local/elasticsearch-7.10.2/config/elasticsearch.keystore

没有权限访问elasticsearch.keystore

  • 第5步. 需要再进行一次授权
# 回退到elasticsearch目录
[esuser@localhost bin]$ cd ..
# 切换到root用户
[esuser@localhost elasticsearch-7.10.2]$ su root
密码:
[root@localhost elasticsearch-7.10.2]# pwd
/usr/local/elasticsearch-7.10.2
# 再次对esuser授权
[root@localhost elasticsearch-7.10.2]# chown -R esuser:esuser /usr/local/elasticsearch-7.10.2
复制代码

授权后再切换到 esuser用户,进行es的启动

[root@localhost elasticsearch-7.10.2]# su esuser
[esuser@localhost elasticsearch-7.10.2]$ cd bin/
[esuser@localhost bin]$ ./elasticsearch
复制代码

又报错,报错信息如下

ERROR: [3] 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 [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]
复制代码
  1. 打开最大文件数太少,应该修改成最少65535
  2. 对于用户esuser,最大线程数太少,应该修改成最少4096
  3. vm.max_map_count也是太少,应该修改成最少262144

  • 第6步. 切换回root用户,修改 /etc/security/limits.conf 文件
[esuser@localhost elasticsearch-7.10.2]$ su root
密码:
[root@localhost elasticsearch-7.10.2]# vim /etc/security/limits.conf
复制代码

添加如下配置

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
复制代码

limits.conf完整配置如下

# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
#
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open file descriptors
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

# End of file
复制代码
  • 第7步. 修改 /etc/sysctl.conf 文件,在最下面添加一行配置 vm.max_map_count=262145
[root@localhost elasticsearch-7.10.2]# vim /etc/sysctl.conf 
复制代码

保存之后,需要使用命令刷新一下

[root@localhost elasticsearch-7.10.2]# sysctl -p
vm.max_map_count = 262145
[root@localhost elasticsearch-7.10.2]# 
复制代码

这个时候再切换到 esuser 用户,再启动es,我不信还启动不成功。。。

[root@localhost elasticsearch-7.10.2]# su esuser
[esuser@localhost elasticsearch-7.10.2]$ cd bin/
[esuser@localhost bin]$ ./elasticsearch
复制代码

启动成功:

启动成功

如果你没改过默认端口的话,ip:9200就可以访问了

{
    "name":"es-node1",
    "cluster_name":"test-elasticsearch",
    "cluster_uuid":"HboO8iLRQkO2AZ8yAjV5Jw",
    "version":{
        "number":"7.10.2",
        "build_flavor":"default",
        "build_type":"tar",
        "build_hash":"747e1cc71def077253878a59143c1f785afa92b9",
        "build_date":"2021-01-13T00:42:12.435326Z",
        "build_snapshot":false,
        "lucene_version":"8.7.0",
        "minimum_wire_compatibility_version":"6.8.0",
        "minimum_index_compatibility_version":"6.0.0-beta1"
    },
    "tagline":"You Know, for Search"
}
复制代码

后台启动方式

[esuser@localhost bin]$ ./elasticsearch -d
复制代码

后台启动关闭方式

[esuser@localhost bin]$ ps -ef | grep elasticsearch
esuser    74171      1 21 15:00 pts/0    00:00:24 /usr/local/elasticsearch-7.10.2/jdk/bin/java -Xshare:auto -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 -XX:+ShowCodeDetailsInExceptionMessages -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.locale.providers=SPI,COMPAT -Xms128m -Xmx128m -XX:+UseG1GC -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -Djava.io.tmpdir=/tmp/elasticsearch-8577041199296559424 -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 -XX:MaxDirectMemorySize=67108864 -Des.path.home=/usr/local/elasticsearch-7.10.2 -Des.path.conf=/usr/local/elasticsearch-7.10.2/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /usr/local/elasticsearch-7.10.2/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
esuser    74225  74171  0 15:00 pts/0    00:00:00 /usr/local/elasticsearch-7.10.2/modules/x-pack-ml/platform/linux-x86_64/bin/controller
esuser    76188  56187  0 15:01 pts/0    00:00:00 grep --color=auto elasticsearch
[esuser@localhost bin]$ kill 74171
复制代码

找到controller那条对应的id:74171,

使用kill命令关闭。

文章分类
后端
文章标签