Elasticsearch6.5.*集群搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013305783/article/details/86590712

一、环境准备

(一)、新增用户

   启动Elasticsearch需要使用非Root用户,否则会报错这里新增一个bigdata用户

useradd bigdata
#为用户添加密码:
passwd bigdata
#将bigdata添加到sudoers
echo "bigdata ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/bigdata
chmod 0440 /etc/sudoers.d/bigdata
#解决sudo: sorry, you must have a tty to run sudo问题,在/etc/sudoer注释掉 Default requiretty 一行
sudo sed -i 's/Defaults    requiretty/Defaults:bigdata !requiretty/' /etc/sudoers

#创建一个bigdata目录
mkdir /{bigdata,data}
#给相应的目录添加权限
chown -R bigdata:bigdata /{bigdata,data}

(二)、安装JDK

   下载java安装包,并解压,vi /etc/profile
export JAVA_HOME=/home/hadoop/jdk1.8.0_161
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=$PATH:$JAVA_HOME/bin:

二、安装Elasticsearch

1、解压安装包

   tar -zxvf elasticsearch-6.5.4.tar.gz

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 see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
# cluster.name: my-application

# 这个是集群的名字, 关系到是否会组成一个集群(相同会在一个集群)
cluster.name: elaticsearch

# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: mini1

# 当前节点的名字,要唯一, 不给时服务会给它一个
node.name: "master-node"

# 为true 它具备成为主节点的资格, 默认就是true
#node.master: true

# Add custom attributes to the node:
#
# node.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

#数据存放位置
path.data: /export/data/es
#日志存放位置
path.logs: /export/logs/es

# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable 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.0.1

#es绑定的ip地址或主机名
network.host: master

# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- 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: ["host1", "host2"]

#初始化时可以进行选举的节点
discovery.zen.ping.unicast.hosts: ["mini1", "mini2" , "mini3"]

#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

3、使用scp拷贝到其他节点

scp -r elasticsearch-6.5.4/ mini2:/home/hadoop
scp -r elasticsearch-6.5.4/ mini3:/home/hadoop

   在其他节点上修改es配置,需要修改的有node.name和network.host

4、启动es

   elasticsearch-6.5.4/bin/elasticsearch -d 以后台模式启动
   用浏览器访问es所在机器的9200端口:

http://192.168.62.131:9200/
{
  "name" : "mini2",
  "cluster_name" : "bigdata",
  "cluster_uuid" : "jaP_VMg8RJuBqnBaTEMmyA",
  "version" : {
    "number" : "6.5.4",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "d2ef93d",
    "build_date" : "2018-12-17T21:17:40.758843Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

5、重启

ps -ef | grep elastic
kill -9 2604

6、测试实例

   向store索引中添加一些书籍

curl  -H "Content-Type: application/json" -XPUT 'http://192.168.62.131:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2015-02-06",
  "price":"49.99"
}'

   通过浏览器查询
   http://192.168.62.131:9200/store/books/1
   再添加一个书的信息

curl -H "Content-Type: application/json"  -XPUT 'http://192.168.62.131:9200/store/books/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'

   可以通过覆盖的方式更新

curl -H "Content-Type: application/json" -XPUT 'http://192.168.62.131:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2016-02-06",
  "price":"99.99"
}'

   最简单filter查询

curl -H "Content-Type: application/json" -XGET 'http://192.168.62.131:9200/store/books/_search' -d '{
    "query" : {
        "bool" : {
            "filter" : {
                "term" : {
                    "price" : 35.99
                  }
              }
        }
    }
}'

7、错误

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

   在 /etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144

三、安装elasticsearch-head

(一)、nodejs安装

   安装elasticsearch-head插件需要nodejs的支持,所以此处讲解一下安装nodejs步骤:

1、下载nodejs

   到nodejs官网现在最新nodejs,官网下载地址:https://nodejs.org/en/download/

2、解压压缩包

   tar -xvf node-v10.15.0-linux-x64.tar.xz

3、在/etc/profile中配置好path环境变量

export PATH=$PATH:/home/hadoop/node-v10.15.0-linux-x64/bin

4、执行node -v验证安装

(二)、elastichsearch-head安装

1、下载

   git clone https://github.com/mobz/elasticsearch-head.git

2、安装

   到elasticsearch-head目录下, 执行npm install 命令, 执行该命名可能会出现以下错误:
> [email protected] install /home/hadoop/elasticsearch-6.2.3/plugins/elasticsearch-head/node_modules/phantomjs-prebuilt
> node install.js

sh: node: command not found
npm WARN [email protected] license should be a valid SPDX license expression
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] install: `node install.js`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-01-22T11_39_28_566Z-debug.log

   此时忽略[email protected],执行命令如下

npm install [email protected] --ignore-scripts

   安装成功显示如下:

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] license should be a valid SPDX license expression
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ [email protected]
added 66 packages and removed 4 packages in 18.237s

3、启动elastichsearch-head

npm run start
验证elasticsearch-head,在浏览器中输入主机地址和端口

在这里插入图片描述

4、重启elastichsearch

   当重启elasticsearch可能会出现以下错误, 这个是因为不能将elasticsearch-head放到plugins文件夹下

[o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalArgumentException: property [elasticsearch.version] is missing for plugin [head]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.3.jar:6.2.3]
    at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.2.3.jar:6.2.3]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) ~[elasticsearch-6.2.3.jar:6.2.3]
Caused by: java.lang.IllegalArgumentException: property [elasticsearch.version] is missing for plugin [head]
    at org.elasticsearch.plugins.PluginInfo.readFromProperties(PluginInfo.java:226) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.plugins.PluginInfo.readFromProperties(PluginInfo.java:184) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.bootstrap.Spawner.spawnNativePluginControllers(Spawner.java:75) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:167) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:323) ~[elasticsearch-6.2.3.jar:6.2.3]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-6.2.3.jar:6.2.3]
    ... 6 more

   当重启服务器之后发现依旧如上图, 还是无法正常连接到elasticsearch服务,这是因为elasticsearch服务与elasticsearch-head之间可能存在跨越,修改elasticsearch配置即可,在elastichsearch.yml中添加如下命名即可

http.cors.enabled: true
http.cors.allow-origin: "*"

   重新连接如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u013305783/article/details/86590712