Elasticsearch安装使用

本文以目前最新版本elasticsearch-6.4.3为例

下载

elasticsearch官方下载:https://www.elastic.co/cn/downloads/elasticsearch

1
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.tar.gz

解压

1
tar -xvf elasticsearch-6.4.3.tar.gz

单例安装

注意:elasticsearch安装时不能以root权限进行安装,否则会报以下错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@kiko1 bin]# ./elasticsearch
[2018-11-10T17:41:55,713][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] 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:140) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.4.3.jar:6.4.3]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[elasticsearch-6.4.3.jar:6.4.3]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:104) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:326) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.4.3.jar:6.4.3]
... 6 more

创建专有用户

1
2
3
4
5
6
## root权限下新增一个用户
useradd kiko
#授权
chown -R kiko /home/kiko/tools/elasticsearch-*
## 进入bin目录下进行安装
[kiko@kiko1 bin]$ ./elasticsearch

启动

1
[kiko@kiko1 bin]$ ./elasticsearch &

测试

1
[kiko@kiko1 config]$ curl localhost:9200

响应结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name" : "2124wwq",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Ir233ETeSx2uSTfEqJ5Egw",
"version" : {
"number" : "6.4.3",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "fe40335",
"build_date" : "2018-10-30T23:17:19.084789Z",
"build_snapshot" : false,
"lucene_version" : "7.4.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

配置 elasticsearch.yml

elasticsearch-6.4.3/config/elasticsearch.yml

1
2
3
4
5
6
## 允许外网访问
network.host: 0.0.0.0
##允许跨域访问,方便后面可视化工具elasticsearch-head
http.cors.enabled: true
http.cors.allow-origin: "*"

配置后重启发现报错,并且不能重启成功

1
2
3
4
5
6
7
8
[2018-11-21T19:52:41,151][INFO ][o.e.b.BootstrapChecks ] [2124wwq] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [2] bootstrap checks failed
[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2018-11-21T19:52:41,221][INFO ][o.e.n.Node ] [2124wwq] stopping ...
[2018-11-21T19:52:41,267][INFO ][o.e.n.Node ] [2124wwq] stopped
[2018-11-21T19:52:41,268][INFO ][o.e.n.Node ] [2124wwq] closing ...
[2018-11-21T19:52:41,285][INFO ][o.e.n.Node ] [2124wwq] closed

解决方法,用root权限修改limits.conf配置

1
vim /etc/security/limits.conf

查找到下配置参数并修改为以下值

1
2
3
4
* soft nproc 2048
* hard nproc 4096
* soft nofile 65536
* hard nofile 131072

1
vim /etc/sysctl.conf

sysctl.conf 最后一行添加如下配置

1
vm.max_map_count = 655360

然后使其生效

1
sysctl -p

保存后重新用kiko用户启动./elasticsearch &

启动完毕之后,打开浏览器输入链接:
http://192.168.229.115:9200

分布式安装

主节点配置

1
2
3
4
5
vim config/elasticsearch.yml
#添加一下配置
cluster.name: kobe
node.name: master
node.master: true

重启服务。

从节点1配置

如果是一台服务器,从新解压一份到slave1文件夹

1
2
3
4
5
6
7
8
vim config/elasticsearch.yml
cluster.name: kobe
node.name: slave1
http.port: 9800
network.host: 0.0.0.0
#设置主动发现主节点地址
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

如果是在另外一台服务器,按照单例安装方式解压配置安装后

1
2
3
4
5
6
7
vim config/elasticsearch.yml
cluster.name: kobe
node.name: slave1
network.host: 0.0.0.0
#设置主动发现主节点地址
discovery.zen.ping.unicast.hosts: ["192.168.229.115"] #这里是主节点