实现方式
本地搭建elasticsearch 集群最方便的方法就是 直接复制多分es,通过配置文件进行关联,废话不多说直接开干
这是我的本地的目录
|—elasticsearch-1
|—elasticsearch-2
|—elasticsearch-master
我这里以 master 为主节点,另外两个为子节点
配置文件
- 修改 elasticsearch-master/config/elasticsearch.yml
cluster.name: test-es
node.master: true
node.attr.rack: r1
node.name: node
network.host: 192.168.1.199
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["192.168.1.199:9300", "192.168.1.199:9301", "192.168.1.199:9302"]
cluster.initial_master_nodes: ["node","node-1","node-2"]
- 修改 elasticsearch-1/config/elasticsearch.yml
cluster.name: glj-es
node.name: node-1
node.master: false
node.attr.rack: r1
node.max_local_storage_nodes: 3
bootstrap.memory_lock: false
network.host: 192.168.1.199
http.port: 9201
transport.tcp.port: 9301
discovery.seed_hosts: ["192.168.1.199:9300", "192.168.1.199:9301", "192.168.1.199:9302"]
cluster.initial_master_nodes: ["node","node-1","node-2"]
- 修改 elasticsearch-2/config/elasticsearch.yml
cluster.name: test-es
node.name: node-2
node.master: false
node.attr.rack: r1
bootstrap.memory_lock: false
network.host: 192.168.1.199
http.port: 9202
transport.tcp.port: 9302
discovery.seed_hosts: ["192.168.1.199:9300", "192.168.1.199:9301", "192.168.1.199:9302"]
cluster.initial_master_nodes: ["node","node-1","node-2"]
配置解释
- cluster.name 集群名称, 相同集群必须相同
- node.name 节点名, 不同节点必须不同
- node.master 是否可作为主节点使用
- network.host 服务器ip地址
- http.port http访问端口
- transport.tcp.port 集群内部通讯访问端口
- discovery.seed_hosts 配置多节点【地址:端口】
- cluster.initial_master_nodes 配置多节点【节点名】
<font color=red> 注 </font> 一定要删除子节点es data
目录下 面的文件,否则会导致子节点启动时无法加入集群,192.168.1.199
这里是我电脑在局域网中的IP,为了使其它局域网内可以访问,所以这里直接监听这个地址
启动服务
- 先启动主节点maste
elasticsearch-master\bin\elasticsearch.bat
打印结果中包含以下信息,说明主节点已经启动了
[2020-12-15T21:37:33,732][INFO ][o.e.n.Node ] [node] started
[2020-12-15T21:37:33,966][INFO ][o.e.l.LicenseService ] [node] license [74789c9a-81eb-41e6-be37-22897ec9017a]
- 启动次节点 node-1
elasticsearch-1\bin\elasticsearch.bat
可以发现有如下打印结果,这说明主节点已经启动了
[2020-12-15T21:42:28,856][INFO ][o.e.n.Node ] [node-1] started
- 启动次节点 node-2
elasticsearch-2\bin\elasticsearch.bat
可以发现有如下打印结果,这说明主节点已经启动了
[2020-12-15T21:43:00,241][INFO ][o.e.n.Node ] [node-2] started
后加入节点时,之前开启的节点窗口都会有已个 新加入节点的提示,如下
[2020-12-15T21:42:59,865][INFO ][o.e.c.s.ClusterApplierService] [node-1] added {{node-2}{7zipBQ80TQ2KbkFfPDoM7A}
测试
开启浏览器,并打开 127.0.0.1:9200/_cluster/health
访问
{
"cluster_name": "glj-es",
"status": "green",
"timed_out": false,
"number_of_nodes": 3,
"number_of_data_nodes": 3,
"active_primary_shards": 6,
"active_shards": 12,
"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
}