个人专题目录
快速入门
1. 安装包下载
- Elasticsearch官网: https://www.elastic.co/products/elasticsearch
- Elastic官网:https://www.elastic.co/cn/
2. 安装Elasticsearch(单节点Linux环境)
使用6.5.4,Elasticsearch6.x要求Linux内核必须是3.5+版本以上。
在linux操作系统中,查看内核版本的命令是: uname -a
Elasticsearch在Linux中安装部署的时候,需要系统为其提供若干系统配置。如:应用可启动的线程数、应用可以在系统中划分的虚拟内存、应用可以最多创建多少文件等。
Elasticsearch是java开发的应用。在6.8.4版本中,要求JDK至少是1.8.0_131版本以上。
出于安全考虑,elasticsearch默认不允许以root账号运行。
- 解压elasticsearch-6.5.4.tar.gz到/opt/module目录下
$ tar -zxvf elasticsearch-6.5.4.tar.gz -C /opt/module/
- 在/opt/module/elasticsearch-6.5.4路径下创建data和logs文件夹
$ mkdir data
$ mkdir logs
#因为安全问题,Elasticsearch 不允许root用户直接运行,所以要创建新用户,在root用户中创建新用户,执行如下命令
useradd elasticsearch # 新增elasticsearch用户
passwd elasticsearch # 为elasticsearch用户设置密码
chown -R elasticsearch:elasticsearch /opt/module/ #文件夹所有者
- 修改配置文件/opt/module/elasticsearch-6.5.4/config/elasticsearch.yml
$ pwd
/opt/module/elasticsearch-6.5.4/config
vim jvm.options
-Xms1g
-Xmx1g
$ vi elasticsearch.yml
#配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称
# ---------------------------------- Cluster -----------------------------------
cluster.name: my-application
# ------------------------------------ Node ------------------------------------
#节点名,elasticsearch会默认随机指定一个名字,建议指定一个有意义的名称,方便管理
node.name: node-102
# ----------------------------------- Paths ------------------------------------
path.data: /opt/module/elasticsearch-6.5.4/data
path.logs: /opt/module/elasticsearch-6.5.4/logs
# ----------------------------------- Memory -----------------------------------
# 设置为true可以锁住ES使用的内存,避免内存与swap分区交换数据。
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
# ---------------------------------- Network -----------------------------------
#设置为0.0.0.0允许外网访问
network.host: 192.168.1.102
# --------------------------------- Discovery ----------------------------------
discovery.zen.ping.unicast.hosts: ["hadoop102"]
#Elasticsearch的http访问端口
http.port: 9200
#初始化新的集群时需要此配置来选举master
cluster.initial_master_nodes: ["node-1"]
(1)cluster.name
如果要配置集群需要两个节点上的elasticsearch配置的cluster.name相同,都启动可以自动组成集群,这里如果不改cluster.name则默认是cluster.name=my-application,
(2)nodename随意取但是集群内的各节点不能相同
(3)修改后的每行前面不能有空格,修改后的“:”后面必须有一个空格
- 配置linux系统环境(参考:http://blog.csdn.net/satiling/article/details/59697916)
(1)切换到root用户,编辑limits.conf 添加类似如下内容,是修改系统中允许应用最多创建多少文件等的限制权限。Linux默认来说,一般限制应用最多创建的文件是65535个。但是Elasticsearch至少需要65536的文件创建权限。
# vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
*代表任意用户,soft表示内存中虚拟文件(软文件),hard表示落地到磁盘的具体文件(硬文件), nofile表示权限,65536表示个数。
(2)切换到root用户,进入limits.d目录下修改配置文件。
在CentOS6.5版本中编辑下述的配置文件
vim /etc/security/limits.d/90-nproc.conf
在CentOS7+版本中编辑配置文件是:
vim /etc/security/limits.conf
是修改系统中允许用户启动的进程开启多少个线程。默认的Linux限制root用户开启的进程可以开启任意数量的线程,其他用户开启的进程可以开启1024个线程。必须修改限制数为4096+。因为Elasticsearch至少需要4096的线程池预备。Elasticsearch在5.x版本之后,强制要求在linux中不能使用root用户启动Elasticsearch进程。所以必须使用其他用户启动Elasticsearch进程才可以。
# vim /etc/security/limits.d/90-nproc.conf
修改如下内容:
* soft nproc 1024
#修改为
* soft nproc 4096
*任何用户 nproc创建线程 数量4096
Linux低版本内核为线程分配的内存是128K。4.x版本的内核分配的内存更大。如果虚拟机的内存是1G,最多只能开启3000+个线程数。至少为虚拟机分配1.5G以上的内存,保险起见建议2G以上。
(3)切换到root用户修改配置sysctl.conf
系统控制文件是管理系统中的各种资源控制的配置文件。Elasticsearch需要开辟一个65536字节以上空间的虚拟内存。Linux默认不允许任何用户和应用直接开辟虚拟内存。
# vim /etc/sysctl.conf
添加下面配置:最大虚拟内存太小,限制一个进程可以拥有的VMA(虚拟内存区域)的数量
vm.max_map_count=655360
并执行命令:
# sysctl -p
然后,重新启动elasticsearch,即可启动成功。
- 启动集群
$ bin/elasticsearch
- 测试集群
$ curl http://localhost:9200
{
"name" : "L6WdN7y",
"cluster_name" : "Elasticsearch",
"cluster_uuid" : "s7_GSd9YQnaH10VQBKCQ5w",
"version" : {
"number" : "6.3.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "eb782d0",
"build_date" : "2018-06-29T21:59:26.107521Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
curl http://localhost:9200/_cat/nodes 获取节点信息
curl http://localhost:9200/_cat/shards 获取分片信息
curl http://localhost:9200/_cat/indices 获取索引信息
curl http://localhost:9200/_cat/health?v 获取健康状态信息
$ curl http://localhost:9200/_cluster/health
查询集群状态
{
"cluster_name": "elasticsearch",
"status": "green",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
"active_primary_shards": 0,
"active_shards": 0,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 0,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 100
}
Status:集群状态。Green 所有分片可用。Yellow所有主分片可用。Red主分片不可用,集群不可用。
- 停止集群
kill -9 进程号
3. 安装Elasticsearch(多节点集群Linux环境)
4. Elasticsearch head插件安装
- 下载插件
https://github.com/mobz/elasticsearch-head
elasticsearch-head-master.zip
- nodejs官网下载安装包
node-v6.9.2-linux-x64.tar.xz
将elasticsearch-head-master.zip和node-v6.9.2-linux-x64.tar.xz都导入到linux的/opt/software目录。
安装nodejs
$ tar -zxvf node-v6.9.2-linux-x64.tar.gz -C /opt/module/
- 配置nodejs环境变量
# vi /etc/profile
export NODE_HOME=/opt/module/node-v6.9.2-linux-x64
export PATH=$PATH:$NODE_HOME/bin
# source /etc/profile
- 查看node和npm版本
# node -v
v6.9.2
# npm -v
3.10.9
- 解压head插件到/opt/module目录下
$ unzip elasticsearch-head-master.zip -d /opt/module/
- 查看当前head插件目录下有无node_modules/grunt目录:
没有:执行命令创建:
$ npm install grunt --save
- 安装head插件:
]$ npm install -g cnpm --registry=https://registry.npm.taobao.org
- 安装grunt:
$ npm install -g grunt-cli
- 编辑Gruntfile.js
$ vim Gruntfile.js
文件93行添加hostname:'0.0.0.0'
options: {
hostname:'0.0.0.0',
port: 9100,
base: '.',
keepalive: true
}
- 检查head根目录下是否存在base文件夹
没有:将 _site下的base文件夹及其内容复制到head根目录下
$ mkdir base
$ cp base/* ../base/
- 启动grunt server:
$ grunt server -d
Running "connect:server" (connect) task
[D] Task source: /opt/module/elasticsearch-head-master/node_modules/grunt-contrib-connect/tasks/connect.js
Waiting forever...
Started connect web server on http://localhost:9100
如果提示grunt的模块没有安装:
Local Npm module “grunt-contrib-clean” not found. Is it installed?
Local Npm module “grunt-contrib-concat” not found. Is it installed?
Local Npm module “grunt-contrib-watch” not found. Is it installed?
Local Npm module “grunt-contrib-connect” not found. Is it installed?
Local Npm module “grunt-contrib-copy” not found. Is it installed?
Local Npm module “grunt-contrib-jasmine” not found. Is it installed?
Warning: Task “connect:server” not found. Use –force to continue.
执行以下命令:
npm install grunt-contrib-clean -registry=https://registry.npm.taobao.org
npm install grunt-contrib-concat -registry=https://registry.npm.taobao.org
npm install grunt-contrib-watch -registry=https://registry.npm.taobao.org
npm install grunt-contrib-connect -registry=https://registry.npm.taobao.org
npm install grunt-contrib-copy -registry=https://registry.npm.taobao.org
npm install grunt-contrib-jasmine -registry=https://registry.npm.taobao.org
最后一个模块可能安装不成功,但是不影响使用。
- 浏览器访问head插件:
http://hadoop102:9100
- 启动集群插件后发现集群未连接
在/opt/module/elasticsearch-5.2.2/config路径下修改配置文件elasticsearch.yml,在文件末尾增加
$ pwd
/opt/module/elasticsearch-5.2.2/config
$ vi elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
再重新启动elasticsearch。
- 关闭插件服务
ctrl+c
$ netstat -lntp | grep 9100
tcp 0 0 192.168.1.102:9100 0.0.0.0:* LISTEN