-
// 切换到root用户 su - root
// 切换到esuser用户 su - esuser
// elasticsearch install begin **************************************************************************************************************************************** - 环境:
系统环境 | rhel-server-7.3-x86_64-dvd.iso |
JDK版本 | jdk-8u144-linux-x64.tar.gz |
ElasticSearch版本 | elasticsearch-5.5.1.tar.gz |
| elasticsearch-servicewrapper-master.zip |
Nodejs版本 | node-v8.2.1-linux-x64.tar.xz |
phantomjs版本 | phantomjs-2.1.1-linux-x86_64.tar.bz2 |
elasticsearch-head版本 | elasticsearch-head-master.zip |
kibana版本 | kibana-5.5.1-linux-x86_64.tar.gz |
- 创建用户(以root身份执行)
useradd esuser - 关闭防火墙或开放端口(以root身份执行)
systemctl stop firewalld
或
systemctl disable firewalld
或
firewall-cmd --add-port=9200/tcp permanent
firewall-cmd --add-port=9300/tcp permanent
firewall-cmd --reload - JDK安装(以root身份执行)
拷贝 | jdk-8u144-linux-x64.tar.gz 至 /opt/下 |
切换目录 | cd /opt/ |
解压 | tar -zxvf jdk-8u144-linux-x64.tar.gz |
授权 | chmod -R esuser+rwx /opt/jdk1.8.0_144/* |
配置环境变量 | vi /etc/profile |
增加如下内容 | |
JAVA_HOME=/opt/jdk1.8.0_144 PATH=$JAVA_HOME/bin:$PATH CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar export PATH JAVA_HOME CLASSPATH | |
按 ESC 键,输入 :wq 保存退出 | |
变量生效 | source /etc/profile |
查看结果 | Java -version |
含有如下内容代表成功 | |
java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode) |
- ElasticSearch环境配置(以root身份执行)
编辑配置文件 | vi /etc/sysctl.conf |
在空白处增加 | vm.max_map_count=262144 |
按 ESC 键,输入 :wq 保存退出 | |
加载系统参数 | sysctl -p |
编辑配置文件 | vi /etc/security/limits.conf |
在空白处增加 | esuser - nofile 65536 |
按 ESC 键,输入 :wq 保存退出 | |
重启系统 | |
执行命令 | su lionsio -c "ulimit -a" |
结果含有如下内容代表成功 | |
open files (-n) 65536 |
- 安装ElasticSearch:
以root身份执行以下命令 | |
拷贝 | elasticsearch-5.5.1.tar.gz 至 /opt/ 下 |
切换目录 | cd /opt/ |
解压 | tar -zxvf elasticsearch-5.5.1.tar.gz |
授权 | chmod -R esuser+rwx /opt/elasticsearch-5.5.1/* |
以esuser身份执行以下命令 | |
切换目录 | cd /opt/elasticsearch-5.5.1 |
编辑配置文件 | vi config/elasticsearch.yml |
更改或增加如下内容 | |
cluster.name: cls-es node.name: node-es1 path.data: /path/to/data #(注:应确保目录存在,并有权限访问) path.logs: /path/to/logs #(注:应确保目录存在,并有权限访问) network.host: 0.0.0.0 http.port: 9200 http.cors.enabled: true http.cors.allow-origin: "*" discovery.zen.ping.unicast.hosts: ["你的真实IP"] discovery.zen.minimum_master_nodes: 1 | |
按 ESC 键,输入 :wq 保存退出 | |
启动ElasticSearch | ./bin/elasticsearch -d |
访问地址 |
// elasticsearch install end & elasticsearch-head install begin ****************************************************************************************************************************************
- 安装nodejs(以root身份执行)
拷贝 | node-v8.2.1-linux-x64.tar.xz 至 /opt/ 下 |
切换目录 | cd /opt/ |
解压 | tar -xvf node-v8.2.1-linux-x64.tar.xz |
授权 | chmod -R esuser+rwx /opt/node-v8.2.1-linux-x64/* |
配置环境变量 | vi /etc/profile |
增加如下内容: | |
PATH=/opt/node-v8.2.1-linux-x64/bin:$PATH export PATH | |
按 ESC 键,输入 :wq 保存退出 | |
变量生效 | source /etc/profile |
查看结果 | node -v |
含有如下内容代表成功 | |
v8.2.1 | |
- 安装phantomjs(以root身份执行)
拷贝 | phantomjs-2.1.1-linux-x86_64.tar.bz2 至 /opt/ 下 |
切换目录 | cd /opt/ |
解压 | tar -jxvf elasticsearch-5.5.1.tar.gz |
授权 | chmod -R esuser+rwx /opt/phantomjs-2.1.1-linux-x86_64/* |
配置环境变量 | vi /etc/profile |
增加如下内容 | |
PATH=/opt/phantomjs-2.1.1-linux-x86_64/bin:$PATH export PATH | |
按 ESC 键,输入 :wq 保存退出 | |
查看结果 | phantomjs -v |
含有如下内容代表成功 | |
2.1.1 | |
- 安装ElasticSearch-head(以root身份执行)
拷贝 | elasticsearch-head-master.zip 至 /opt/ 下 |
切换目录 | cd /opt/ |
解压 | unzip elasticsearch-head-master.zip |
授权 | chmod -R esuser+rwx /opt/elasticsearch-head-master/* |
切换目录 | cd /opt/elasticsearch-head-master/ |
执行安装 | npm install |
修改 Gruntfile.js (约93行) | vi Gruntfile.js |
增加如下内容: | |
hostname: '0.0.0.0', | |
按 ESC 键,输入 :wq 保存退出 | |
修改 _site/app.js (约4329行) | vi _site/app.js |
原有内容 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200"; 改变后 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.8.230:9200"; | |
按 ESC 键,输入 :wq 保存退出 | |
检查head根目录下是否存在base文件夹 | |
没有:将 _site下的base文件夹及其内容复制到head根目录下,复制命令:cp -R _site/base/ ./base/ |
- 创建node_modules/grunt(以root身份执行)
查看当前head插件目录下有无node_modules/grunt目录:
没有则执行如下命令创建:
npm install grunt --save - 安装grunt(以root身份执行)
npm install -g grunt-cli - 启动 elasticsearch-head
cd /opt/elasticsearch-head-master/
grunt server -d
访问地址:http://your'ip:9100/elasticsearch-head
// elasticsearch-head install end & kibana install begin **************************************************************************************************************************************** - 安装kibana(以root身份执行)
拷贝 | kibana-5.5.1-linux-x86_64.tar.gz 至 /opt/ 下 |
切换目录 | cd /opt/ |
解压 | tar -zxvf kibana-5.5.1-linux-x86_64.tar.gz |
授权 | chmod -R esuser+rwx /opt/kibana-5.5.1-linux-x86_64/* |
切换目录 | cd /opt/kibana-5.5.1-linux-x86_64/ |
更改配置 | vi config/kibana.yml |
server.host: "服务器的真实IP" elasticsearch.url: "http://elasticsearchIp:9200" | |
按 ESC 键,输入 :wq 保存退出 | |
启动 | ./bin/kibana |
访问地址 | http://your'ip:5601/kibana |
// kibana install end & X-Pack install begin ****************************************************************************************************************************************
- 安装X-Pack(以root身份执行)
切换目录 | /opt/elasticsearch-5.5.1 |
执行命令 | ./bin/elasticsearch-plugin install x-pack |
切换目录 | cd /opt/kibana-5.5.1-linux-x86_64/ |
执行命令 | ./bin/kibana-plugin install x-pack |
授权 | chmod -R esuser+rwx /opt/elasticsearch-5.5.1/* |
| chmod -R esuser+rwx /opt/kibana-5.5.1-linux-x86_64/* |
说明 X-Pack 经过上面步骤的安装会被集成到elasticsearch和kibana,直接启动elasticsearch和kibana就可以了 |
// X-Pack install end ****************************************************************************************************************************************
// ============================================================================================
// elasticsearch配置文件config/elasticsearch.yml配置项
// ============================================================================================
cluster.name:elasticsearch
配置es的集群名称,默认是elasticsearch,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群。
node.name:"FranzKafka"
节点名,默认随机指定一个name列表中名字,该列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者添加的有趣名字。
node.master:true
指定该节点是否有资格被选举成为node,默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master。
node.data:true
指定该节点是否存储索引数据,默认为true。
#node.max_local_storage_nodes: 1
配置每个几点可以启动的Elasticsearch最大实例个数
index.number_of_shards:5
设置默认索引分片个数,默认为5片。
index.number_of_replicas:1
设置默认索引副本个数,默认为1个副本。
path.conf:/path/to/conf
设置配置文件的存储路径,默认是es根目录下的config文件夹。
path.data:/path/to/data
设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:
path.data:/path/to/data1,/path/to/data2
path.work:/path/to/work
设置临时文件的存储路径,默认是es根目录下的work文件夹。
path.logs:/path/to/logs
设置日志文件的存储路径,默认是es根目录下的logs文件夹
path.plugins:/path/to/plugins
设置插件的存放路径,默认是es根目录下的plugins文件夹
bootstrap.mlockall:true
设置为true来锁住内存。因为当jvm开始swapping时es的效率会降低,所以要保证它不swap,可以把ES_MIN_MEM和ES_MAX_MEM两个环境变量设置成同一个值,并且保证机器有足够的内存分配给es。同时也要允许elasticsearch的进程可以锁住内存,linux下可以通过`ulimit-l unlimited`命令。
network.bind_host:192.168.0.1
设置绑定的ip地址,可以是ipv4或ipv6的,默认为0.0.0.0。
network.publish_host:192.168.0.1
设置其它节点和该节点交互的ip地址,如果不设置它会自动判断,值必须是个真实的ip地址。
network.host:192.168.0.1
这个参数是用来同时设置bind_host和publish_host上面两个参数。
transport.tcp.port:9300
设置节点间交互的tcp端口,默认是9300。
transport.tcp.compress:true
设置是否压缩tcp传输时的数据,默认为false,不压缩。
http.port:9200
设置对外服务的http端口,默认为9200。
http.max_content_length:100mb
设置内容的最大容量,默认100mb
http.enabled:false
是否使用http协议对外提供服务,默认为true,开启。
gateway.type:local
gateway的类型,默认为local即为本地文件系统,可以设置为本地文件系统,分布式文件系统,Hadoop的HDFS,和amazon的s3服务器。
gateway.recover_after_nodes:1
设置集群中N个节点启动时进行数据恢复,默认为1。
gateway.recover_after_time:5m
设置初始化数据恢复进程的超时时间,默认是5分钟。
gateway.expected_nodes:2
设置这个集群中节点的数量,默认为2,一旦这N个节点启动,就会立即进行数据恢复。
cluster.routing.allocation.node_initial_primaries_recoveries:4
初始化数据恢复时,并发恢复线程的个数,默认为4。
cluster.routing.allocation.node_concurrent_recoveries:2
添加删除节点或负载均衡时并发恢复线程的个数,默认为4。
indices.recovery.max_size_per_sec:0
设置数据恢复时限制的带宽,如入100mb,默认为0,即无限制。
indices.recovery.concurrent_streams:5
设置这个参数来限制从其它分片恢复数据时最大同时打开并发流的个数,默认为5。
discovery.zen.minimum_master_nodes:1
设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)
discovery.zen.ping.timeout:3s
设置集群中自动发现其它节点时ping连接超时时间,默认为3秒,对于比较差的网络环境可以高点的值来防止自动发现时出错。
discovery.zen.ping.multicast.enabled:false
设置是否打开多播发现节点,默认是true。
discovery.zen.ping.unicast.hosts:["host1", "host2:port","host3[portX-portY]"]
设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点
http.cors.enabled: false
是否支持跨域,默认为false
http.cors.allow-origin: "*"
当设置允许跨域,默认为*,表示支持所有域名,如果我们只是允许某些网站能访问,那么可以使用正则表达式。比如只允许本地地址。 /https?:\/\/localhost(:[0-9]+)?/
http.cors.max-age: 1728000
浏览器发送一个“预检”OPTIONS请求,以确定CORS设置。最大年龄定义多久的结果应该缓存。默认为1728000(20天)
http.cors.allow-methods: "OPTIONS,HEAD,GET,POST,PUT,DELETE"
允许跨域的请求方式,默认OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers: "X-Requested-With,Content-Type,Content-Length"
跨域允许设置的头信息,默认为X-Requested-With,Content-Type,Content-Length
http.cors.allow-credentials: false
是否返回设置的跨域Access-Control-Allow-Credentials头,如果设置为true,那么会返回
// ============================================================================================
// elasticsearch配置文件config/elasticsearch.yml其他设置
// ============================================================================================
日志参数设置:
index.search.slowlog.level: TRACE
index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms
index.search.slowlog.threshold.fetch.warn: 1s
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug:500ms
index.search.slowlog.threshold.fetch.trace: 200ms1
线程池:
一个Elasticsearch节点会有多个线程池,但重要的是下面四个:
索引(index):主要是索引数据和删除数据操作(默认是cached类型)
搜索(search):主要是获取,统计和搜索操作(默认是cached类型)
批量操作(bulk):主要是对索引的批量操作(默认是cached类型)
更新(refresh):主要是更新操作(默认是cached类型)
可以通过给设置一个参数来改变线程池的类型(type),例如,把索引的线程池改成blocking类型:
min: 1
size: 30
wait_time: 30s
下面是三种可以设置的线程池的类型:
cache
cache线程池是一个无限大小的线程池,如果有很多请求的话都会创建很多线程,下面是个例子:
threadpool:
index:
type: cached
fixed
fixed线程池保持固定个数的线程来处理请求队列。
size参数设置线程的个数,默认设置是cpu核心数的5倍
queue_size可以控制待处理请求队列的大小。默认是设置为-1,意味着无限制。当一个请求到来但队列满了的时候,reject_policy参数可以控制它的行为。默认是abort,会使那个请求失败。设置成caller会使该请求在io线程中执行。
threadpool:
index:
type: fixed
size: 30
queue: 1000
reject_policy: caller
blocking
blocking线程池允许设置一个最小值(min,默认为1)和线程池大小(size,默认为cpu核心数的5倍)。它也有一个等待队列,队列的大小(queue_size )默认是1000,当这队列满了的时候。它会根据定好的等待时间(wait_time,默认是60秒)来调用io线程,如果超时没有执行就会报错。
threadpool:
index:
type: blocking
min: 1
size: 30
wait_time: 30s
// ============================================================================================
// 常见问题及解决办法
// ============================================================================================
1、启动 elasticsearch 如出现异常 can not run elasticsearch as root
解决方法:创建ES 账户,修改文件夹 文件 所属用户 组
2、启动异常:ERROR: bootstrap checks failed
system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
问题原因:因为Centos6不支持SecComp,而ES5.2.1默认bootstrap.system_call_filter为true进行检测,所以导致检测失败,失败后直接导致ES不能启动。详见 :https://github.com/elastic/elasticsearch/issues/22899
解决方法:在elasticsearch.yml中配置bootstrap.system_call_filter为false,注意要在Memory下面:
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
3、启动后,如果只有本地可以访问,尝试修改配置文件 elasticsearch.yml
中network.host(注意配置文件格式不是以 # 开头的要空一格, : 后要空一格)
为 network.host: 0.0.0.0
默认端口是 9200
注意:关闭防火墙 或者开放9200端口
4、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
5、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
6、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
然后,重新启动elasticsearch,即可启动成功。
// ============================================================================================