您的位置:首页 > 其它

安装配置ElasticSearch集群

2017-07-12 09:59 585 查看

安装配置ElasticSearch

Elasticsearch,简单点理解,就是在Lucene的基础上封装了一层分布式架构,它有如下特点:

* 处理方式灵活 Elasticsearch 是实时全文索引,不需要像 storm 那样预先编程才能使用;

* 配置简易上手 Elasticsearch 全部采用 JSON 接口,目前业界通用的配置语法设计;

* 集群线性扩展 Elasticsearch 集群可以扩展到上百台服务器,处理PB级结构化或非结构化数据;

* 检索性能高效 虽然每次查询都是实时计算,但是优秀的设计和实现基本可以达到百亿级数据查询的秒级响应;

1.下载安装

在官网进行下载中安装包:

https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.5/elasticsearch-2.3.5.tar.gz

使用ftp工具上传至linux上,在linux中进行解压缩

# tar -zxvf  elasticsearch.2.3.5.tar -c /usr/local/src/


elsaticsearch依赖于java,所以先保证你安装好了JDK。安装JDK此处就不做讲解了。

如果需要其他电脑访问 elasticsearch ,则需要修改配置文件

# vim /usr/local/src/elasticsearch.2.3.5/config/elasticsearch.yml
修改54行
network.host: 172.21.3.195


2.启动

启动elasticsearch时需要注意,因为elasticsearch有远程执行脚本的功能所以容易中木马病毒,所以不允许用root用户启动,测试阶段可以使用如下方式解决。

# /usr/local/src/elasticsearch.2.3.5/bin/elasticsearch
Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:270)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
Refer to the log for complete error details.


解决办法是运行时加上参数:Des.insecure.allow.root,如下:

# usr/local/src/elasticsearch.2.3.5/bin/elasticsearch -Des.insecure.allow.root=true


或者修改bin/elasticsearch,加上ES_JAVA_OPTS属性:

# vim /usr/local/src/elasti
4000
csearch.2.3.5/bin/elasticsearch
# ES_JAVA_OPTS="-Des.insecure.allow.root=true"


保存之后重启Elasticsearch。

查看elasticsearch状态,或使用浏览器打开http://172.21.3.195:9200

# curl http://172.21.3.195:9200 {
"name" : "Ant-Man",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "g8gomxXfRzq-nwdTP9glng",
"version" : {
"number" : "2.3.5",
"build_hash" : "fcbb46dfd45562a9cf00c604b30849a6dec6b017",
"build_timestamp" : "2017-01-03T11:33:16Z",
"build_snapshot" : false,
"lucene_version" : "5.5.2"
},
"tagline" : "You Know, for Search"
}


3.安装插件

安装head插件

# /usr/local/src/elasticsearch-2.3.5/bin/plugin install mobz/elasticsearch-head


安装完成后重启,使用浏览器打开http://172.21.3.195:9200/_plugin/head/ 进行查看

安装ik分词器

Github进行下载安装1.9.5版本.并解压

cd /usr/local/src/
tar -zxvf /usr/local/src/elasticsearch-analysis-ik-1.9.5.tar.gz


elasticsearch 要使用 ik,就要先构建 ik 的 jar包,我们使用 maven 包管理工具。对 maven 的下载和环境变量的配置此处也不做说明了。

我假定你已经做好配置了,先查看下我的maven

#  mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/apache-maven-3.3.9
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /usr/local/jdk1.8/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-696.3.2.el6.x86_64", arch: "amd64", family: "unix"


构建ik的jar包

cd /usr/local/src/elasticsearch-analysis-ik-1.9.5/
mvn package


第一次构建可能会有点慢

unzip -d /usr/local/src/elasticsearch-analysis-ik-1.9.5/plugins/ik /usr/local/src/elasticsearch.2.3.5/target/releases/elasticsearch-analysis-ik-1.8.0.zip


重启 elasticsearch,然后就可以使用ik分词进行查看。

ik 带有两个分词器

ik_max_word :会将文本做最细粒度的拆分;尽可能多的拆分出词语

ik_smart:会做最粗粒度的拆分;已被分出的词语将不会再次被其它词语占有

# curl -XGET 'http://172.21.3.218:9200/_analyze?pretty&analyzer=ik_max_word' -d '今天天气真好,有太阳!'
{
"tokens" : [ {
"token" : "今天天气",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
}, {
"token" : "今天",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
}, {
"token" : "天天",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 2
}, {
"token" : "天气",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 3
}, {
"token" : "真好",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 4
}, {
"token" : "有",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 5
}, {
"token" : "太阳",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 6
} ]
}

# curl -XGET 'http://172.21.3.218:9200/_analyze?pretty&analyzer=**ik_smart**' -d '今天天气真好,有太阳!'
{
"tokens" : [ {
"token" : "今天天气",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
}, {
"token" : "真好",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
}, {
"token" : "有",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 2
}, {
"token" : "太阳",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 3
} ]
}


错误示例:启动时提示错误(版本不兼容,重新下载对应版本就行)

Exception in thread "main" java.lang.IllegalArgumentException: Plugin [analysis-ik] is incompatible with Elasticsearch [2.2.1]. Was designed for version [2.3.5]
at org.elasticsearch.plugins.PluginInfo.readFromProperties(PluginInfo.java:118)
at org.elasticsearch.plugins.PluginsService.getPluginBundles(PluginsService.java:378)
at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:128)
at org.elasticsearch.node.Node.<init>(Node.java:146)
at org.elasticsearch.node.Node.<init>(Node.java:128)
at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:145)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:285)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
Refer to the log for complete error details.


4.安装elasticsearch-cluster

好了,我们来进行集群的搭建配置。

先来了解下我们接下来搭建elasticsearch-cluster 信息:

主节点从节点从节点
172.21.3.108172.21.3.109172.21.3.110
不存储数据存储数据存储数据
分别在172.21.3.108 109 110 三台主机上面配置安装elasticsearch。

我们先来修改172.21.3.108主机里主节点的配置文件

vim /usr/local/src/elasticsearch.2.3.5/config/elasticsearch.yml


基础的master配置文件如下:

# ---------------------------------- 集群 -----------------------------------
# 集群名称:
cluster.name: mycluster
# ------------------------------------ 节点 ------------------------------------
# 节点名称:
node.name: es-01
node.master: true
node.data: false
# 为节点添加自定义属性,如机架:
# node.rack: r1
#定义索引分片的数量
index.number_of_shards: 5
#定义副本的数量
index.number_of_replicas: 1
# ----------------------------------- 路径 ------------------------------------
# 存放数据的目录 (多个目录用逗号分隔):
# path.data: /path/to/data
# 日志文件目录:
# path.logs: /path/to/logs
# ----------------------------------- 内存 -----------------------------------
# 启动时锁定内存:
# bootstrap.mlockall: true
# ---------------------------------- 网络 -----------------------------------
# 设置绑定地址到指定IP (IPv4 or IPv6):
network.host: 172.21.3.108
# 设置自定义 HTTP 端口:
http.port: 9200
# 集群内部通信端口:
transport.tcp.port: 9300
# --------------------------------- 节点发现 ----------------------------------
# 当新节点加入时,传递一个主机的初始化列表以完成节点发现:
# 默认主机列表为 ["127.0.0.1", "[::1]"]
discovery.zen.ping.unicast.hosts: ["172.21.3.108:9300","172.21.3.109:9300","172.21.3.110:9300"]
# 通过配置大多数节点缓解脑裂现象发生 (数量: 节点总数量 / 2 + 1):
# discovery.zen.minimum_master_nodes: 2
discovery.zen.ping.multicast.enabled: false
# ---------------------------------- 网关 -----------------------------------
# 当整个集群重新启动后, 只有 N 个节点启动了, 集群才会恢复,否则将阻塞:
# gateway.recover_after_nodes: 2
# ---------------------------------- 其他 -----------------------------------
# 禁止在一个系统上启动多个节点:
# node.max_local_storage_nodes: 1
# 当删除 index 是必需显式名称:
# action.destructive_requires_name: true
# 配置elasticsearch的分词。
index:
analysis:
tokenizer:
my_pinyin:
type: pinyin
first_letter: prefix
padding_char: ''
pinyin_first_letter:
type: pinyin
first_letter: only
mmseg_maxword:
ty
dad7
pe: mmseg
seg_type: max_word
mmseg_complex:
type: mmseg
seg_type: complex
mmseg_simple:
type: mmseg
seg_type: simple
semicolon_spliter:
type: pattern
pattern: ";"
pct_spliter:
type: pattern
pattern: "[%]+"
ngram_1_to_2:
type: nGram
min_gram: 1
max_gram: 2
ngram_1_to_3:
type: nGram
min_gram: 1
max_gram: 3
filter:
ngram_min_3:
max_gram: 10
min_gram: 3
type: nGram
ngram_min_2:
max_gram: 10
min_gram: 2
type: nGram
ngram_min_1:
max_gram: 10
min_gram: 1
type: nGram
min2_length:
min: 2
max: 4
type: length
min3_length:
min: 3
max: 4
type: length
pinyin_first_letter:
type: pinyin
first_letter: only
analyzer:
lowercase_keyword:
type: custom
filter:
- lowercase
tokenizer: standard
lowercase_keyword_ngram_min_size1:
type: custom
filter:
- lowercase
- stop
- trim
- unique
tokenizer: nGram
lowercase_keyword_ngram_min_size2:
type: custom
filter:
- lowercase
- min2_length
- stop
- trim
- unique
tokenizer: nGram
lowercase_keyword_ngram_min_size3:
type: custom
filter:
- lowercase
- min3_length
- stop
- trim
- unique
tokenizer: ngram_1_to_3
lowercase_keyword_ngram:
type: custom
filter:
- lowercase
- stop
- trim
- unique
tokenizer: ngram_1_to_3
lowercase_keyword_without_standard:
type: custom
filter:
- lowercase
tokenizer: keyword
lowercase_whitespace:
type: custom
filter:
- lowercase
tokenizer: whitespace
ik:
alias:
- ik_analyzer
type: ik
ik_max_word:
type: ik
use_smart: true
ik_smart:
type: ik
use_smart: true
mmseg:
alias:
- mmseg_analyzer
type: mmseg
mmseg_maxword:
type: custom
filter:
- lowercase
tokenizer: mmseg_maxword
mmseg_complex:
type: custom
filter:
- lowercase
tokenizer: mmseg_complex
mmseg_simple:
type: custom
filter:
- lowercase
tokenizer: mmseg_simple
comma_spliter:
type: pattern
pattern: "[,|\\s]+"
pct_spliter:
type: pattern
pattern: "[%]+"
custom_snowball_analyzer:
type: snowball
language: English
simple_english_analyzer:
type: custom
tokenizer: whitespace
filter:
- standard
- lowercase
- snowball
edge_ngram:
type: custom
tokenizer: edgeNGram
filter:
- lowercase
pinyin_ngram_analyzer:
type: custom
tokenizer: my_pinyin
filter:
- lowercase
- nGram
- trim
- unique
pinyin_first_letter_analyzer:
type: custom
tokenizer: pinyin_first_letter
filter:
- standard
- lowercase
pinyin_first_letter_keyword_analyzer:
alias:
- pinyin_first_letter_analyzer_keyword
type: custom
tokenizer: keyword
filter:
- pinyin_first_letter
- lowercase
path_analyzer: #used for tokenize :/something/something/else
type: custom
tokenizer: path_hierarchy

#index.analysis.analyzer.default.type: mmseg
index.analysis.analyzer.default.type: ik
# rtf.filter.redis.host: 127.0.0.1
# rtf.filter.redis.port: 6379


再来修改主机172.21.3.109 110中从节点的配置文件时,只需要注意与master节点配置文件中的:

第3行集群名称要相同。

第6行各节点名称不能相同。

第7行从节点改为false。

第8行数据存储改为true。

5.启动elasticsearch-cluster

启动集群时应想启动主节点,再启动从节点。(否则的话在比较大的集群中可能会因为主节点没启动,而出现选举出来的新主节点,导致程序出错)

# /usr/local/src/elasticsearch.2.3.5/elastcisearch


启动完成后在浏览器输入http://172.21.3.195:9200/_plugin/head/ 就可以看到节点信息和状态了。

配置文件全参数详解

--------------------------------- Cluster ---------------------------------
#定义集群名称,默认是elasticsearch
#cluster.name: elasticsearch

--------------------------------- Node ---------------------------------
#定义此节点名称
#node.name: "node_1"
#此节点是否为master,master作用就是做协调,协调集群的状态,数据的读取时由集群的各个节点共同完成的,但是数据的修改只能master完成
#node.master: true
#此节点是否为子节点,功能就是存储数据,存储索引之类的
#node.data: true
#所用的架构类型
#node.rack: r1
# 默认情况下多个节点可以使用一个路径,一般为1就好,因为一般情况下一台机器只跑一个节点
#node.max_local_storage_nodes: 1
--------------------------------- Index ---------------------------------
#定义索引分片的数量
#index.number_of_shards: 5
#定义副本的数量
#index.number_of_replicas: 1

--------------------------------- Paths ---------------------------------
#定义配置文件路径的设置,默认为当前路径中的conf
#path.conf: /path/to/conf
#定义索引数据存储的路径
#path.data: /path/to/data
#path.data: /path/to/data1,/path/to/data2
#定义临时文件的路径
#path.work: /path/to/work
# Path to log files:
#定义日志文件的路径
#path.logs: /path/to/logs
#定义插件的位置
#path.plugins: /path/to/plugins

--------------------------------- Memory ---------------------------------
#es在内存不够jvm开启swapping的时候,表现的会很差,所以为了避免这个问题,将概述性设置为true,表示锁定es所使用的内存

--------------------------------- Network And HTTP ---------------------------------
#elasticsearch节点绑定的地址
#network.bind_host: 192.168.0.1
#elasticsearch和其他节点通信的地址,如果不设置的话 会自动获取
#network.publish_host: 192.168.0.1
#这个参数是用来同时设置bind_host和publish_host上面两个参数。
#network.host: 192.168.0.1

#设置节点之间通信的端口
#transport.tcp.port: 9300
#定义是否压缩tcp传输时的数据
#transport.tcp.compress: true
#定义http传输监听的端口
#http.port: 9200
#设置http交互中传输内容的最大长度
#http.max_content_length: 100mb
#是否开启http服务对外提供服务
#http.enabled: false

--------------------------------- Gateway ---------------------------------
#elasticsearch底层持久化,默认是走的本地,也可以设置为aws的s3
#gateway.type: local
#控制集群在达到多少个节点之后才会开始数据恢复,通过这个设置可以避免集群自动相互发现的初期,shard分片不全的问题,假如es集群内一共有5个节点,就可以设置为5,那么这个集群必须有5个节点启动后才会开始数据分片,如果设置为3,就有可能另外两个节点没存储数据分片
#gateway.recover_after_nodes: 1
#初始化数据恢复的超时时间,假如gateway.recover_after_nodes参数设置为5,就是5个节点全部启动后,再过5分钟开始数据恢复
#gateway.recover_after_time: 5m
#启动几个节点后开始数据恢复,假如gateway.recover_after_nodes这个参数设置为5,那么等到这5个节点全部启动后直接可以数据恢复,不用等待gateway.recover_after_time设置的时间
#gateway.expected_nodes: 2

--------------------------------- Recovery Throttling ---------------------------------
#设置一个节点的并发数量,
#cluster.routing.allocation.node_initial_primaries_recoveries: 4
#cluster.routing.allocation.node_concurrent_recoveries: 2
#恢复数据时,限制的宽带流量,如果是0就是无限制
#indices.recovery.max_bytes_per_sec: 20mb

#从其他分片恢复数据时,最大打开并发的值
#indices.recovery.concurrent_streams: 5

--------------------------------- Discovery ---------------------------------
#设置这个集群,有多少个节点有master候选资格,如果集群较大官方建议为(n/2)+1个
#discovery.zen.minimum_master_nodes: 1
#es集群中自动发现其他节点的超时时间,如果网络延迟较大,建议设置长一点,防止误判
#discovery.zen.ping.timeout: 3s

#是否打开多播协议,由于其安全性,其实现在基本的云服务(比如阿里云)是不支持多播的
#discovery.zen.ping.multicast.enabled: false
#设置集群中的Master节点的初始列表,可以通过这些节点来自动发现其他新加入集群的节点
#discovery.zen.ping.unicast.hosts:["10.0.0.209:9300","10.0.0.206:9300","10.0.0.208:9300"]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: