一、下载
安装 Elasticsearch 之前,你需要先安装一个较新的版本的 Java,jdk1.8以上。
官网下载所需的版本:https://www.elastic.co/cn/downloads/elasticsearch
本文所下载的是7.5.1版本。
Elasticsearch7.x版本程序包默认打包jdk: 以至于7.x版本的程序包大小突然边300MB+ 对比6.x发现,包大了200MB+, 正是JDK的大小
二、安装
[root@localhost soft]# ls
elasticsearch-7.5.1 elasticsearch-7.5.1-linux-x86_64.tar.gz
[root@localhost soft]#
[root@localhost soft]# tar -zxvf elasticsearch-7.5.1-linux-x86_64.tar.gz
#安装...
[root@localhost soft]# cd elasticsearch-7.5.1/
[root@localhost elasticsearch-7.5.1]# ll
total 552
drwxr-xr-x 2 root root 4096 Dec 17 07:01 bin
drwxr-xr-x 2 root root 148 Dec 17 07:01 config
drwxr-xr-x 9 root root 107 Dec 17 07:01 jdk
drwxr-xr-x 3 root root 4096 Dec 17 07:01 lib
-rw-r--r-- 1 root root 13675 Dec 17 06:54 LICENSE.txt
drwxr-xr-x 2 root root 6 Dec 17 07:01 logs
drwxr-xr-x 38 root root 4096 Dec 17 07:01 modules
-rw-r--r-- 1 root root 523209 Dec 17 07:01 NOTICE.txt
drwxr-xr-x 2 root root 6 Dec 17 07:01 plugins
-rw-r--r-- 1 root root 8499 Dec 17 06:54 README.textile
目录结构:
- bin:可执行文件目录
- config:配置文件(elasticsearch.yml)
- data:Elasticsearch存放数据的文件
- lib:运行所需类库
- logs:日志文件
- modules:加载模块列表(其实是必要插件)
- plugins:插件文件(需自定义安装)
- jdk:Elasticsearch7.x版本程序包默认打包jdk
2.1、elasticsearch.yml
属性名 | 说明 |
---|---|
cluster.name | 配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称。 |
node.name | 节点名,es会默认随机指定一个名字,建议指定一个有意义的名称,方便管理 |
path.conf | 设置配置文件的存储路径,tar或zip包安装默认在es根目录下的config文件夹,rpm安装默认在/etc/ elasticsearch |
path.data | 设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开 |
path.logs | 设置日志文件的存储路径,默认是es根目录下的logs文件夹 |
path.plugins | 设置插件的存放路径,默认是es根目录下的plugins文件夹 |
bootstrap.memory_lock | 设置为true可以锁住ES使用的内存,避免内存进行swap |
network.host | 设置bind_host和publish_host,设置为0.0.0.0允许外网访问 |
http.port | 设置对外服务的http端口,默认为9200。 |
transport.tcp.port | 集群结点之间通信端口 |
discovery.zen.ping.timeout | 设置ES自动发现节点连接超时的时间,默认为3秒,如果网络延迟高可设置大些 |
discovery.zen.minimum_master_nodes | 主结点数量的最少值 ,此值的公式为:(master_eligible_nodes / 2) + 1 ,比如:有3个符合要求的主结点,那么这里要设置为2 |
三、运行
3.1、不能以root用户运行elasticsearch
出于安全考虑elasticsearch默认不允许使用root账户运行,当以root用户运行elasticsearch,将会报错:
[root@localhost bin]# ./elasticsearch
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/java/jdk1.8.0_171/jre] does not meet this requirement
[2020-04-21T20:09:32,531][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [HikvisionOS] 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
3.2、创建用户
[root@localhost bin]# useradd elasticsearch
[root@localhostbin]# cat /etc/passwd
...
elasticsearch:x:1003:1004::/home/elasticsearch:/bin/bash
3.3、修改ES文件夹的拥有者为新创建的用户
[root@localhost bin]# chown -R elasticsearch:elasticsearch /home/soft/elasticsearch-7.5.1/
[root@localhost soft]#
[root@localhost soft]# ll
total 283312
drwxr-xr-x 9 elasticsearch elasticsearch 154 Dec 17 07:01 elasticsearch-7.5.1
-rw-r--r-- 1 root root 290094012 Apr 21 19:36 elasticsearch-7.5.1-linux-x86_64.tar.gz
3.4、以新创建的用户启动ES
su elasticsearch -c '/home/soft/elasticsearch-7.5.1/bin/elasticsearch'
测试 Elasticsearch 是否启动成功,可以打开另一个终端,执行以下操作:
curl 'http://localhost:9200/?pretty'
#cURL是一个利用URL语法在命令行下工作的文件传输工具
你应该得到和下面类似的响应(response):
{
"name" : "kdc",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "xjUYX-BHRsmt3LnxTl5ESg",
"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"
}
这就意味着你现在已经启动并运行一个 Elasticsearch 节点了,你可以用它做实验了。 单个 节点 可以作为一个运行中的 Elasticsearch 的实例。 而一个 集群 是一组拥有相同 cluster.name
的节点, 他们能一起工作并共享数据,还提供容错与可伸缩性。(当然,一个单独的节点也可以组成一个集群) 你可以在 elasticsearch.yml
配置文件中 修改 cluster.name
,该文件会在节点启动时加载 (译者注:这个重启服务后才会生效)。
当 Elastcisearch 在前台运行时,你可以通过按 Ctrl+C 去停止。
3.3、带参数启动ES
后台启动 -d
[root@localhost elasticsearch-7.5.1]# su elasticsearch -c 'bin/elasticsearch -d -p pid'
# -p 选项可以将es进程PID记录到pid文件中,该文件可以指定
# -d 后台运行
[root@localhost elasticsearch-7.5.1]# cat pid
44250
指定内存-Xms
./elasticsearch -d -Xmx2g -Xms2g #后台启动,启动时指定内存大小(2G)
指定日志等级
./elasticsearch -d -Des.logger.level=DEBUG #可以在日志中打印出更加详细的信息。
3.4、集群配置
elasticsearch.yml配置ES相关信息:
1、bootstrap.memory_lock:true
设置为true可以锁住ES使用的内存,避免内存进行swap;
elasticsearch官网建议生产环境需要设置bootstrap.memory_lock: true;
若设置为true,需要在/etc/security/limits.conf
添加如下内容:
#allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
2、discovery.seed_hosts:
当要与其他主机上的节点组成群集时,必须使用discovery.seed_hosts设置提供群集中其他节点的列表,这些节点符合主服务器的条件,并且可能是活动的和可联系的,以便为发现过程设定种子。此设置通常应包含群集中所有符合主服务器条件的节点的地址。
3、cluster .initial_master_nodes:
当您第一次启动一个全新的ElasticSearch集群时,有一个集群引导步骤,它确定在第一次选举中计票的主合格节点集。在开发模式下,在没有配置发现设置的情况下,此步骤由节点本身自动执行。由于这种自动引导固有的不安全性,当您在生产模式下启动一个全新集群时,必须明确列出主合格节点,其投票应在第一次选举中计算。此列表是使用cluster.initial_master_nodes
设置的。
41 # Lock the memory on startup:
42 #
43 bootstrap.memory_lock: true
44 #
45 # Make sure that the heap size is set to about half the memory available
46 # on the system and that the owner of the process is allowed to use this
47 # limit.
48 #
49 # Elasticsearch performs poorly when the system is swapping the memory.
50 #
63 # --------------------------------- Discovery ----------------------------------
64 #
65 # Pass an initial list of hosts to perform discovery when this node is started:
66 # The default list of hosts is ["127.0.0.1", "[::1]"]
67 #
68 discovery.seed_hosts: ["10.41.62.165","10.41.62.65","10.41.62.50"]
69 #
70 # Bootstrap the cluster using an initial set of master-eligible nodes:
71 #
72 cluster.initial_master_nodes: ["ES_node_165","ES_node_65","ES_node_50"]
3.5、可能出现的问题
问题一:ERROR: bootstrap checks failed
max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]
解决:切换到root用户,编辑limits.conf 添加类似如下内容
vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
问题二:max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
解决:切换到root用户,进入limits.d目录下修改配置文件。
vi /etc/security/limits.d/90-nproc.conf
修改如下内容:
* soft nproc 1024
#修改为
* soft nproc 2048
问题三:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
解决:切换到root用户修改配置sysctl.conf
vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p