面试题答案
一键面试1. 理解Reroute机制
Reroute机制允许手动或自动地将分片从一个节点移动到另一个节点,以优化集群的资源使用和性能。
2. 涉及的API
- Cluster Reroute API:
POST _cluster/reroute { "commands": [ { "move": { "index": "your_index", "shard": 0, "from_node": "source_node", "to_node": "destination_node" } } ] }
index
:指定要操作的索引。shard
:指定要移动的分片。from_node
:源节点名称。to_node
:目标节点名称。
- Allocation API:用于获取和设置索引级或集群级的分配设置。例如,获取索引分配设置:
设置索引分配设置(将索引分配到具有特定标签的节点):GET /your_index/_settings?include_defaults=true&filter_path=index.routing.allocation.*
PUT /your_index/_settings { "index.routing.allocation.require.tag": "value" }
3. 配置修改思路
区分热数据和冷数据
- 索引层面:
- 为热数据创建单独的索引模式(例如
hot_data_*
),为冷数据创建另一个索引模式(例如cold_data_*
)。 - 利用索引模板为不同模式的索引设置不同的分配和存储策略。
- 示例索引模板:
PUT _index_template/hot_template { "index_patterns": ["hot_data_*"], "settings": { "index.routing.allocation.require.hot": "true", "index.storage.type": "ssd" } }
PUT _index_template/cold_template { "index_patterns": ["cold_data_*"], "settings": { "index.routing.allocation.require.cold": "true", "index.storage.type": "hdd" } }
- 为热数据创建单独的索引模式(例如
- 节点层面:
- 为节点添加标签,标记其适合存储热数据还是冷数据。例如,在
elasticsearch.yml
中:
或node.attr.hot: true
node.attr.cold: true
- 为节点添加标签,标记其适合存储热数据还是冷数据。例如,在
动态性能调优
- 自动Reroute:
- 配置
cluster.routing.allocation.balance.shard
参数来控制分片在节点间的平衡程度。例如,设置为较低的值(如0.4),可以使分片更快地在节点间重新分配,以适应负载变化。
cluster.routing.allocation.balance.shard: 0.4
- 利用
cluster.routing.allocation.enable
参数来控制哪些类型的分片(如primaries
、replicas
或new_primaries
)可以进行分配。例如,在负载变化时,可能希望暂时禁用副本分片的分配,以减少资源消耗:
cluster.routing.allocation.enable: primaries
- 配置
- 手动Reroute:
- 监控集群负载指标(如CPU、内存、磁盘I/O等)。当检测到负载变化时,通过Cluster Reroute API手动移动分片。例如,如果某个节点的CPU负载过高,可以将其部分热数据分片移动到负载较低的节点:
POST _cluster/reroute { "commands": [ { "move": { "index": "hot_data_1", "shard": 0, "from_node": "overloaded_node", "to_node": "underloaded_node" } } ] }