1. elasticsearch
elasticsearch 是一个分布式扩展的实时搜索和分析引擎,一个建立在全文索引引擎Apache Lucene基础上的搜索引擎,它不仅可以进行全文搜索,还可以进行一下工作
- 分布式实时文件存储,并将每一个字段都编入索引,使其可以被索引
- 实时分析的分布式搜索引擎
- 可以扩展到上百台服务器,处理PB级别的结构化或者非及结构化数据
- 提供了 REST API 操作接口,开箱即用
2. 存储单元
1KB = 1024B
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
1PB = 1024TB
1EB = 1024PB
1ZB = 1024EB
1YB = 1024ZB
3. ElastcSearch 核心概念
ElastcSearch 是一个面向文档数据库,一条数据在这里称为一个文件
1)索引 (index)
索引是具有某种相似特征的文档集合。例如: 你可以有一个顾客数据索引,产品目录索引和订单数据索引,索引有一个名称标识(必须全部为小写), 该名称用于文档执行索引、搜索、更新和删除操作时引用索引,在单个集群中,尽量定义多个索引。
2)文档( Document)
ElasticSearch 文档是一个存储索引中的JSON 文档,每个文档都有一个类型和对应的ID,这个ID 是唯一的。
例如:
{ "_index" : "packtpub", "_type" : "elk", "_id" : "1", "_version" : 1, "found" : true, "_source":{ book_name : "learning elk", book_author:"鲁迅" } }
3) 字段 ( Field)
文档内的一个基本单元,键值对形式(book_name:"learning elk“)
4) 类型(Type)
类型是 index 下的一个逻辑分类,例如: web这个 index当中,可以按照城市分组,也可以按照气候分组,这种分组就叫做类型。
5) 映射 (Mapping)
映射用于映射文档的每个 字段(field) 以及其对应的数据类型, 例如: 字符串、整数、浮点数、双精度数、日期等等
6) 分片(Shard)
单台服务器无法存储大量的数据,ES可以将一个索引中的数据且分为多个 shard,分布在多台服务器上存储,有了 shard 可以横向扩展,存储跟多的数据,让搜索和分析等操作分不到多台服务器上执行,提升吞吐量
- 例如:
通过上图我们,可以简单理解一下,将 index 这个索引,进行切片(shrad),分别存储在三台服务器中。
7) 分词
将一段分文中的词按一定的规则进行分割
简单的来讲,类似于 百度搜索,根据你输入的内容,进行分割,然后按分割的内容进行匹配
8) 主分片 (Primary shard) 与复制分片(replica shard)
主分片 切割后的分片,
复制分片,类似于备份分片,通畅在一个不同的节点上,在故障转移和负载均衡的情况下,可以满足多个请求。
例如:
通过以上来看,我们可以看到,将 index 一个索引的内容,进行分片,分别存储在不同的节点中,为了方式某个节点出现故障导致数据丢失,所以有了复制分片,通过主节点,将各个分片的内容备份在不同的节点中,从而保证了数据安全。
9) 集群 (Cluster)
集群是存储索引数据的节点集合,他们共同存储了整个数据,并提供跨所有节点的联合索引和搜索功能。ElasticSearch 提供了水平的可伸缩性用以存储集群的数据,每个集群都由一个唯一的名称标识来表示,默认这个唯一标识名称为 ” elasticsearch“ ,不同节点指定集群名称就可以连接在一起。
10) 节点(Node)
节点是一个单独运行的elasticsearch 实例,它属于一个集群, 默认情况下,elasticsearch 中的每个节点都加入名为 “elasticsearch” 的集群,每个节点都可以在 elasticesearch 中使用自己的 ElastcSearch.yml ,他们可以对内存和资源分配有不同的设置。
3. ES集群分类
1) 数据节点 (Data Node)
数据节点索引文档并对索引文档执行所搜索,建议添加更多的数据节点,以提高性能或者扩展集群。通过在 ElasticSearch 中设置这些属性,可以使节点称为一个数据节点。Elasticesarch .yml 配置
- node.master: false 表示是参与 master(主节点)选举
- node.data: true 表示是否为数据节点
2) 管理节点(Master Node)
主节点负责集群的管理,对于大型集群,建议由三个专用的主节点(一个主节点和两个备份节点),他们只作为主节点,不存储索引或者执行搜索。在ElasticSearch.yml 配置声明节点为主节点:
- node.master: true
- node.data: false
3) 路由节点 也称 负载均衡节点(Routing Node or load balance人 node)
这些节点不扮演主或者数据节点的角色,但只需要执行负载均衡,或者为搜索请求路由,或将文档编入适当的节点。者对高容量搜索或索引操作非常有用。
- node.master: false
- node.data: false
4) 提取节点 (Ingest 节点)
可以创建多个预处理管道,用于修改传入文档
4. Zendiscovery 通信
默认 ES 进程会绑定在自己的回环地址上,然后扫描本机的 9300-9305 号端口,尝试跟其他端口上的启动的ES进程进行通信,然后自动形成了一个集群。 如果修改了监听地址为 非回环地址,ES 按照配置文件里指定的地址或者自动扫描当前网段其他节点,自动跟其他节点上的 ES Node 进行通信。
5. Master 选举
首选ping所有的节点,过滤出有 master 选举资格的节点
- 如果当前没有 master 主节点,则从 活动的节点(具有选举资格,且在线的节点) 进行选举, 参与者达到一定数量时,得到临时的 Master(相当于实习期),然后对其进行判断是否为 本地节点,
- 如果是则进行投票,然后判断是否超时,最后成功当选 master
- 如果当前有Master 。 则通过当前 Master 得到临时Master
- 如果不是本地节点,就将其加入到 集群
6. SlasticSearch 脑裂
因为网络或者其他故障,导致一个集群被划分为 两伙儿或者多方势力,这些群伙都有多个 node 以及一个 master ,那么原来的集群就出现了多个 master, master 住在了集群状态 的维护以及 shard(分片) 的分配,如果有多个master,可能会导致数据被破坏。
7. 容错机制
1) 集群状态
状态 | 意义 |
---|---|
green | 所有的主分片和从分片都可用 |
yellow | 所有主分片可用,但存在不可用的从分片 |
red | 存在不可用的主分片 |
2) 宕机瞬间
master node (主节点) 宕机的一瞬间,该节点的primary shard (主分片) 就没有了,此时状态就不是 active status, 那么集群中就不是所有的主分片都是 active的了。
简单的说,就是 主节点在宕机的一瞬间,数据就已经没有,就算再次加入到集群只是一个具有选取资格的节点,没有任何数据
3) 容错步骤一
master 选举,ES 自动选举另一个 mode称为了 master ,承担起master 的责任
4) 容错步骤二
新 master 将丢失掉的主分片的某个复制分片提升为主分片,此时 Cluster Status 会变成 yellow,因为所有的主分片都变成了 active status 了,但是会少了一个分片
5) 容错步骤三
master 将缺失的复制分片复制到某节点上,此时所有的主分片和复制分片都可以用,集群状态变为了 green, 将修复好的 宕机节点重新加入集群,供 master支配使用
ES 是基于内存的工作的 , 数据可以持久化
8. 部署 ES 集群
1) 同步所有终端时间
[root@localhost ~]# ntpdate ntp.ntsc.ac.cn
2) 关掉防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
3) 修改参数
- 修改文件同时打开数量
# ulimit -HSn 数量 为命令修改,系统文件描述符,默认为 1024(临时)
# 配置文件需重启生效
[root@localhost ~]# echo "* soft nofile 655360" >> /etc/security/limits.conf
# 设置软限制 数量
[root@localhost ~]# echo "* hard nofile 655360" >> /etc/security/limits.conf
# 设置硬
-
调整用户可用进程数
[root@localhost ~]# echo "* soft nproc 2048" >> /etc/security/limits.conf [root@localhost ~]# echo "* hard nproc 4096" >> /etc/security/limits.conf
-
JVM 能使用的最大线程数
[root@localhost ~]# echo "vm.max_map_count=655360" >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
vm.max_map_count = 65536
4) 配置JAVA环境变量
5) 安装 ES
-
es 不可以使用 root 用户启动,否则会报错
-
创建 es 运行用户
[root@localhost ~]# useradd es
- 创建数据、日志存放目录
[root@localhost ~]# mkdir -p /es/{data,logs}
- 解压安装 es
[root@localhost ~]# tar -zxf elasticsearch-6.3.2.tar_\(1\).gz
[root@localhost ~]# mv elasticsearch-6.3.2 /usr/local/es
[root@localhost ~]# mkdir /usr/local/es/plugins/ik
- 更改属主属组
[root@localhost ~]# chown -R es:es /usr/local/es/
6) 安装 elasticsearch-analysis
- 安装该组件 elasticsearch+analysis 实现全文索引
[root@localhost ~]# unzip elasticsearch-analysis-ik-6.3.2.zip -d /usr/local/es/plugins/ik/
- 修改配置文件(node-1)
----------------------------------- Cluster ------------------------------------
cluster.name: my-application # 集群名称
----------------------------------- Node ------------------------------------
node.name: node-1 # 节点名称
node.data: true # 添加 为数据节点
node.master: true # 添加 具有 master主节点 选举资格
PS: 一般来说,要么是数据节点,要么是管理节点,很少兼职两个节点。
----------------------------------- Paths ------------------------------------
path.data: /es/data # 指定存放数据的目录
path.logs: /es/logs # 指定存放日志的目录
---------------------------------- Network -----------------------------------
network.host: 192.168.116.103 # 指定本地IP地址
http.port: 9200 # 解开注释,默认的监听端口
transport.tcp.port: 9300 # 通信端口 添加
--------------------------------- Discovery ----------------------------------
discovery.zen.ping.unicast.hosts: ["192.168.116.103:9300", "192.168.116.104:9300","192.168.116.105:9300"] # 解开注释,指定集群节点的位置
discovery.zen.minimum_master_nodes: 2 # 指定有几个 master 选举资格的才能选举
discovery.zen.ping_timeout: 120s # 集群通信超时时间,防止超时后形成脑裂,可预防网络波动
---------------------------------- Gateway -----------------------------------
# 集群重启后恢复数据的参数
gateway.recover_after_nodes: 3 # 集群重启后,有几个节点开始恢复数据
gateway.recover_after_time: 5m # 节点等待时间, 分钟
gateway.recover_after_data_nodes: 2 # 等待时间过后,有几个数据节点也开始恢复
- 将配置文件进行传输
- 因三者配置文件,相差不大,只需要更改 节点名称、本地IP地址即可
[root@localhost ~]# scp /usr/local/es/config/elasticsearch.yml root@192.168.116.104:/usr/local/es/config/
root@192.168.116.104's password:
elasticsearch.yml 100% 3052 2.7MB/s 00:00
[root@localhost ~]# scp /usr/local/es/config/elasticsearch.yml root@192.168.116.105:/usr/local/es/config/
root@192.168.116.105's password:
elasticsearch.yml 100% 3052 2.4MB/s 00:00
- 修改配置 node-2
[root@localhost ~]# vim /usr/local/es/config/elasticsearch.yml
node.name: node-2 # 修改节点 名称
network.host: 192.168.116.104 # 修改本地监听地址
- 修改配置文件 node-3
[root@localhost ~]# vim /usr/local/es/config/elasticsearch.yml
node.name: node-3
network.host: 192.168.116.105
7) 启动服务(all)
- 赋予 /es 目录 es 的属主,属组,用于生成数据文件与日志文件
[root@localhost ~]# chown -R es:es /es
#一定要修改属主属组,否则没法生成 数据与日志文件,导致报错
- 启动服务
[root@localhost ~]# su es # 切换至 es 用户
[es@localhost root]$ /usr/local/es/bin/elasticsearch
# 可以输出当前结果查看日志信息异常信息
# 如果出现报错,一般来说配置文件,另外检查 /es/ 是否更改属主属组
8) 命令查看集群信息
- 查看集群节点状态
[root@localhost ~]# curl http://192.168.116.103:9200/_cluster/health?pretty
{
"cluster_name" : "my-application",
"status" : "red",
"timed_out" : false,
"number_of_nodes" : 2, # 节点数量
"number_of_data_nodes" : 2,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 1,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : "NaN"
}
# 关于 red 状态, red --> yellow --> green 稍等一会儿就变为了 green
[root@localhost ~]# curl http://192.168.116.103:9200/_cluster/health?pretty
{
"cluster_name" : "my-application",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"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.0
- 查看自身节点信息
- 格式: http://ip:9200
[root@localhost ~]# curl http://192.168.116.103:9200
{
"name" : "node-1",
"cluster_name" : "my-application",
"cluster_uuid" : "IRk5iaqkQ2C7emQmmUW5Fw",
"version" : {
"number" : "6.3.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "053779d",
"build_date" : "2018-07-20T05:20:23.451332Z",
"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"
}
网页请求方式默认是 GET
curl -X 指定请求方法, 不加 -X 为 GET
- 查看集群内所有节点
- 带有 小星星的是 master
[root@localhost ~]# curl -XGET http://192.168.116.103:9200/_cat/nodes
192.168.116.103 9 44 0 0.00 0.01 0.05 mdi - node-1
192.168.116.105 13 60 1 0.00 0.01 0.05 mdi - node-3
192.168.116.104 16 45 1 0.00 0.02 0.05 mdi * node-2
- 查看集群内 master 节点
[root@localhost ~]# curl -XGET http://192.168.116.103:9200/_cat/master
4DiWXEAyQFe5AJ0kxzD_Kw 192.168.116.104 192.168.116.104 node-2
9) 使用分词器
分词 --》 数据 --》 索引 --》 类型 --》 映射
- 创建索引
[root@localhost ~]# curl -XPUT http://192.168.116.103:9200/shipin
{"acknowledged":true,"shards_acknowledged":true,"index":"shipin"}
- 指定类型并添加映射
[root@localhost ~]# curl -XPOST -H 'Content-Type: application/json' http://192.168.116.103:9200/shipin/shuiguo/_mapping -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}'
{"acknowledged":true}
- 添加数据
[root@localhost ~]# curl -XPOST -H 'Content-Type: application/json' http://192.168.116.103:9200/shipin/shuiguo/1 -d'{"content": "水果很好吃也"}'
{"_index":"shipin","_type":"shuiguo","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1}
- 使用分词器进行简单的分词操作
[root@localhost ~]# curl -XPOST -H 'Content-Type: application/json' http://192.168.116.103:9200/shipin/shuiguo/_search -d'
{
"query": {"match": {"content": "好吃"}},
"highlight": {
"pre_tags": ["<tag1>","<tag2>"],
"post_tags": ["</tag1>","</tag2>"],
"fields": {
"content": {}
}
}
}'
{"took":325,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"shipin","_type":"shuiguo","_id":"1","_score":0.2876821,"_source":{"content": "水果很好吃也"},"highlight":{"content":["水果很<tag1>好吃</tag1>也"]}}]}}
9. ES 命令
ES 命令通过 curl 实现,并通过 请求动作实现具体的操作,
例如: GET 请求, POST 提交、DELETE 产出……………
curl 命令通过 -X 指定请求动作
1) 查看所有可查看项
-
格式
curl http://ip:9200/_cat
-
例:
[root@localhost ~]# curl http://192.168.116.103:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
2) 显示详细信息 (?v)
-
格式
curl http://ip:9200/_cat/master?v
-
例:
[root@localhost ~]# curl http://192.168.116.103:9200/_cat/master?v
id host ip node
4DiWXEAyQFe5AJ0kxzD_Kw 192.168.116.104 192.168.116.104 node-2
或者:
[root@localhost ~]# curl http://192.168.116.103:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.116.103 14 45 3 0.00 0.07 0.11 mdi - node-1
192.168.116.104 14 44 3 0.00 0.09 0.13 mdi * node-2
192.168.116.105 14 59 2 0.00 0.11 0.15 mdi - node-3
3) 输出可以显示的列 (?help)
-
格式
curl http://ip:9200/_cat/master?help
-
例:
[root@localhost ~]# curl http://192.168.116.103:9200/_cat/master?help
id | | node id
host | h | host name
ip | | ip address
node | n | node name
4) 指定输出的那个列 (?h)
-
格式
curl http://ip:9200/_cat/master?h
[root@localhost ~]# curl http://192.168.116.103:9200/_cat/master?h=ip,name
192.168.116.104
5) 查看所以的索引
- curl http://ip:9200/_cat/indices?v
[root@localhost ~]# curl http://192.168.116.103:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open shipin dm6SDo-XQHSt4bxszgYOkQ 5 1 1 0 8.2kb 4.1kb
6) 创建索引
-
格式:
curl -XPUT http://ip:9200/index_name?pretty
-
索引名: kgctest1
[root@localhost ~]# curl -XPUT http://192.168.116.103:9200/kgctest1?pretty
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "kgctest1"
}
7) 关闭索引
- curl -XPOST http://ip:9200/index_name/_close?pretty
[root@localhost ~]# curl -XPOST http://192.168.116.103:9200/kgctest1/_close?pretty
{
"acknowledged" : true
}
8) 开启索引
[root@localhost ~]# curl -XPOST http://192.168.116.103:9200/kgctest1/_open?pretty
{
"acknowledged" : true,
"shards_acknowledged" : true
}
9) 删除索引
[root@localhost ~]# curl -XDELETE http://192.168.116.103:9200/kgctest1?pretty
{
"acknowledged" : true
}
10) 插入数据
[root@localhost ~]# curl -XPUT http://192.168.116.103:9200/kgctest/fulltext/1?pretty -H 'Content-Type: application/json' -d'
> {
> "name": "tiechui"
> }'
{
"_index" : "kgctest",
"_type" : "fulltext",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
11) 取出 fulltext 类型的 id 为1 的数据
[root@localhost ~]# curl -XGET 'http://192.168.116.103:9200/kgctest/fulltext/1?pretty'
{
"_index" : "kgctest",
"_type" : "fulltext",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "tiechui"
}
}
12) 更新文档
[root@localhost ~]# curl -XPOST 'http://192.168.116.103:9200/kgctest/fulltext/1/_update?pretty' -H 'Content-Type: application/json' -d'
> {
> "doc": {"name": "Tianci"}
> }'
{
"_index" : "kgctest",
"_type" : "fulltext",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
13) 删除文档
[root@localhost ~]# curl -XDELETE 'http://192.168.116.103:9200/kgctest/fulltext/1?pretty'
{
"_index" : "kgctest",
"_type" : "fulltext",
"_id" : "1",
"_version" : 3,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
14) 查询所有记录
[root@localhost ~]# curl -XPOST 'http://192.168.116.103:9200/kgctest/fulltext/_search?pretty' -H 'Content-Type: application/json' -d'
> {
> "query": {"match_all":{}}
> }'
{
"took" : 127,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
15) 查询符合条件的记录
[root@localhost ~]# curl -XPOST 'http://192.168.116.103:9200/kgctest/fulltext/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {"match": {"name": "Tianci"}}
}'
{
"took" : 141,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
16) 清空内存中的缓存 clear
[root@localhost ~]# curl -XPOST 'http://192.168.116.103:9200/kgctest/_cache/clear?pretty' -H 'Content-Type: application/json'
{
"_shards" : {
"total" : 10,
"successful" : 10,
"failed" : 0
}
}
17) flush 和 refresh (强制刷新数据到磁盘)
- flush
[root@localhost ~]# curl -XPOST 'http://192.168.116.103:9200/kgctest/_flush?pretty' -H 'Content-Type:application/json'
{
"_shards" : {
"total" : 10,
"successful" : 10,
"failed" : 0
}
}
- refresh
[root@localhost ~]# curl -XPOST 'http://192.168.116.103:9200/kgctest/_refresh?pretty' -H 'Content-Type: application/json'
{
"_shards" : {
"total" : 10,
"successful" : 10,
"failed" : 0
}
}
-
refresh 与 flush 区别:
当一个文档进入 ES的初期,文档是被存储在内存中,默认经过 1s 之后,会被写入到文件系统缓存,这样该文档就可以被搜索到了,注意,此时该索引数据没有最终写入到磁盘上,如果你对这1 s的时间间隔还不满意的话,调用_refresh, 就可以立即实现内存> 文件系统缓存, 从而使文档可以立即被搜索到,ES为了数据的安全,在就接受写入的文档时候,在写入内存buffer 的同时,会写一份 translog日志,从而在出现程序故障、磁盘异常时,保证数据的完整和安全, flush 会触发 Lucene commit ,并清空translog 日志文件,transllog 的 flush 是 ES 在后台自动执行的,默认情况下 ES 每隔 5s 会去 检查要不要 flush, 默认 条件是: 每30分钟主动进行一次 flush,或者当 translog 文件大小大于 512M主动进行一次 flush
- 将 ES集群停止,在进行以下操作
1) 部署 node 环境
-
部署 node 语言环境,head 依赖于head 环境
-
通过 npm 命令下载 cnpm 、grunt命令
- cnpm 对 head 插件的源代码进行编译安装
- grunt 作为head 插件的启动工具
-
部署node环境
- 解压后,不需要编译,将其移动至指定的目录,将其bin下的目录,写入系统变量中。
[root@localhost ~]# tar -zxf node-v10.6.0-linux-x64.tar.gz
[root@localhost ~]# mv node-v10.6.0-linux-x64 /usr/local/node
[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/node/bin' >> /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# node -v
v10.6.0
[root@localhost ~]# npm -v
6.1.0
2) 安装 es-head 插件
[root@localhost ~]# unzip elasticsearch-head-master.zip
[root@localhost ~]# mv elasticsearch-head-master /usr/local/head
[root@localhost ~]# cd /usr/local/head/
- 通过 npm 命令安装 cnmp命令
[root@localhost head]# pwd
/usr/local/head
[root@localhost head]# npm install -g cnpm --registry=https://registry.npm.taobao.org
# --registry= 通过国内资源进行下载,安装,默认使用国外资源
- 通过 npm 命令安装 grunt
[root@localhost head]# pwd
/usr/local/head
[root@localhost head]# npm install -g grunt --registry=https://registry.npm.taobao.org
- 对 head 插件 编译安装
[root@localhost head]# pwd
/usr/local/head
[root@localhost head]# cnpm install
- 修改 head 插件所使用对象为所有
[root@localhost head]# pwd
/usr/local/head
[root@localhost head]# vim Gruntfile.js
server: {
options: {
hostname: '0.0.0.0', # 92, 添加,允许所有人使用 head 插件
port: 9100,
base: '.',
keepalive: true
- 修改es 配置文件
[root@localhost head]# vim /usr/local/es/config/elasticsearch.yml
# 在末尾添加一下两行
http.cors.enabled: true
http.cors.allow-origin: "*"
选项解释:
http.cors.enabled: true # 运行跨域访问
http.cors.allow-origin: "*" # 允许跨域访问的ip
- 启动head 的插件
[root@localhost head]# pwd
/usr/local/head
[root@localhost head]# grunt server
# 启动后会进入阻塞,并提示访问 http://localhost:9200
- 再次启动ES 集群
[root@localhost ~]# su es
[es@localhost root]$ /usr/local/es/bin/elasticsearch
访问 head 插件
http://192.168.116.103:9100/
粗线矿: 为 主分片
细现况: 为 从分片