Skip to content

集群架构与选举 ​

集群发现机制 ​

1. 单播发现 ​

yaml
# 默认配置
discovery.seed_hosts:
  - 192.168.1.1
  - 192.168.1.2
  - 192.168.1.3

discovery.type: single-node  # 单节点模式

2. zen discovery(ES 7.x之前) ​

┌─────────────────────────────────────────┐
│           Zen Discovery                 │
├─────────────────────────────────────────┤
│                                          │
│  1. PING        节点探测                  │
│  2. Unicast    单播列表                  │
│  3. Master    Master选举                │
│  4. Fault     故障检测                 │
└─────────────────────────────────────────┘

3. 集群协调(7.x+) ​

yaml
# 推荐配置
cluster.initial_master_nodes:
  - node-1
  - node-2
  - node-3

Master选举 ​

选举流程 ​

                            ┌──────────────┐
                            │ 节点启动    │
                            └──────┬─────┘
                                   │
                            ┌──────▼──────┐
                            │ PING所有节点│
                            └──────┬─────┘
                                   │
                    ┌───────────────┼───────────────┐
                    │               │               │
              ┌─────▼─────┐   ┌─────▼─────┐   ┌─────▼─────┐
              │ 节点A     │   │ 节点B     │   │ 节点C     │
              │(master)  │   │           │   │           │
              └─────┬─────┘   └─────┬─────┘   └─────┬─────┘
                    │               │               │
                    └───────────────┼───────────────┘
                                   │
                    ┌──────────────▼──────────────┐
                    │ 按nodeId排序,票数>半数    │
                    │ 最小的成为Master          │
                    └──────────────┬──────────────┘
                                   │
                            ┌──────▼──────┐
                            │ Master就绪 │
                            └───────────┘

选举条件 ​

条件说明
node.master: true节点有资格成为Master
票数 > 节点数/2需要多数派
nodeId最小字典序最小的

配置参数 ​

yaml
# 脑裂问题配置
discovery.zen.minimum_master_nodes: 2  # 设为 (master节点数/2)+1

故障检测 ​

故障检测机制 ​

Master                              Data节点
   │                                    │
   │──────────── Ping ───────���────────────→│
   │←────────── Pong ──────────────────────│
   │                                    │
   │         (节点正常,每30s)              │
   │                                    │
   │───────── Ping ─────────────────────→│
   │×           (节点无响应)              │
   │                                    │
   │    ┌──────────────┐                  │
   │    │ 标记node为 │                  │
   │    │ 离线     │                  │
   │    └──────┬───┘                  │
   │         │                       │
   │    ┌────▼─────┐               │
   │    │ 重新分配 │               │
   │    │ 分片    │               │
   │    └─────────┘               │

fault detection配置 ​

yaml
discovery.zen.fd.ping_interval: 1s         # 探测间隔
discovery.zen.fd.ping_timeout: 30s        # 响应超时
discovery.zen.fd.ping_retries: 3              # 重试次数

脑裂问题 ​

什么是脑裂? ​

正常情况:
[A/Master] ─────── [B] ─────── [C]

脑裂(两个Master):
[A/Master1] ──  × ── [B] ──  × ── [C/Master2]
          两个集群,各有分片,数据不一致

解决方案 ​

yaml
# 1. 配置最小Master节点数
discovery.zen.minimum_master_nodes: 2  # (master节点数/2)+1

# 2. 7.x+,使用cluster coordination
cluster.coordination.election_timeout: 5s
cluster.coordination.minimum_voting_nodes: 3

节点类型与职责 ​

节点配置组合 ​

配置角色
master:true, data:trueMaster + Data
master:false, data:trueData节点
master:true, data:falseMaster节点
master:false, data:falseIngest/协调节点

协调节点 ​

yaml
node.master: false
node.data: false
node.ingest: false

# 处理请求转发和结果合并

面试考点 ​

Q: Master节点可以是Data节点吗? ​

可以,但不推荐(压力大)

Q: 脑裂怎么预防? ​

  • 设置minimum_master_nodes
  • 使用多数派选举

Q: 数据节点可以当Master? ​

可以,但数据量大时不推荐

Q: 如何发现集群状态? ​

bash
GET /_cluster/health
GET /_cluster/health?level=indices
GET /_cluster/health?level=shards

最后更新: