以 7.16.1 版本为例
1. 下载 Elasticsearch
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.1-linux-x86_64.tar.gz
2. 下载完解压缩后,执行启动命令,就可以在本机上使用了。
$ tar -zxf elasticsearch-7.16.1-linux-x86_64.tar.gz -C /usr/local
$ cd /usr/local/elasticsearch-7.16.1
$ ./elasticsearch
Elasticsearch 不允许以 root 账号运行,要创建一个新账号,比如 es。
$ useradd es
$ chown es:es /usr/local/elasticsearch-7.16.1
$ su es
$ ./elasticsearch
后台运行需要加 -d 参数:
$ ./elasticsearch -d
启动之后,就可以访问了:
[es@localhost config]$ curl -X GET http://127.0.0.1:9200
{
"name" : "localhost.localdomain",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "OmvHEILHTRyRKp1DBhiwuA",
"version" : {
"number" : "7.16.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "5b38441b16b1ebb16a27c107a4c3865776e20c53",
"build_date" : "2021-12-11T00:29:38.865893768Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
3. 允许远程访问
默认情况下,Elasticsearch 不允许远程访问,只能本机访问。要开放远程访问,需要做如下配置:
$ vi /usr/local/elasticsearch-7.16.1/config/elasticsearch.yml
将 network.host 配置项的值改为 0.0.0.0
- network.host: 0.0.0.0
保存后启动,发现报错:
[es@localhost config]$
ERROR: [4] bootstrap checks failed. You must address the points described in the following [4] lines before starting Elasticsearch.
bootstrap check failure [1] of [4]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
bootstrap check failure [2] of [4]: max number of threads [3805] for user [es] is too low, increase to at least [4096]
bootstrap check failure [3] of [4]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
bootstrap check failure [4] of [4]: 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
ERROR: Elasticsearch did not exit normally - check the logs at /usr/local/elasticsearch-7.16.1/logs/elasticsearch.log
可以看到有4条错误提示:
- 单个进程能打开的文件数太少,至少要65535;
- es 这个 user 可以创建的线程数太少,至少要4096;
- 可用的虚拟内存太少,至少要262144;
- 开放 Elasticsearch 的远程访问,Elasticsearch 就认为当前环境是生产环境,要求配置参与集群选主的节点列表。
接下来逐个解决上述 4 个问题。
- 先解决第1个和第2个问题,调整进程打开的文件数上限和es这个user可以创建的线程数上限,* 代表所有用户。
$ vi /etc/security/limits.conf
添加以下配置:
#增加下面这2行是为了实现elasticsearch允许远程访问,允许进程打开的最大文件数。
* soft nofile 65535
* hard nofile 65535
#增加下面这2行是为了实现elasticsearch允许远程访问,允许进程打开的最大线程数。
* soft nproc 4096
* hard nproc 4096
- 再解决第3个问题,将虚拟内存调大。
# vi /etc/sysctl.conf
添加以下配置:
#elasticsearch允许远程访问时,必须配置该项,默认的65530太小。
vm.max_map_count=262144
重新加载配置文件,让配置生效:
$ sysctl -p
- 最后解决第4个问题,
# vi /usr/local/elasticsearch-7.16.1/config/elasticsearch.yml
添加以下配置项,将运行模式指定为单节点模式,就解决了第4个问题:
discovery.type: single-node
如果是docker,则在启动时加上该配置项:
$ docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.16.1
也可以用另一个配置项解决第4个问题:
discovery.seed_hosts : 127.0.0.1:9300
如果是要构建自己的docker镜像,那么应该在 Dockerfile 中添加如下配置:
FROM docker.elastic.co/elasticsearch/elasticsearch:7.16.1
RUN echo discovery.seed_hosts : 127.0.0.1:9300 >> /usr/local/elasticsearch-7.16.1/config/elasticsearch.yml
RUN cat /usr/local/elasticsearch-7.16.1/config/elasticsearch.yml
解决完这4个问题后,就可以成功启动Elasticsearch了,当然,要开启9200端口,集群模式还要开启9300端口。
$ firewall-cmd --zone=public --add-port=9200/tcp --permanent
$ firewall-cmd --reload
$ firewall-cmd --list-ports