一、定义  

    Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。  但是,Lucene只是一个库。想要使用它,你必须使用Java来作为开发语言并将其直接集成到你的应用中,更糟糕的是,Lucene非常复杂,你需要深入了解检索的相关知识来理解它是如何工作的。 Elasticsearch也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。  不过,Elasticsearch不仅仅是Lucene和全文搜索,我们还能这样去描述它:  

  1. 分布式的实时文件存储,每个字段都被索引并可被搜索;  

  2. 分布式的实时分析搜索引擎;  可以扩展到上百台服务器,处理PB级结构化或非结构化数据;  

  3. 所有的这些功能被集成到一个服务里面,你的应用可以通过简单的RESTful API、各种语言的客户端甚至命令行与之交互。


二、环境信息

centos7.4.1708

elasticsearch6.4.2

java1.8.0_181


三、安装java依赖

打开网址https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下载64位版本

cd /home
wget https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

tar -zxvf jdk-8u181-linux-x64.tar.gz\?AuthParam\=1539229632_04ab3f58562c9c9d93d12eeae8203e9e

mv /home/jdk1.8.0_181/ /usr/local/java
cd /usr/local/java
./java -version

将java添加到环境变量中

vi /etc/profile 
#文件末尾加入
JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

使系统配置立即生效

source /etc/profile

四、安装elasticsearch

官网找到下载页面https://www.elastic.co/cn/downloads/elasticsearch

cd /home
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.2.tar.gz

tar -zxvf elasticsearch-6.4.2.tar.gz
mv elasticsearch-6.4.2.tar.gz /usr/local/elasticsearch

由于elasticsearch不允许root账户运行,所以需要为elasticsearch设置运行账户

groupadd elsearch # 添加用户组
useradd elsearch -g elsearch -p elasticsearch # 添加用户
passwd elsearch  # 设置密码

配置elasticsearch

cd /usr/local/elasticsearch/
vi config/elasticsearch.yml

>bootstrap.memory_lock: false
>bootstrap.system_call_filter: false
>http.port: 9200
>network.host: 0.0.0.0  # 找到该项,设置为可被外部访问的ip

更改目录所有者

chown -R elsearch:elsearch /usr/local/elasticsearch/

切换运行账户,试运行

su elsearch
./bin/elasticsearch

启动的时候可能出现的错误

1、java.nio.file.AccessDeniedException: /usr/local/elasticsearch/config/elasticsearch.keystore

chown -R elsearch:elsearch /usr/local/elasticsearch

2、max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

vi /etc/security/limits.d/20-nproc.conf
elsearch soft nofile 65536
elsearch hard nofile 131072
elsearch soft nproc 4096
elsearch hard nproc 4096

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

vi /etc/sysctl.conf 
vm.max_map_count=655360
sysctl -p

后台运行命令

./bin/elasticsearch -d

关闭

ps aux |grep elastic
kill -9 xxx

查看网络状态

netstat -anp|grep java

将9200、9300的端口开放,以便外部访问

su root
sudo firewall-cmd --zone=public --add-port=9200/tcp --permanent
sudo firewall-cmd --zone=public --add-port=9300/tcp --permanent
sudo firewall-cmd --reload

浏览器输入http://xxxxxx:9200即可出现elasticsearch状态信息

3.png

五、安装辅助工具elasticsearch-head

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

su root
cd /home
wget -c --no-check-certificate https://github.com/mobz/elasticsearch-head/archive/master.zip

这里我们还需要安装nodejs,为head插件提供支持 https://nodejs.org/en/download/

wget https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.xz
tar -Jxvf node-v8.12.0-linux-x64.tar.xz
mv node-v8.12.0-linux-x64 /usr/local/
cd /usr/local
mv node-v8.12.0-linux-x64 node

将node加入环境变量中

vi /etc/profile
>NODE_HOME=/usr/local/node
#整合java环境变量和node环境变量
>export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$NODE_HOME/bin

使配置立即生效

source /etc/profile
node -v

进入head目录进行安装与启动

cd /usr/local/elasticsearch-head
npm install phantomjs-prebuilt@2.1.16 --ignore-scripts
npm install

es-head使用的是grunt server作为服务器,而grunt server默认监听的是localhost,所以我们需要修改Gruntfile.js文件,增加一段配置,不然外部是无法访问的,在grunt.initConfig中添加

connect: {
        server: {
                options: {
                        hostname: '0.0.0.0',
                        port: 9100,
                        base: '.',
                        keepalive: true
                }
        }
},

es与es-head是两个独立的进程,当es-head访问es服务时,会存在一个跨域问题。所以我们需要修改es的配置文件,增加一些配置项来解决这个问题

vi /usr/local/elasticsearch/config/elasticsearch.yml
>http.cors.enabled: true
>http.cors.allow-origin: "*"

开放head端口

firewall-cmd --zone=public --add-port=9100/tcp --permanent
firewall-cmd --reload

启动head

#nohup npm run start&
npm run start

浏览器中预览 http://xxx:9100

1.png

六、分布式集群设定

我们以同一台机器为例设定集群

cd /home
tar -zxvf elasticsearch-6.4.2.tar.gz
mv elasticsearch-6.4.2 /usr/local/
cd /usr/local/
mv elasticsearch-6.4.2/ elasticsearch_slave_01

修改从节点配置

vi elasticsearch_slave_01/config/elasticsearch.yml
>cluster.name: es  #集群名
>node.name: slave01 #节点名
>network.host: 127.0.0.1
>http.port: 8200
>discovery.zen.ping.unicast.hosts: ["127.0.0.1"]#master主节点列表

设置从节点目录所有者

chown elsearch:elsearch elasticsearch_slave_01

开发从节点端口

firewall-cmd --zone=public --add-port=8200/tcp --permanent
firewall-cmd --reload

设置主节点

su elsearch
vi elasticsearch/config/elasticsearch.yml
>cluster.name: es
>node.name: master
>node.master: true

结束主节点,重新启动

ps aux |grep elastic
kill -9 xxx

su elsearch
./elasticsearch/bin/elasticsearch -d
./elasticsearch_slave_01/bin/elasticsearch -d

浏览器中查看状态

http://xxx:9200

head工具

http://xxx:9100/

2.png

容易出现的错误,同一台机器容易把已经运行过的elasticsearch文件夹直接拿过来复制,这样会导致下面的错误

[2018-10-15T15:27:19,285][INFO ][o.e.d.z.ZenDiscovery     ] [slave01] failed to send join request to master [{master}{DTOueMyARh2NkE6PZbNKJA}{4xViRJVISgu6B3x6NanvPg}{192.168.0.100}{192.168.0.100:9300}{ml.machine_memory=3974127616, ml.max_open_jobs=20, xpack.installed=true, ml.enabled=true}], reason [RemoteTransportException[[master][192.168.0.100:9300][internal:discovery/zen/join]]; nested: IllegalArgumentException[can't add node {slave01}{DTOueMyARh2NkE6PZbNKJA}{ZOZ0sEEzTlapOo-oM-eKfQ}{127.0.0.1}{127.0.0.1:9301}{ml.machine_memory=3974127616, ml.max_open_jobs=20, xpack.installed=true, ml.enabled=true}, found existing node {master}{DTOueMyARh2NkE6PZbNKJA}{4xViRJVISgu6B3x6NanvPg}{192.168.0.100}{192.168.0.100:9300}{ml.machine_memory=3974127616, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true} with the same id but is a different node instance]; ]

这个时候 我们需要把 elasticsearch/data目录下的所有内容删除重新运行即可。

为了更好的区分,最好在每一个节点中设置不同的transport.tcp.port,并在从节点中指明

discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300"]