手把手教你如何在 Centos 7 上安装 elasticsearch7.x

声明

本文记录了 elasticsearch7 的整个安装过程(以下简称 es),安装过程中出现一个错误,就解决一个。因此建议通读全文后,再自行安装。

另外,不同版本 的 es,不同版本的操作系统,不同的机器配置,会导致安装过程中出现的错误存在差异,需要见招拆招。

一、安装环境

  • 一台 Centos7 虚拟机
  • 2个单核 CPU 、4GB 内存、100G 硬盘容量
  • elasticsearch-7.5.1

二、安装过程

ES 是一个 Java 应用程序,因此需要机器上安装有 jdk。为了帮助用户更快地使用上 ES,ES7 版本中已经预先捆绑了 JDK。

如果想使用指定的的 jdk,需要配置 jdk 的环境变量。

elasticsearch-7.5.1 自带的 jdk 在 es 的安装目录下, jdk 版本 是 13.

[root@virtual-linux01 bin]# ./java -version
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.1+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.1+9, mixed mode, sharing)

2.1 准备安装包

elasticsearch-7.5.1-linux-x86_64.tar

下载地址: https://www.elastic.co/cn/downloads/elasticsearch

2.2 解包

tar -xvf elasticsearch-7.5.1-linux-x86_64.tar

2.3 查看默认分配的堆内存空间

进入 es 安装目录的 config 目录, 查看 jvm.options 文件, 可以看到 elasticsearch-7.0 默认分配的最大、最小堆内存都是 1G。

因此,启动前请确认自己的机器内存大小,和 es 所需要的内存大小,以便做调整。

# 查看 
cat jvm.options

# 找到对应的配置项
# You should always set the min and max JVM heap size to the same value
-Xms1g
-Xmx1g

2.4 启动 es

进入 es 安装目录下的 bin 目录,执行启动脚本.(显然启动脚本里是通过 java 命令运行 es,并且启动参数会从 jvm.options 文件中读取)

2.4.1 启动脚本中具体执行的命令如下
exec /home/test/elasticsearch/jdk/bin/java 

-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 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.locale.providers=COMPAT 

# 注意这两个参数
-Xms1g -Xmx1g 
-XX:+UseConcMarkSweepGC 

-XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Djava.io.tmpdir=/tmp/elasticsearch-1414717080572387589 -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=536870912 -Des.path.home=/mano/module/elasticsearch -Des.path.conf=/mano/module/elasticsearch/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp '/mano/module/elasticsearch/lib/*' 

org.elasticsearch.bootstrap.Elasticsearch

可以看到用的是 es 自带的 jdk,并且堆内存大小是 1g,

2.4.2 执行启动脚本

启动日志出现了两条告警信息:

  1. java 的 UseConcMarkSweepGC 启动参数过时(不影响es的启动,有兴趣自行研究)
  2. 不能以 root 用户启动 es
[root@virtual-linux01 bin]# ./elasticsearch

## 报错信息
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2020-11-25T18:47:54,249][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [base-omu] 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.5.1.jar:7.5.1]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.5.1.jar:7.5.1]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.5.1.jar:7.5.1]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.5.1.jar:7.5.1]
        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.5.1.jar:7.5.1]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.5.1.jar:7.5.1]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.5.1.jar:7.5.1]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.5.1.jar:7.5.1]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.5.1.jar:7.5.1]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.5.1.jar:7.5.1]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.5.1.jar:7.5.1]
        ... 6 more
2.4.3 使用普通用户启动 es

下面涉及到 linux 相关命令,不再详解。

# 添加分组zhangsan
groupadd zhangsan

# 添加一个用户 zhangsan,分组 为 zhangsan, 密码zhangsan
useradd zhangsan -g zhangsan -p zhangsan

# 更改 es 目录的权限 
chown -R zhangsan:zhangsan /home/test/elasticsearch-7.5.1

# 切换到 zhangsan 用户
su zhangsan

# 启动 es
./elasticsearch

如果不更改权限, 用普通用户启动 es, 会报如下错误:

Exception in thread "main" java.nio.file.AccessDeniedException: /home/test/elasticsearch-7.5.1/config/jvm.options
2.4.4 验证 es 运行结果

到此为止, es 应该能够正常运行。(如果不能,就接着往下看,因为此时出现的错误很可能在后面会得到解决的办法),可以通过 curl 命令发起请求。如果遇见 curl 命令不存在得情况,自行百度安装。或者先跳过,进行下一步。

[root@vultual-linux01 ~]# curl localhost:9200
{
  "name" : "base-omu",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "xqVPcAq8S5yPL6zCb87pVQ",
  "version" : {
    "number" : "7.5.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "3ae9ac9a93c95bd0cdc054951cf95d88e1e18d96",
    "build_date" : "2019-12-16T22:57:37.835892Z",
    "build_snapshot" : false,
    "lucene_version" : "8.3.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
2.4.5 配置允许外网访问

虽然我们能本地访问了,但是仍然无法通过浏览器访问 http://ip地址:9200 。此时需要修改如下配置:

修改 config 下的 elasticsearch.yml 文件的如下配置项:

network.host: 0.0.0.0

# 最好指定下默认端口, 测试过程中,如果不指定配置,再启动时,端口就变成 9300了,这里先不做深究。
http.port: 9200

启动后, 检查没有通过, 报错如下:

ERROR: [2] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[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

第一个问题:

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

# 切换至 root 用户
su 
# 编辑 /etc/sysctl.conf,追加以下内容:
vm.max_map_count=262144
# 保存后,执行:
sysctl -p

第二个问题:

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 文件中如下配置的其中的任意一项:

  • discovery.seed_hosts

  • discovery.seed_providers

  • cluster.initial_master_nodes

# 编辑elasticsearch.yml文件
修改
cluster.initial_master_nodes: ["node-1", "node-2"] 
为 
cluster.initial_master_nodes: ["node-1"]
再次启动 es

这时我们就可以通过 浏览器访问 http://服务器 ip:9200

三、安装总结

以上便是 elasticsearch 7.x 在 CentOS 7 上的单机安装全过程。

可以看出,当我们第一次启动成功后,发现无法通过外网访问 es 的端口时,我们停掉了 es,并配置了外网访问,然后第二次启动时,就出现了诸多错误。

这里大胆假设其中一个原因跟 host 相关配置有关,另外就是 es 第一次启动并关闭后占用了很多系统资源没有释放。 具体原因后面有机会会仔细研究。

另外, 在查阅资料时,发现他人在安装时出现的错误更多。文中未出现的错误还包含如下:

错误 1:

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

# 修改/etc/security/limits.conf文件,增加配置,用户退出后重新登录生效

* soft nofile 65536 
* hard nofile 65536

错误 2:

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

# 最大线程个数太低。修改配置文件/etc/security/limits.conf,增加配置
soft    nproc           4096
hard    nproc           4096
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值