centos7下elasticsearch 集群安装、配置及示例

简介 
本文是在两台centos7虚拟机下,安装了三个节点。本次搭建es同时实践了两种模式——单机模式和分布式模式。条件允许的话,可以在多台机器上配置es节点,如果你机器性能有限,那么可以在一台虚拟机上完成多节点的配置。 
如图,是本次3个节点的分布。

hostname IP es节点
master 192.168.8.100 node1、node3
slave 192.168.8.101 node2
注:es的主节点是选出来的,它不一定在master 机器上,也就是es主节点不一定非得是node1或node2,也可能是node3.

1、下载

名称 版本 下载地址
elasticsearch 6.1.1 https://www.elastic.co/downloads/elasticsearch/elasticsearch-6.1.1.tar.gz

下载后,放到对应的目录下并解压. 因为我们要配置包含三个节点的集群,可以先将其重命名为elasticsearch-node1。比如存放路径及命名如: /home/zkpk/elasticsearch-node1。

2、修改配置文件

(1) 初步修改 

打开/home/zkpk/elasticsearch-node1/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: elasticsearch
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: es-node3
#
# 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: 192.168.8.100
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# 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]"]
#
discovery.zen.ping.unicast.hosts: ["192.168.8.104", "192.168.8.105", "192.168.8.104:9301"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
discovery.zen.minimum_master_nodes: 2 
#
# 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

network.bind_host: 192.168.8.104
#设置绑定的ip地址,这是master虚拟机的IP

network.publish_host: 192.168.8.104
#设置其它节点和该节点交互的ip地址

(2) 进一步修改 
拷贝 elasticsearch-node1 整个文件夹,两份,一份elasticsearch-node2,一份elasticsearch-node3. 
将elasticsearch-node2 文件夹copy到另外一台IP为192.168.8.105的机器上。而在 192.168.8.104 机器上有 node1和node3.

对于node3: node3和node1在一台机器上,node1的配置文件里端口默认分别是9300和9200,所以要改一下node3配置文件里的端口,elasticsearch.yml 文件修改如下:

node.name: "es-node3"
# 在配置文件最后添加下面配置信息
transport.tcp.port: 9301
http.port: 9201


对于node2:  对  elasticsearch.yml  修改如下:
node.name: "es-node2"
network.bind_host: 192.168.8.105
network.publish_host: 192.168.8.105
network.host: 192.168.8.105

二、运行 & 关闭 elasticsearch

1、运行elasticsearch:

前台运行命令:

[lyz@localhost /]$ sh /home/zkpk/elasticsearch-node1/bin/elasticsearc

后台运行命令:

[lyz@localhost /]$ sh /home/zkpk/elasticsearch-node1/bin/elasticsearch -d

2、关闭elasticsearch:

前台运行:可以通过”CTRL+C”组合键来停止运行 
后台运行,可以通过”kill -15 进程号”停止.也可以通过REST API接口:

通过如下语句关闭整个集群:

curl -XPOST http://主机IP:9200/_cluster/nodes/_shutdown
通过如下语句关闭单个节点:
curl -XPOST http://主机IP:9200/_cluster/nodes/节点标示符(如es-node1)/_shutdown

三、错误解决

1、启动es过程这若报以下错误:
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
这是因为elasticsearch默认是不支持用root用户来启动的,会报错。解决方案为添加其他用户:
chown -R lyz:lyz elasticsearch-node1
2、对于以下错误:
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

解决方式如下:

ulimit -n 65536 
or set nofile to 65536 in /etc/security/limits.conf
3、对如下错误
max number of threads [1024] for user [xxx] is too low, increase to at least [2048]
解决方式:
ulimit -u 2048
Or set the nproc value to 2048 or above in /etc/security/limits.conf before starting elasticsearch.
4、对于错误:
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决:
set vm.max_map_count=262144 in /etc/sysctl.conf then do sysctl -p


你可能感兴趣的:(centos7下elasticsearch 集群安装、配置及示例)