前提准备
从官方网站 https://www.elastic.co, 下载最新版的 ElasticSearch, 截止目前, 最新版为 5.6.3, 直接下载链接. 除此之外, 还需要准备以下内容.
- Linux 虚拟机, 并设置虚拟机内存大小为 2GB, 请参考用 VirtualBox 构建虚拟环境
- 启动虚拟机, 我的虚拟机 ip 为
172.24.38.184
(各人会有不同) - 安装常用软件
yum install -y unzip vim wget curl
- 安装 Java 8, 请参考安装和配置 Java 8, 带你入坑!
- 新建用于运行 ElasticSearch 的用户, 因为 ES5 以后, 要求不能使用 root 用户运行. (用户名可自行决定, 本文中是
henry
)
useradd henry
passwd henry
安装 ES
ElasticSearch 是基于 Java 环境运行的, 只要当前 Linux 系统中 Java 环境配置正确, ElasticSearch 几乎是解压缩即可使用. 上传 elasticsearch-5.6.3.zip
文件到用户运行 ES 的用户主目录下
scp elasticsearch-5.6.3.zip henry@172.24.38.184
以 henry
用户登录虚拟机, 解压缩文件, 即可执行
unzip -q elasticsearch-5.6.3.zip
cd elasticsearch-5.6.3
./bin/elasticsearch
在启动 ES 的日志中, 我们可以留意到这样的内容 publish_address {127.0.0.1:9200}
, 接下来, 重新打开一个终端窗口, 以 henry
用户登录, 访问 curl 'http://127.0.0.1:9200'
查看 ES 是否运行成功, 如果看到如下图所示的输出, 那么说明你安装成功啦!
配置 ES
为了在安装 ES 之外的机器上, 访问 ES 服务, 需要配置网络和防火墙.
- henry 用户编辑
vim config/elasticsearch.yml
文件, 修改#network.host: 192.168.0.1
, 为network.host: 0.0.0.0
. - root 用户编辑
vim /etc/sysconfig/iptables
, 开放 9200 端口, 然后重启防火墙service iptables restart
.
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT
重启 ES 服务, 如果还出现问题, 请参考下一节内容.
常见问题
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
root 用户编辑 vim /etc/security/limits.conf
文件, 添加以下内容
* hard nofile 65536
[2]: max number of threads [1024] for user [henry] is too low, increase to at least [2048]
root 用户编辑 vim /etc/security/limits.d/90-nproc.conf
修改以下内容
* soft nproc 2048
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
root 用户编辑 vim /etc/sysctl.conf
添加以下内容
vm.max_map_count=262144
并执行命令 sysctl -p
[4]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
henry 用户编辑 vim config/elasticsearch.yml
文件, 添加以下内容
bootstrap.system_call_filter: false
可能需要重启服务器, 然后重启 ES 服务. 在外部通过浏览器访问 http://172.24.38.184:9200, 如果看到如下图所示的输出, 那么说明你配置成功啦!