elasticsearch-6.2.3集群搭建

开发环境

我这里在本地windows做demo,linux基本差不多,外网搭建会稍有不同,我会特别注释出来。

准备

下载对应版本的elasticsearch

地址:http://www.elastic.co/downloads/elasticsearch
我下载的是6.2.3,由于需要搭集群,下载解压之后,再拷贝两份相同的放到本地

下载elasticsearch-head

地址:https://github.com/mobz/elasticsearch-head

这个工具主要用来监测一下es的运行状态和做简单查询。es5.0以上版本直接使用

npm install
npm run start

就可以了。

编写集群配置

分别进入三个es目录下的config目录,修改elasticsearch.yml文件,直接上代码
主节点配置:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: ooo-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#节点名字
node.name: node-master
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#本机地址
network.host: 127.0.0.1
#
# Set a custom port for HTTP:
#
#http端口
http.port: 9201
#tcp通讯端口
transport.tcp.port: 9330
#是否作为主机
node.master: true
#是否作为数据节点
node.data: false
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#集群信息,默认的通讯接口是9300
discovery.zen.ping.unicast.hosts: ["127.0.0.01:9330", "127.0.0.01:9331","127.0.0.01:9332"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#开启跨域访问支持,默认为false
http.cors.enabled: true
##跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: /.*/  

数据1节点配置:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: ooo-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#节点名字
node.name: node-data1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#本机地址
network.host: 127.0.0.1
#
# Set a custom port for HTTP:
#
#http端口
http.port: 9202
#tcp通讯端口
transport.tcp.port: 9331
#是否作为主机
node.master: false
#是否作为数据节点
node.data: true
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#集群信息,默认的通讯接口是9300
discovery.zen.ping.unicast.hosts: ["127.0.0.01:9330", "127.0.0.01:9331","127.0.0.01:9332"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#开启跨域访问支持,默认为false
http.cors.enabled: true
##跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: /.*/  

数据节点2配置:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: ooo-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#节点名字
node.name: node-data2
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#本机地址
network.host: 127.0.0.1
#
# Set a custom port for HTTP:
#
#http端口
http.port: 9203
#tcp通讯端口
transport.tcp.port: 9332
#是否作为主机
node.master: false
#是否作为数据节点
node.data: true
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#集群信息,默认的通讯接口是9300
discovery.zen.ping.unicast.hosts: ["127.0.0.01:9330", "127.0.0.01:9331","127.0.0.01:9332"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#开启跨域访问支持,默认为false
http.cors.enabled: true
##跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: /.*/  

使用外网搭建集群时需注意:

network.host需要修改为0.0.0.0,同时要暴露你的外网地址,代码如下:

network.host: 0.0.0.0
network.publish_host: xx.xx.xx.xx

discovery.zen.ping.unicast.hosts修改为各个服务器的外网地址和通讯端口即可。

启动ES

首先进入主节点es目录下的bin目录,windows可以直接双击elasticsearch.bat启动,linux需要非root用户使用命令启动。
主节点启动好后,用同样的方法启动子节点就可以了。

检查集群情况

启动elasticsearch-head,启动方法就不多说了,实在不懂可以详看github上的说明。连接任一节点的http地址。

这里写图片描述

图(1)

成功,elasticsearch集群搭建就是这么简单。

猜你喜欢

转载自blog.csdn.net/Mr_OOO/article/details/80228286