您的位置:首页 > 其它

Elasticsearch权威指南--集群

2016-04-21 22:16 381 查看
本文是基于Elasticsearch2.3写的
1.一个空的集群
如果启动单个Elasticsearch实例,没有数据和索引。那么集群就是如下形式:



Node 节点,就是一个运行的Elasticsearch实例。一个集群Cluster包含一个或者多个具有相同cluster.name的节点。cluster.name在elasticsearch.yml中指定。一个集群中的节点共同分担整个集群的数据和压力。当向集群中增加或者删除节点时,集群自身会从新均匀地分布数据到其他节点上。
集群中的Master节点时通过选举产生的,Master节点会负责整个集群层面的变更,例如创建或者删除一个索引,或者增加或删除一个节点。Master节点并不会涉及到文档层面的变更或者查询,这就意味着当流量增长时如果在集群中只有一个Master节点并不会成为瓶颈。任何节点都可以成为Master节点。

用户可以和集群中任何一个节点通信,包括Master节点。每个节点都知道每个文档存放在哪里并且会将我们的查询请求直接转发到存储所要数据的节点上。然后那个节点会将响应直接返回给客户端。所有这些都是通过Elasticsearch自身来管理的。

2.集群健康状况
curl {' target='_blank'>http://127.0.0.1:9200/_cluster/health?pretty
{ "cluster_name" : "xxxxx_prod_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.0
}


最重要的就是status字段,表示整个集群的监控状况
green 表示所有的primary和repica分片活动状态。
yellow 表示所有的primary分片处于活动状态,但是不是所有的replica分片是活动的
red 不是所有的primary分片都是活动的

3.增加一个索引
为了向Elasticsearch中添加数据,我们需要一个索引,就是一个存放数据的地方。实际上,一个索引就仅是一个指向一个或者多个物理分片的逻辑上的域名空间而已。

一个分片只是索引中的所有的数据的一个切片,它是一个低级的工作单元。文档是存放并索引在分片中的,但是应用程序不直接和分片通信,而是通过索引来通信。

一个分片要么是一个primary分片或者一个replica分片。索引中的每条文档都归属于单个primary分片。所以,primary分片的数量决定了索引可以存储的最大数据容量。

replica分片仅仅是primary分片的一个拷贝,是为了为数据提供冗余,防止在硬件出故障的时候数据丢失。并且提供查询文档或者获取文档的功能。
现在创建一个叫做blogs的索引

curl -XPUT  curl' target='_blank'>http://127.0.0.1:9200/blogs
curl -XGET  {' target='_blank'>http://127.0.0.1:9200/blogs?pretty=true
{ "blogs" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1461250293462",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "fIdjCvoXTQy9eMaKmIrnrQ",
"version" : {
"created" : "2030199"
}
}
},
"warmers" : { }
}
}


默认情况下,新创建的索引会被分配5个primary分片,每个primary分片被分配1个replica分片。



然后再查看集群的状态
curl -XGET  {' target='_blank'>http://127.0.0.1:9200/_cluster/health?pretty
{ "cluster_name" : "xxxx_prod_elasticsearch",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 5,
"active_shards" : 5,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 5,
"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" : 50.0
}


可以看到status是yellow状态,unassigned_shards是5
集群健康状态是yellow表示所有的primary分片都是活动的,可以向外提供任何的请求,但是不是所有的replica分片是活动的。

4.增加备用节点
只运行单个节点意味着存在单点故障的问题,没有冗余功能。可以通过增加备用节点来保护我们的数据。一台物理机上的多个Elasticsearch可以共用一个数据目录,只要cluster.name设置相同会自动添加到集群中,如果多个实例部署在不同的物理机上就需要做些配置。

cluster.name: xxxx
node.name: xxxxx
network.host: xxxxx
discovery.zen.ping.unicast.hosts: ["xxxx", "xxxx"]

需要在elasticsearch.yml中作以上设置,使用unicast来发现集群节点.从Elasticsearch2.0开始默认绑定IP是localhost,所以需要单独设置需要绑定的IP。由于Elasticsearch的各个版本可能存在一些配置变更,在部署的时候要注意查看使用的版本相关的说明文档。



两台都配置好后,新增加的节点就会自动加入到集群中,在添加节点的时候一定要注意新添加的节点不能比其他节点先启动,也就是一定不能让新添加的空的节点成为Master,要不然数据就全部清空了。
一个新增的索引会先存储在primary分片上,然后会被并行复制到其他的replica节点。这就保证了需要查找的文档可以从primary分片或者任何它的replica分片上查找。

再查看集群状态
{
"cluster_name" : "xxxxxx",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 5,
"active_shards" : 10,
"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
}


status变成了green,active_shards变成了10,unassigned_shards变成了0

5.水平扩展

再添加第三个节点到集群中去
查看三个节点的日志可以看出集群检测到了Node3



primary分片的数量在索引被创建的时候就固定了。

参考文档:
https://www.elastic.co/guide/en/elasticsearch/guide/current/distributed-cluster.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch