小白学ES 02 - Elasticsearch单机服务的部署及常见问题解决

此部署过程以Elasticsearch-5.6.10版本为例, 后续的学习和演示也用此版本.

1 前提: 安装JDK

学习使用ES的前提是成功安装JDK —— 很基础的一项步骤, 这里省略.

此处学习演示所用的JDK版本为:

[root@host-10-0-20-50 ~]# java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

2 准备安装包

  1. 根据自己的系统版本下载相应的安装包, 链接如下:
    https://www.elastic.co/downloads/elasticsearch
    这里下载MacOS/Linux系统下的版本 elasticsearch-5.6.10.tar.gz.

  2. 上传并解压:

    # 工作路径
    mkdir /data/elk
    # 上传安装包后解压
    tar -zxf elasticsearch-5.6.10.tar.gz
    # 修改安装包文件名
    mv elasticsearch-5.6.10.tar.gz es-node1
    

3 创建ES专用用户

# 创建用户
useradd elastic -s /bin/bash 
# 为该用户赋予相关操作权限
chown -R elastic:elastic /data/elk 

4 启动ES服务

# 切换用户
su elastic 
# 启动服务, -d是指在后台启动, 若不用, 则Elasticsearch将
cd /data/elk/master
./bin/elasticsearch -d 

注意事项:

Elasticsearch必须以 非root用户启动, 否则将抛出如下错误:

[2018-11-05T04:24:50,035][ERROR][o.e.b.Bootstrap          ] Exception
java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:195) ~[elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:342) [elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:132) [elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:123) [elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:70) [elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134) [elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.cli.Command.main(Command.java:90) [elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) [elasticsearch-5.6.10.jar:5.6.10]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) [elasticsearch-5.6.10.jar:5.6.10]

5 验证ES服务是否可用

  • 通过 jps 命令查看当前系统中运行的所有Java进程, 前提是JDK的环境变量设置OK:

    [elastic@host-10-0-20-50 bin]$ jps
    25810 Elasticsearch   # Elasticsearch的进程号
    25926 Jps
    
  • 通过 ps -ef 或 ps aux 命令查看

    [elastic@host-10-0-20-50 bin]$ ps aux | grep elasticsearch 
    elastic  25810 23.0 28.6 5753680 2310200 pts/0 Sl   04:31   0:19 /server/jdk1.8.0_151/bin/java -Xms2g -Xmx2g 
    -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch 
    -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true 
    -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 
    -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError 
    -Des.path.home=/data/elk/es-node1 -cp /data/elk/es-node1/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
    elastic  27403  0.0  0.0 103260   852 pts/0    R+   04:33   0:00 grep elasticsearch
    
  • 在MacOS/Linux的终端(或Windows的命令行)下运行命令:

    # MacOS/Linux下: 
    curl http://localhost:9200/ 
    # Windows下: 
    Invoke-RestMethod http://localhost:9200 
    
    图片
  • 或在浏览器中访问 “http://localhost:9200/”, 若能出现类似上述终端中的信息, 说明ES服务启动成功.

    运行界面参数解释:

    {
      "name" : "Xa0PFJM",                   # 当前Node的名称
      "cluster_name" : "elasticsearch",     # 当前节点所属的集群名称(默认是elasticsearch)
      "cluster_uuid" : "Bk9p7xDhR8WM9HRx3pFEEA",
      "version" : {
        "number" : "5.6.10",                # 当前运行的ES的版本号
        "build_hash" : "b727a60",
        "build_date" : "2018-06-06T15:48:34.860Z",
        "build_snapshot" : false,           # 当前运行版本是否是从源代码构建而来
        "lucene_version" : "6.6.1"
      },
      "tagline" : "You Know, for Search"    # ES的第一个tabline
    }
    

6 关闭ES服务

Elasticsearch没有直接关闭或重启服务的命令, 关闭只能通过kill命令杀掉进程的方式, 如下:

[elastic@host-10-0-20-50 bin]$ ps aux | grep elasticsearch  # 查看ES进程的id
[elastic@host-10-0-20-50 bin]$  kill -8 25810  # 通过进程id来杀掉服务

7 重启ES服务

[elastic@host-10-0-20-50 bin]$ sh elasticsearch -d

当然可以编写一个服务脚本, 来方便快捷地启动或关闭Elasticsearch服务.

8 常见问题的解决

8.1 使用ES专属用户登录时出错

  • 问题描述:
    使用ES专属用户启动ES服务时, 终端抛出如下错误:

    [elastic@host-10-0-20-50 bin]$ 2018-11-05 04:26:38,466 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
         at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
         at java.lang.SecurityManager.checkPermission(SecurityManager.java:585)
         ......
    
    SettingsException[Failed to load settings from /data/elk/es-node1/config/elasticsearch.yml]; nested: AccessDeniedException[/data/elk/es-node1/config/elasticsearch.yml];
         ......
         
    Caused by: java.nio.file.AccessDeniedException: /data/elk/es-node1/config/elasticsearch.yml
         ......
    
  • 问题分析:

    错误信息说明: 当前用户无的访问被拒绝, 可知ES专属用户无法执行当前应用.

  • 解决方法:

    为ES创建专属用户后, 对其赋予相应的读写权限.

    # 为该用户赋予相关操作权限
    chown -R elastic:elastic /data/elk 
    

8.2 不能安装"syscall filter"

  • 问题描述:
    启动ES时, 抛出如下错误信息:

    [2018-11-06T03:12:35,812][WARN ][o.e.b.JNANatives         ] unable to install syscall filter: 
    java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in
     at org.elasticsearch.bootstrap.SystemCallFilter.linuxImpl(SystemCallFilter.java:329) ~[elasticsearch-5.6.10.jar:5.6.10]
     ......
    [2018-11-06T03:12:39,947][INFO ][o.e.n.Node               ] initialized
    [2018-11-06T03:12:39,947][INFO ][o.e.n.Node               ] [jVSUBme] starting ...
    [2018-11-06T03:12:40,131][INFO ][o.e.t.TransportService   ] [jVSUBme] publish_address {10.0.20.50:9300}, bound_addresses {[::]:9300}
    [2018-11-06T03:12:40,145][INFO ][o.e.b.BootstrapChecks    ] [jVSUBme] bound or publishing to a non-loopback address, enforcing bootstrap checks
    [2018-11-06T03:12:40,148][ERROR][o.e.b.Bootstrap          ] [jVSUBme] node validation exception
    [1] 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
    [2018-11-06T03:12:40,150][INFO ][o.e.n.Node               ] [jVSUBme] stopping ...
    [2018-11-06T03:12:40,186][INFO ][o.e.n.Node               ] [jVSUBme] stopped
    [2018-11-06T03:12:40,186][INFO ][o.e.n.Node               ] [jVSUBme] closing ...
    [2018-11-06T03:12:40,199][INFO ][o.e.n.Node               ] [jVSUBme] closed
    
  • 问题解决:

    Centos 6.5不支持SecComp, 而ES 5.x版本起 bootstrap.system_call_filter 的默认值是 true.
    禁用: 在elasticsearch.yml中配置 bootstrap.system_call_filter=false, 注意要在Memory配置项下添加:

    bootstrap.system_call_filter: false
    

8.3 无法锁定Memory

  • 问题描述:
    启动Elasticsearch时, 抛出如下错误信息:

    [2018-11-06T03:18:53,221][WARN ][o.e.b.JNANatives         ] Unable to lock JVM Memory: error=12, reason=Cannot allocate memory
    [2018-11-06T03:18:53,232][WARN ][o.e.b.JNANatives         ] This can result in part of the JVM being swapped out.
    [2018-11-06T03:18:53,232][WARN ][o.e.b.JNANatives         ] Increase RLIMIT_MEMLOCK, soft limit: 65536, hard limit: 65536
    [2018-11-06T03:18:53,233][WARN ][o.e.b.JNANatives         ] These can be adjusted by modifying /etc/security/limits.conf, for example: 
      # allow user 'elastic' mlockall
      elastic soft memlock unlimited
      elastic hard memlock unlimited
     [2018-11-06T03:18:53,233][WARN ][o.e.b.JNANatives         ] If you are logged in interactively, you will have to re-login for the new limits to take effect.
        ......
     [2018-11-06T03:18:57,644][ERROR][o.e.b.Bootstrap          ] [jVSUBme] node validation exception
     [1] bootstrap checks failed
     [1]: memory locking requested for elasticsearch process but memory is not locked
     [2018-11-06T03:18:57,646][INFO ][o.e.n.Node               ] [jVSUBme] stopping ...
     [2018-11-06T03:18:57,693][INFO ][o.e.n.Node               ] [jVSUBme] stopped
     [2018-11-06T03:18:57,693][INFO ][o.e.n.Node               ] [jVSUBme] closing ...
     [2018-11-06T03:18:57,707][INFO ][o.e.n.Node               ] [jVSUBme] closed
    
  • 问题分析:
    Elasticsearch的配置文件中有如下选项: bootstrap.memory_lock: true , 意为在启动Elasticsearch服务时, 锁定JVM需要的内存, 避免OS层面的Swap交换 ---- 降低ES服务性能.

    此选项默认为false, 即不开启锁定.

  • 问题解决:

    ① 在配置文件中用"#"注释掉bootstrap.memory_lock: true, 或修改其值为false;

    ② 或者修改系统 /etc/security/limits.conf 文件, 为ES专属用户elastic解除限制:

    # 在文件最后添加下述配置, 允许用户'elastic'锁定内存
    elastic soft memlock unlimited
    elastic hard memlock unlimited
    

版权声明

作者: ma_shoufeng(马瘦风)

出处: CSDN 马瘦风的博客

您的支持是对博主的极大鼓励, 感谢您的阅读.

本文版权归博主所有, 欢迎转载, 但未经博主同意必须保留此段声明, 且在文章页面明显位置给出原文链接, 否则博主保留追究法律责任的权利.

阅读更多
换一批

没有更多推荐了,返回首页