es6.4集群安装 head插件

基本概念

Elasticsearch 是一个Near Realtime近实时的搜索平台。这意味着从您索引一个文档开始直到它可以被查询时会有轻微的延迟时间(通常为一秒)。

Cluster(集群)

Elasticsearch 启动时会根据配置文件中设置的集群名字(cluster.name)自动查找并加入集群。Elasctisearch 节点默认使用 9300 端口寻找集群,所以必须开启这个端口。

Node(节点)

一个 Elasticsearch 集群中一般拥有三种角色的节点,master、data 和 client。

master:master 节点负责一些轻量级的集群操作,比如创建、删除数据索引、跟踪记录集群中节点的状态、决定数据分片(shards)在 data 节点之间的分布;
data:data 节点上保存了数据分片。它负责数据相关操作,比如分片的 CRUD,以及搜索和整合操作。这些操作都比较消耗 CPU、内存和 I/O 资源;
client:client 节点起到路由请求的作用,实际上可以看做负载均衡器。

配置文件中有两个与集群相关的配置:

node.master:默认 true。True 表示该节点是 master 节点;

注意:此属性的值为true,并不意味着这个节点就是主节点。
因为真正的主节点,是由多个具有主节点资格的节点进行选举产生的。
所以,这个属性只是代表这个节点是不是具有主节点选举资格。

node.data:默认 true。True 表示该节点时 data 节点。
如果两个值都为 false,表示是 client 节点。

一个集群中不一定有 client 节点,但是肯定有 master 和 data 节点。默认第一个启动的节点是 master。Master 节点也能起到路由请求和搜索结果整合的作用,所以在小规模的集群中,无需 client 节点。但是如果集群规模很大,则有必要设置专门的 client。

主节点+数据节点(默认)
node.master: true
node.data: true
节点即有成为主节点的资格,又存储数据。这个时候如果某个节点被选举成为了真正的主节点,那么他还要存储数据,这样对于这个节点的压力就比较大了。Elasticsearch 默认每个节点都是这样的配置,在测试环境下这样做没问题。实际工作中建议不要这样设置,这样相当于主节点和数据节点的角色混合到一块了。

数据节点
node.master: false
node.data: true
节点没有成为主节点的资格,不参与选举,只会存储数据。在集群中需要单独设置几个这样的节点负责存储数据,后期提供存储和查询服务。主要消耗磁盘,内存。

主节点
node.master: true
node.data: false
不会存储数据,有成为主节点的资格,可以参与选举,有可能成为真正的主节点。普通服务器即可(CPU、内存消耗一般)。

客户端节点
node.master: false
node.data: false
不会成为主节点,也不会存储数据,主要是针对海量请求的时候可以进行负载均衡。普通服务器即可(如果要进行分组聚合操作的话,建议这个节点内存也分配多一点)

在生产环境下,如果不修改 Elasticsearch 节点的角色信息,在高数据量,高并发的场景下集群容易出现脑裂等问题。

Index(索引)

index(索引)是具有稍微类似特征文档的集合。例如,您有一个消费者数据的索引,一个产品目录的索引,和另一个是订单数据的索引。一个索引通过名字(必须全部是小写)来标识,并且该名字在对 document(文档)执行 indexing(索引),search(搜索),update(更新)和 delete(删除)操作时会涉及到。

一个集群可以定义多个索引。

Type(类型)

在 Index(索引)中,可以定义一个或多个类型。一个类型是索引中一个逻辑的种类/分区,它的语义完全取决于您自己。一般情况下,一个类型被定义成一组常见字段的文档。例如,假设您运行着一个博客平台并且在一个单独的索引中存储了所有的数据。在这个索引中,您也许定义了一个用户数据类型,博客数据类型,和评论数据类型。

Document(文档)

document(文档)是索引信息的基本单位。例如,您有一存储 customer(客户)数据的文档,另一个是存储 product(产品)数据的文档,还有一个是存储 order(订单)数据的文档。该文档可以使用 JSON 来表示,它是一种无处不在的互联网数据交换格式。

在索引/类型中,您可以存储许多文档。注意,尽管一个文档物理的存在于索引中,实际上一个文档必须被 索引/分配 给索引内的类型。

Shards & Replicas(分片 & 副本)

索引可以存储大量数据,可以超过单个节点的硬件限制。例如,十亿个文档占用了 1TB 的磁盘空间的单个索引可能不适合放在单个节点的磁盘上,并且从单个节点服务请求会变得很慢。

为了解决这个问题,Elasticsearch 提供了把 Index(索引)拆分到多个 Shard(分片)中的能力。在创建索引时,您可以简单的定义 Shard(分片)的数量。每个 Shard 本身就是一个 fully-functional(全功能的)和独立的 “Index(索引)”,(Shard)它可以存储在集群中的任何节点上。

Sharding(分片)非常重要两个理由是 :

  • 水平的拆分/扩展。
  • 分布式和并行跨 Shard 操作(可能在多个节点),从而提高了性能/吞吐量。

Shard 的分布式机制以及它的文档是如何聚合支持搜索请求是完全由 Elasticsearch 管理的,并且是对用户透明的。

在 网络/云 环境中可能随时会故障,无论出于何种原因,在 shard/node 不知何故会离线或者消失的情况下强烈建议设置故障转移是非常有效的。为了达到这个目的,Elasticsearch 可以让您设置一个或多个索引的 Shard 副本到所谓的副本分片,或者副本中去。

副本非常重要的两个理由是 :

  • 在 shard/node 故障的情况下提供了高可用性。为了达到这个目的,需要注意的是在原始的/主 Shard 被复制时副本的 Shard 不会被分配到相同的节点上。
  • 它可以让你水平扩展搜索量/吞吐量,因为搜索可以在所有的副本上并行执行。

总而言之,每个索引可以被拆分成多个分片,一个索引可以设置 0 个(没有副本)或多个副本。开启副本后,每个索引将有主分片(被复制的原始分片)和副本分片(主分片的副本)。分片和副本的数量在索引被创建时都能够被指定。在创建索引后,您也可以在任何时候动态的改变副本的数量,但是不能够改变分片数量。

默认情况下,Elasticsearch 中的每个索引分配了 5 个主分片和 1 个副本,这也就意味着如果您的集群至少有两个节点的话,您的索引将会有 5 个主分片和另外 5 个副本分片(1 个完整的副本),每个索引共计 10 个分片。

主节点自动选举

elasticsearch集群一旦建立起来以后,会选举出一个master,其他都为slave节点。
但是具体操作的时候,每个节点都提供写和读的操作。就是说,你不论往哪个节点中做写操作,这个数据也会分配到集群上的所有节点中。

这里有某个节点挂掉的情况,如果是slave节点挂掉了,那么首先关心,数据会不会丢呢?不会。如果你开启了replicate,那么这个数据一定在别的机器上是有备份的。
别的节点上的备份分片会自动升格为这份分片数据的主分片。这里要注意的是这里会有一小段时间的yellow状态时间。

如果是主节点挂掉怎么办呢?当从节点们发现和主节点连接不上了,那么他们会自己决定再选举出一个节点为主节点。
但是这里有个脑裂的问题,假设有5台机器,3台在一个机房,2台在另一个机房,当两个机房之间的联系断了之后,每个机房的节点会自己聚会,推举出一个主节点。
这个时候就有两个主节点存在了,当机房之间的联系恢复了之后,这个时候就会出现数据冲突了。
解决的办法就是设置参数:

discovery.zen.minimum_master_nodes
为3(超过一半的节点数),那么当两个机房的连接断了之后,就会以大于等于3的机房的master为主,另外一个机房的节点就停止服务了。

对于自动服务这里不难看出,如果把节点直接暴露在外面,不管怎么切换master,必然会有单节点问题。所以一般我们会在可提供服务的节点前面加一个负载均衡。

参考:
https://www.cnblogs.com/yjf512/p/4865930.html
http://cwiki.apachecn.org/pages/viewpage.action?pageId=4260364

安装

安装三个节点如下,三个节点的角色为 默认的 主节点+数据节点(此处为安装测试,生产环境官方建议每个节点只担任一个角色)

主机名 ip
master 192.168.255.130
slave1 192.168.255.121
slave2 192.168.255.122

1、下载安装

wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-6.x/6.4.3/elasticsearch-6.4.3.rpm -P /opt/software/
rpm -ivh /opt/software/elasticsearch-6.4.3.rpm
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service

测试是否成功

curl http://localhost:9200
{
  "name" : "4BUPFCB",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "JRF6JJLaQLykruIBrx_mFQ",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

2、配置

自行修改每个节点上不同的配置项

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: install-test  #cluster.name相同时会自动加入集群
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: master
#

node.master: true  #属性值为true或false,表示节点是否具有成为主节点的资格,属性的值为true,并不意味着这个节点就是主节点。因为真正的主节点,是由多个具有主节点资格的节点进行选举产生的。

node.data: true    #表示节点是否存储数据

# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: true  #锁定内存中进程的地址空间,用来禁止使用swap分区,因为这会严重影响集群性能和稳定性,此项生效需要从系统层面禁用swap功能,可以通过编辑/etc/fstab文件,注释文件中所有包含swap单词的行。
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.255.130
#
# Set a custom port for HTTP:
#
http.port: 9200

transport.tcp.port: 9300  #集群内部通讯端口
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.zen.ping.unicast.hosts: ["192.168.255.121", "192.168.255.122"]  #单播模式,扫描列表中的主机节点,使用9300端口通讯,自动组成集群
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
discovery.zen.minimum_master_nodes: 2 #设置当前集群中主节点数最少是多少,一般为所以节点数量的一半以上,这样做的目的是防止脑裂.
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#
#
#
#
#下面两行配置用来使head插件可以以http方式访问es集群信息
http.cors.enabled: true
http.cors.allow-origin: "*"

3、验证是否安装成功

查看集群节点信息,pretty意思是打印出漂亮的格式


[root@master] /usr/local$ curl -XGET '192.168.255.130:9200/_cat/nodes?v&pretty'
ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.255.122           12          93   1    0.01    0.04     0.05 mdi       -      slave2
192.168.255.121           15          93   1    0.08    0.10     0.07 mdi       -      slave1
192.168.255.130           11          94   0    0.16    0.08     0.06 mdi       *      master

常用的命令操作

1、创建索引

[root@master] /usr/local$ curl -XPUT '192.168.255.130:9200/customer?pretty&pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}

2、查询索引

命令的结果告诉我们现在已经有 1 个名为 customer 的索引,并且它有 5 个主分片和 1 个副本(默认)以及它包含了 0 文档在索引中。

[root@master] /usr/local$ curl -XGET '192.168.255.130:9200/_cat/indices?v&pretty'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   customer T-Ic8JOcSqmpiSuQmwwTYQ   5   1          0            0      2.2kb          1.1kb

3、创建索引和查询文档
现在让我们放入一些东西动我们的 customer 索引中去。回想一下前面的内容,为了索引一个文档,我们必须告诉 Elasticsearch 在索引中应该使用哪种类型。
让我们索引一个简单的 customer 文档到 customer 索引中,“external” 类型,与一个为 1 的 ID,如下所示 :

[root@master] /usr/local$ curl -XPUT -H 'Content-Type: application/json' 192.168.255.130:9200/customer/external/1?pretty -d'
> {
> "name": "John Doe"
> }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

从上面我们可以看到一个新的 customer 文档被成功的创建到 customer 索引和 external 类型中。该文档还有一个为 1 的内部 ID,它是我们在索引时指定的。

需要注意的是,在您可以索引文档到 Elasticsearch 之前时,它不需要您首先就明确的创建一个索引。在前面的例子中,Elasticsearch 将自动的创建 customer 索引(如果它事先不存在)。

现在让我们检索我们刚刚索引的文档 :

4、检索文档

[root@master] /usr/local$ curl -XGET '192.168.255.130:9200/customer/external/1?pretty&pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

除了一个字段 found 之外,没有什么特别的东西,它指出了我们使用 ID 为 1 请求的状态,另一个字段,_source,它返回了我们先前步骤中索引的全部的 JSON 文档。

5、删除索引

[root@master] /usr/local$ curl -XDELETE '192.168.255.130:9200/customer?pretty&pretty'
{
  "acknowledged" : true
}
[root@master] /usr/local$ curl -XGET '192.168.255.130:9200/_cat/indices?v&pretty'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

6、更新文档

[root@master] /usr/local$ curl -H 'Content-Type: application/json' -XPOST '192.168.255.130:9200/customer/external/1/_update?pretty&pretty' -d'
> {
> "doc": {"name": "Jane Doe" }
> }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

添加age字段

[root@master] /usr/local$ curl -H 'Content-Type: application/json' -XPOST '192.168.255.130:9200/customer/external/1/_update?pretty&pretty' -d'
{
"doc": {"name": "Jane Doe", "age": 20 }
}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

使用简本更新

[root@master] /usr/local$ curl -H 'Content-Type: application/json' -XPOST '192.168.255.130:9200/customer/external/1/_update?pretty&pretty' -d'
{
"script": "ctx._source.age += 5"
}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

在上面的例子中,ctx._source 代表当前将被更新的源文档。

请注意,在编写该文档时,在同一时间中更新只能够被执行一个文档上。在将来,Elasticsearch 也许会提供给定一个查询条件(像 SQL UPDATE-WHERE 语句)来更新多个文档的功能。
7、删除文档

curl -XDELETE '192.168.255.130:9200/customer/external/2?pretty&pretty'

head插件

采用最简单的chrome插件方式安装,连接集群内任一节点ip均可。
github地址: https://github.com/mobz/elasticsearch-head#running-with-built-in-server
chrome插件地址: https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm/
从web界面可以很直观的可以看到, 绿色方块0-4就是shard分片,分散在集群的各个节点上,并且有一个副本也分散到各个节点上,这样就实现了高可用的目的。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/fanren224/article/details/84070166