一、集群管理
1.1 单机&集群
- 单台Elasticsearch服务器提供服务,往往都有最大的负载能力,超过这个阈值,服务器性能就会大大降低甚至不可用,所以生产环境中,一般都是运行在指定服务器集群中
- 除了负载能力,单点服务器也存在其他问题
- 单台机器存储容量有限
- 单服务器容易出现单点故障,无法实现高可用
- 单服务的并发处理能力有限
- 配置服务器集群时,集群中节点数量没有限制,大于等于2个节点就可以看做是集群了
- 一般出于高性能及高可用方面来考虑集群中节点数量都是3个以上。
1.2 集群Cluster
- 一个集群就是由一个或多个服务器节点组织在一起,共同持有整个的数据,并一起提供索引和搜索功能
- 一个Elasticsearch集群有一个唯一的名字标识,这个名字默认就是”elasticsearch”
- 这个名字很重要,一个节点只能通过指定某个集群的名字来加入这个集群
1.3 节点Node
- 集群中包含很多服务器,一个节点就是其中的一个服务器
- 作为集群的一部分,它存储数据,参与集群的索引和搜索功能。
- 一个节点也是由一个名字来标识的,默认情况下,这个名字是一个随机的漫威漫画角色的名字,这个名字会在启动的时候赋予节点。这个名字对于管理工作来说比较重要,因为在这个管理过程中,会去确定网络中的哪些服务器对应于Elasticsearch集群中的哪些节点
- 一个节点可以通过配置集群名称的方式来加入一个指定的集群,默认情况下,每个节点都会被安排加入到一个叫做“elasticsearch”的集群中,这意味着,如果你在你的网络中启动了若干个节点,并假定它们能够相互发现彼此,它们将会自动地形成并加入到一个叫做“elasticsearch”的集群中
- 在一个集群里,可以拥有任意多个节点,如果当前你的网络中没有运行任何Elasticsearch节点,这时启动一个节点,会默认创建并加入一个叫做“elasticsearch”的集群
- 集群健康状态
- green:所有主分配和副本分片都正常运行
- yellow:主分片正常,但存在副本分片不正常
- red:存在主分配没有正常运行
二、Windows集群
- 复制份ES安装文件,并按照端口号分别按照如下命名,具体按照自己需求命名即可

- 节点说明,该集群测试一个主节点带两个数据节点
- 9200:master节点
- 9201:数据节点
- 9202:数据节点
- 集群操作是建立在单节点操作上,下面的集群做增量配置,前置如jdk等配置参考单节点安装,先要保证单节点启动运行正常
2.1 主节点
- 修改config/elasticsearch.yml文件,增加以下配置
cluster.name: elasticsearch
node.name: node-9200
node.master: true
node.data: true
network.host: localhost
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
- 启动主节点,观察是否正常启动
2.2 数据节点
- 两个数据节点配置相同,只需要按照各自端口改一下http和transport端口
- 修改config/elasticsearch.yml文件,增加以下配置
- 注意:node.master: false,只是作为数据节点,如果是主节点需要开启
- 注意:discovery.seed_hosts需要配置主节点列表
- elasticsearch-9201配置
cluster.name: elasticsearch
node.name: node-9201
node.master: false
node.data: true
network.host: localhost
http.port: 9201
transport.tcp.port: 9301
discovery.seed_hosts: ["localhost:9300"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: elasticsearch
node.name: node-9202
node.master: true
node.data: true
network.host: localhost
http.port: 9202
transport.tcp.port: 9302
discovery.seed_hosts: ["localhost:9300"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
http.cors.enabled: true
http.cors.allow-origin: "*"
- 分别启动来两个数据节点,然后观察3个节点是否都正常
2.3 集群测试
- 获取集群健康信息
- GET _cluster/health
- number_of_nodes:集群节点此时是3
- number_of_data_nodes:数据节点此时也是3
{
"cluster_name" : "elasticsearch",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 7,
"active_shards" : 14,
"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
}
- 向主节点写入数据
PUT cluster/_doc/1
{
"name": "集群测试",
"message": "主节点写入"
}
- 两个从节点查询数据,可以看到数据成功同步
{
"took": 25,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "cluster",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"name": "集群测试",
"message": "主节点写入"
}
}
]
}
}
三、Linux集群
- 准备三台Linux服务器
- 准备一份es服务文件,修改config/elasticsearch.yml文件,配置以下内容
- 注意前置jdk等环境参考核心模块单机安装先配置完成
cluster.name: cluster-es
node.name: node-1
network.host: linux1
node.master: true
node.data: true
http.port: 9200
http.cors.allow-origin: "*"
http.cors.enabled: true
http.max_content_length: 200mb
cluster.initial_master_nodes: ["node-1"]
discovery.seed_hosts: ["linux1:9300","linux2:9300","linux3:9300"]
gateway.recover_after_nodes: 2
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
cluster.routing.allocation.cluster_concurrent_rebalance: 16
cluster.routing.allocation.node_concurrent_recoveries: 16
cluster.routing.allocation.node_initial_primaries_recoveries: 16
- 将配置的es服务,分发到剩下两个服务器,不需要改动任何配置
- 分别启动三台服务器,然后按照windows集群下的集群测试即可