Skip to content

Elasticsearch Three-nodes Cluster

三节点集群 Three-node cluster

  • 高可用一主(master/primary)二次(secondary)结构,集群自动同步保持数据一致性
    • 任意一个节点(无论主次)宕机了,会从另两个中选出新主,另两个依然可读写;health 会变为 yellow ;
    • 任意一个节点(无论主次)宕机了,集群相关 path 接口依然可用;
  • 每个分片数据冗余存储到其他节点上;
    • 客户端需要通过 load balance 访问集群,或设置失败重试轮写另一个,避免写固定一个节点;
    • 推荐在生产环境使用此结构;

搭建步骤

测试环境

  • ES 版本 8.18.2
  • 虚拟机系统

如果每个 ES 节点部署在不同主机上,注意:

  • 各个主机之间可互相访问 9200 (HTTP 默认)和 9300 (transport 默认)端口
  • 新增 ES 节点数据目录为空,不能已初始化或加入过其他集群

es01 es02 节点参考《双节点集群》搭建;

配置 es03 节点

shell
mkdir es03
cp -a config es03/

修改 es03/config/elasticsearch.yml

yaml
bootstrap.memory_lock: true

xpack.security.enabled: false
xpack.security.enrollment.enabled: false
xpack.security.http.ssl.enabled: false
xpack.security.transport.ssl.enabled: false

path.data: es03/data
path.logs: es03/logs

http.port: 9203

cluster.name: my-cluster
node.name: es03

network.host: 0.0.0.0
cluster.initial_master_nodes: ["es01"]

# 多个 ES 节点部署在不同主机上时需要设置
network.publish_host: "10.0.0.3"
discovery.seed_hosts: ["10.0.0.1", "10.0.0.2", "10.0.0.3"]

启动 es03 节点 ES_PATH_CONF=es03/config bin/elasticsearch

es01 es02 的 配置文件 以下几个参数改为一致

  • cluster.initial_master_nodes: ["localhost:9201","localhost:9202","localhost:9203"]
  • node.master: true
  • node.data: true

先后重启 es01 es02 节点。

观察 health 和 master

curl 'localhost:9201/_cat/health?v=true'
curl 'localhost:9201/_cat/master?v=true'

实验 1: 集群(green)可用时,可同时读写主或从节点

TBD.

实验 2: 次节点宕机,其他两个节点可读写

TBD.

实验 3: 主节点宕机,其他两个节点选出新主并可读写

TBD.

Released under the CC-BY-NC-4.0