Linux下安装ElasticSearch
一、下载 & 安装
-
先安装JDK
-
下载elasticsearch-7.0.0-linux-x86_64.tar.gz
https://www.elastic.co/cn/downloads/elasticsearch
-
安装
cd /opt mkdir elasticsearch rz tar -zvxf elasticsearch-7.0.0-linux-x86_64.tar.gz -C elasticsearch/
-
配置
vim config/elasticsearch.yml
修改/添加以下配置
#--------------------Cluster ---------------------- cluster.name: my-application #集群的名称,同一个集群该值必须设置成相同的 #-------------------- Node ----------------------- node.name: node-1 #该节点的名字 #-------------------- Paths ----------------------- path.data: /path/to/data #数据存放目录 path.logs: /path/to/logs #日志存放目录 #-------------------- Memory -------------------- bootstrap.memory_lock: true #禁止es内存交换 #-------------------- Network -------------------- network.host: 192.168.6.3 #所在机器IP transport.tcp.port: 9300 #设置节点之间交互的端口号 http.port: 9200 #http访问端口 #使用head等插件监控集群信息需要打开此配置项 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-credentials: true #-------------------- Discovery ------------------- discovery.zen.ping.unicast.hosts: ["host1", "host2"] #设置集群中的Master节点的初始列表,可以通过这些节点来自动发现其他新加入集群的节点 discovery.seed_hosts: ["192.168.6.3:9300"] #7.0.0版本为此参数改为此项 discovery.zen.minimum_master_nodes: 1 #设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点,默认1 cluster.initial_master_nodes: ["node-1", "node-2"] #7.0.0版本为此参数改为此项
-
启动
elasticsearch-7.0.0/bin/elasticsearch
-
启动异常详见ElasticSearsh/安装中遇到的问题及解决方案
二、安装中遇到的问题及解决方案
-
启动
./bin/elasticsearch
-
java.lang.RuntimeException: can not run elasticsearch as root
更换非root用户启动
adduser elk # 添加用户 passwd elk # 给用户赋值 su elk -c './bin/elasticsearch' # 使用新用户启动
-
java.nio.file.AccessDeniedException: …config/jvm.options
原因:当前用户没有执行权限
chown linux用户名 elasticsearch安装目录 -R # ex: chown elk home/elasticsearch-6.2.4 -R
同时要赋予用户data目录和logs目录的权限
-
ERROR: [4] bootstrap checks failed
(1) max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
原因:每个进程最大同时打开文件数太小
查看当前每个进程最大同时打开文件数
ulimit -Sn ulimit -Hn
修改配置
vi /etc/security/limits.conf
添加如下内容
* soft nofile 65536 * hard nofile 131072
用户退出后重新登录生效
(2) memory locking requested for elasticsearch process but memory is not locked
原因:锁定内存失败
vim /etc/security/limits.conf
添加如下内容
* soft memlock unlimited * hard memlock unlimited
(3) max number of threads [3795] for user [elastic] is too low, increase to at least [4096]
原因:最大线程个数太低
vim /etc/security/limits.conf
添加如下内容
* soft nproc 2048 * hard nproc 4096
(4) max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
原因:操作系统的vm.max_map_count参数设置太小
vim /etc/sysctl.conf
添加
vm.max_map_count=262144
执行此命令使上面的配置生效
sysctl -p
或者直接通过命令设置
sysctl -w vm.max_map_count=262144 sysctl -a | grep "vm.max_map_count" //查看是否修改成功
-
启动elasticsearch直接退出,并返回killed
原因:内存不足, 需要设置es的虚拟机参数
vim elasticsearch-7.0.0/bin/elasticsearch
添加以下内容
ES_JAVA_OPTS="-Xms1g -Xmx1g"
或者
vim elasticsearch-7.0.0/config/jvm.options
添加以下内容
-Xms1g -Xmx1g
-
failed to obtain node locks
原因:本地最大储存节点数默认限制为1
vim elasticsearch.yml
添加以下内容
node.max_local_storage_nodes: 你的本地储存节点数
-
无法通过网页访问
先尝试本地访问
curl -get 192.168.6.3:9200
如果可以访问,关闭防火墙或开放端口即可
# 关闭防火墙 systemctl stop firewalld.service # 开启/关闭开机启动防火墙 systemctl enable/disable firewalld # 开放端口 firewall-cmd --permanent --add-port=9200/tcp --zone=public firewall-cmd --reload
-
启动head
设置软连接
cd node-v4.4.7-linux-x64/bin grunt -> /elasticsearch-head-master/node_modules/grunt/bin/grunt npm -> ../lib/node_modules/npm/bin/npm-cli.js
启动
grunt server &
软连接用法参见/linux/常用命令
三、使用中遇到的问题及解决方案
-
The number of object passed must be even but was [1]
setSource(json)
参数json在高版本不能直接使用String 需要转成Map -
Rejecting mapping update to [XXX] as the final mapping would have more than 1 type: [XXX, XX]
原因:6.0的版本不允许一个index下面有多个type,并且官方说是在接下来的7.0版本中会删掉type
-
Validation Failed: 1: this action would add [2] total shards, but this cluster currently has [3000]/[3000] maximum shards open
elasticsearch7默认每个node最多1000个shard,可通过
cluster.max_shards_per_node=3000
修改。但添加到yml中无效,可暂时通过http请求修改此值,等待官方debug。curl -X PUT localhost:9200/_cluster/settings -H "Content-Type: application/json" -d '{ "persistent": { "cluster.max_shards_per_node": "3000" } }'
-
Fielddata is disabled on text fields by default.
参考https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html
-
high disk watermark [0b] exceeded on [O2-Ef7fET9S_MJNAL-q_yA][Desmond Pitt] free: -1b[100%], shards will be relocated away from this node
es默认的磁盘分配策略问题。
设置
cluster.routing.allocation.disk.threshold_enabled: false
参考https://www.elastic.co/guide/en/elasticsearch/reference/2.1/disk-allocator.html
-
[parent] Data too large, data for [<transport_request>] would be larger than
fielddata的内存被占用完了,其他索引无法分配更多的内存
设置堆内存
./elasticsearch -Xms10g -Xmx10g
或export ES_HEAP_SIZE=10g
参考https://www.elastic.co/guide/cn/elasticsearch/guide/current/heap-sizing.html
es7修改了这个配置
export ES_JAVA_OPTS="-Xms10g -Xmx10g"
或修改config/jvm.options文件参考https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/jvm-options.html -
副本shards unassigned
硬盘容量超过一定百分比
禁用磁盘分配决策程序
cluster.routing.allocation.disk.threshold_enabled: false
参考https://www.elastic.co/guide/en/elasticsearch/reference/current/disk-allocator.html
四、安装head
-
安装git
yum install git
-
下载head
git clone git://github.com/mobz/elasticsearch-head.git
-
安装nodejs
参见Linux/安装nodejs
-
启动
cd elasticsearch-head/ npm install(使用cnpm需要先安装bzip2,yum install bzip2) npm run start open http://localhost:9100
在elasticsearch.yml中添加,然后重启elasticsearch
http.cors.enabled: true http.cors.allow-origin: "*"
五、安装kibana
-
下载
https://www.elastic.co/cn/downloads/kibana
-
解压
tar -zxvf kibana-7.0.0-linux-x86_64.tar.gz
-
修改配置
vim config/kibana.yml
添加以下内容
server.host: "192.168.6.3" elasticsearch.hosts: ["http://192.168.6.3:9200"]
-
启动
sh bin/kibana
-
访问
http://localhost:5601