Centos7搭建ES集群

ES集群搭建

Posted by Jae on March 10, 2019

搭建环境

本次实验准备了6台Centos7的虚拟机来搭建ES集群环境,虚拟机内网IP如下:

master1 192.168.10.55
master2 192.168.10.108
data1 192.168.10.59
data2 192.168.10.60
client 192.168.10.54
ingest 192.168.10.61

ES中节点分为四种类型:

1) 主节点 master节点

主要职责就是负责集群管理,如索引创建或删除,发现集群其它节点并确定那些分片分配到相应的节点,默认情况下任何一个节点都有可能被选举为master节点,但是索引数据何搜索查询等操作会占用大量CPU、内存和IO资源,为了保证集群的稳定,通常分离主节点和数据节点;

node.master: true
node.data: false
node.ingest: false

2)数据节点 data节点

数据节点主要是存储索引数据,主要对文档进行增删改查操作,数据节点对CPU、内存和IO要求较高;

node.master: false
node.data: true
node.ingest: false

3)负载均衡节点 client节点

当一个节点既不配置为主节点也不配置为数据节点时,该节点只能处理路由请求,处理搜索,分发索引操作;

node.master: false
node.data: false
node.ingest: false

4) 预处理节点 ingest节点

预处理节点在索引数据之前可以对数据做预处理,默认所有的节点都支持ingest操作,但是也可以专门设置一个Ingest节点;

node.master: false
node.data: false
node.ingest: true

1.环境准备

Elasticsearch是基于Lucene的,Lucene是基于Java,所以每一台机器上需要安装java环境,这里我们安装的java1.8,安装java不再累述; 下载Elasticsearch安装包:下载地址

2.安装ES

Elasticsearch需要使用非root用户来运行,所以我们使用普通用户来操作,首先我们将安装包保存在/data目录下。该目录属主和属组均为普通用户。

2.1 解压并修改配置文件(这里以master1为例其它类似,其它的对应上面的node.*:bool来配置)
[xidian@localhost data]$ ls
elasticsearch-6.5.4  elasticsearch-6.5.4.tar.gz  elasticsearch-analysis-ik-6.5.4.zip
[xidian@localhost data]$ cd elasticsearch-6.5.4/
[xidian@localhost elasticsearch-6.5.4]$

编辑/config/elasticsearch.yml文件

# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: es-cluster   集群名字,集群中所有的节点中该名称要相同
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-master1 节点名称:集群中每一个节点名字要不相同
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
node.master: true  该节点可以被选为master
node.data: false   该节点不做数据节点

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0 允许其它机器访问
#
# 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.10.55", "192.168.10.108", "192.168.10.54", "192.168.10.59", "192.168.10.60",
 "192.168.10.61"] 集群中其它节点IP地址
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
discovery.zen.minimum_master_nodes: 2 配置集群最少主节点数目,注意这里计算公式为(可以被选举为master节点数)/2+1 本次实验中有两个可以被选举为master 所以这里设置为(2/2+1=2)
#
# For more information, consult the zen discovery module documentation.
2.2 启动ES

在前台启动ES:

[xidian@localhost elasticsearch-6.5.4]$ ./bin/elasticsearch
2.3 可能会报错

可能会报如下错:

1、max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
2、max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]
2.4.解决方法

第二个错误: 切换到root用户,编辑 /etc/sysctl.conf文件并在文件末尾追加如下内容

[root@localhost elasticsearch-6.5.4]# vi /etc/sysctl.conf

vm.max_map_count=262144 这里的数据根据ES报错来决定
sysctl -p

第一个错误: 切换到root用户,并编辑/etc/security/limits.conf文件,在文件末尾追加下面内容后重启机器

[xidian@localhost elasticsearch-6.5.4]$ ulimit -n 查看当前用户最大文件描述符数量
4096
[xidian@localhost elasticsearch-6.5.4]$ su
[root@localhost elasticsearch-6.5.4]# vi /etc/security/limits.conf

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

3. 查看集群状态

http://192.168.10.54:9200/_cluster/state?pretty
{
"cluster_name": "es-cluster",
"compressed_size_in_bytes": 10518,
"cluster_uuid": "dLW0ST1FQzKh4B6j5BEkrw",
"version": 9,
"state_uuid": "inyNT61mS2iagLICITEIpQ",
"master_node": "ce9CvxUyQ0Gqhzb7dRXB9g",
"blocks": {},
"nodes": {
    "rKqY1PCDTBK4qfm76w-2hA": {
        "name": "node-data2",
        "ephemeral_id": "e_RrOvOfQg25xSgxP5a9oQ",
        "transport_address": "192.168.10.60:9300",
        "attributes": {
            "ml.machine_memory": "3974705152",
            "ml.max_open_jobs": "20",
            "xpack.installed": "true",
            "ml.enabled": "true"
        }
    },
    "iq14LHD0R4yAtDIyVbJ13Q": {
        "name": "node-data1",
        "ephemeral_id": "w0baIUfcSEKhDhlGhpQ4KQ",
        "transport_address": "192.168.10.59:9300",
        "attributes": {
            "ml.machine_memory": "3974705152",
            "ml.max_open_jobs": "20",
            "xpack.installed": "true",
            "ml.enabled": "true"
        }
    },
    "Pl_ar_PDSTiOiJP4-pSsRA": {
        "name": "node-ingest",
        "ephemeral_id": "frHNQNPETMO3k1mNrmtpDA",
        "transport_address": "192.168.10.61:9300",
        "attributes": {
            "ml.machine_memory": "3974705152",
            "ml.max_open_jobs": "20",
            "xpack.installed": "true",
            "ml.enabled": "true"
        }
    },
    "xv2bWTIuToyufkbnyUSq3A": {
        "name": "node-client",
        "ephemeral_id": "6zh4NTq5RPe6_wScCoM0TQ",
        "transport_address": "192.168.10.54:9300",
        "attributes": {
            "ml.machine_memory": "3974705152",
            "ml.max_open_jobs": "20",
            "xpack.installed": "true",
            "ml.enabled": "true"
        }
    },
    "ce9CvxUyQ0Gqhzb7dRXB9g": {
        "name": "node-master2",
        "ephemeral_id": "NcP-06JwSgGdRdAcrc15Cw",
        "transport_address": "192.168.10.108:9300",
        "attributes": {
            "ml.machine_memory": "3974705152",
            "ml.max_open_jobs": "20",
            "xpack.installed": "true",
            "ml.enabled": "true"
        }
    },
    "z1A28w3ZTgeDLafAZwN47A": {
        "name": "node-master1",
        "ephemeral_id": "l4d4x536Qd22RPuGUGVD2g",
        "transport_address": "192.168.10.55:9300",
        "attributes": {
            "ml.machine_memory": "3974705152",
            "ml.max_open_jobs": "20",
            "xpack.installed": "true",
            "ml.enabled": "true"
        }
    }
},
...

4.设置开机自启

新建文件elasticsearch

#!/bin/bash
#
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch-6.5.4

export ES_HOME=/data/elasticsearch-6.5.4 # 这里是我es安装目录

case $1 in
    start)
            su xidian<<!  # 这里是我启动es的用户名
            cd $ES_HOME
            ./bin/elasticsearch -d -p pid
            exit
!
            echo "elasticsearch is started"
            ;;
    stop)
            pid=`cat $ES_HOME/pid`
            kill -9 $pid
            echo "elasticsearch is stopped"
            ;;
    restart)
            pid=`cat $ES_HOME/pid`
            kill -9 $pid
            echo "elasticsearch is stopped"
            sleep 1
            su xidian<<! # 这里是我启动es的用户名
            cd $ES_HOME
            ./bin/elasticsearch -d -p pid
            exit
!
            echo "elasticsearch is started"
    ;;
\*)
    echo "start|stop|restart"
    ;;  
esac
exit 0

将该文件拷贝到/etc/init.d/目录下并赋予权限

chmod 777 /etc/init.d/elasticsearch

添加和删除服务并设置开机启动方式

chkconfig --add elasticsearch
chkconfig --del elasticsearch

启动和关闭服务

service elasticsearch start
service elasticsearch stop
service elasticsearch restart

设置服务启动方式

chkconfig elasticsearch on
chkconfig elasticsearch off

查看chkconfig是否注册了我们的服务 sudo chkconfig –list

elasticsearch   0:关    1:开    2:开    3:开    4:开    5:开    6:关
mongod          0:关    1:关    2:关    3:开    4:关    5:开    6:关
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关

这里0-6的含义为:

0表示:表示关机
1表示:单用户模式
2表示:无网络连接的多用户命令行模式
3表示:有网络连接的多用户命令行模式
4表示:不可用
5表示:带图形界面的多用户模式
6表示:重新启动

然后重启我们的机器验证ES服务是否开机自启。


< script src = " / js / bootstrap.min.js " >