spring bootdiboJava技术升华

全文检索--ElasticSearch(二)

2019-09-25  本文已影响0人  无剑_君

一、ElasticSearch简介

  ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。ElasticSearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。
  Elasticsearch是一个可用于构建搜索应用的成品软件,它最早由Shay Bannon创建并于2010年2月发布。现在已经非常流行,成为商业解决方案之外一个开源的重要选择。
  Elasticsearch是一个基于Lucene的搜索服务器,提供一个分布式多用户能力的全文搜索引擎,基于RESTful web借口,使用Java开发,在Apache许可条款下开发源代码发布。做到准实时搜索、稳定、可靠、安装使用方便。
  Elasticsearch具有合理的默认配置,默认就是分布式工作模式,使用对等架构(P2P)避免单点故障(SPOF),易于横向扩展新的节点。此外由于没有对索引中的数据结构强行添加限制,从而允许用户调整现有数据模型。
官网:https://www.elastic.co/cn/

官网
Elasticsearch 2.x 版本与其他软件的兼容性:
地址:https://www.elastic.co/cn/support/matrix#matrix_compatibility

二、下载安装

  1. 下载
    https://www.elastic.co/cn/downloads/
    下载
    下载
    如果要使用SpringBoot开发,请参考以下网址:
    https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.RC3/reference/html/#preface.versions
    进行安装:
# 建议安装6.4.3版本,因为目前:SpringBoot2.1.17 支持最高6.4.3
[root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.3.1-linux-x86_64.tar.gz

6.4.3安装:
下载地址:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/zip-targz.html

# 下载
[root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.tar.gz
# 解压文件
[root@localhost ~]# tar -xzf elasticsearch-6.4.3.tar.gz -C /usr/elasticsearch/
# 切换目录
[root@localhost ~]# cd /usr/elasticsearch/elasticsearch-6.4.3

# 配置自动创建索引 elasticsearch.yml
[root@localhost elasticsearch-6.4.3]# vi config/elasticsearch.yml 
# 添加内容
network.host: 0.0.0.0
#action.auto_create_index: .monitoring*,.watches,.triggered_watches,.watcher-history*,.ml*
# 添加用户
[root@localhost bin]# useradd es
[root@localhost bin]# passwd es
# 设置权限
[root@localhost elasticsearch-6.4.3]#  chown -R es:es /usr/elasticsearch/elasticsearch-6.4.3

# 使用es用户运行
[es@localhost elasticsearch-6.4.3]$ ./bin/elasticsearch
# 后台运行
[es@localhost elasticsearch-6.4.3]$ ./bin/elasticsearch -d -p pid
# 关闭服务
[es@localhost elasticsearch-6.4.3]$ kill -9 8988

测试访问:
http://192.168.77.132:9200/

测试访问
  1. 解压(7.3.1安装)
    需安装JDK11
[root@localhost ~]# mkdir /usr/elasticsearch
[root@localhost ~]# tar -zxvf elasticsearch-7.3.1-linux-x86_64.tar.gz -C /usr/elasticsearch
[root@localhost ~]# cd /usr/elasticsearch/elasticsearch-7.3.1/
# 创建用户
[root@localhost bin]# useradd es
[root@localhost bin]# passwd es
# 设置权限
[root@localhost elasticsearch-7.3.1]# chown -R es:es /usr/elasticsearch/elasticsearch-7.3.1/

  1. yum安装(可按官网进行源配置)
# 配置更新源 /etc/yum.repos.d/elasticsearch.repo
[root@localhost ~]# vi /etc/yum.repos.d/elasticsearch.repo
# 内容
[elasticsearch-2.x]
name=Elasticsearch repository for2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
# 安装
[root@localhost ~]# yum install elasticsearch
# 添加开机自启
[root@localhost ~]# chkconfig --add elasticsearch

默认安装目录: /usr/share/elasticsearch/

三、服务启动

先使用以下命令启动:

[root@localhost elasticsearch-7.3.1]# ./bin/elasticsearch 

没有错误,后台启动:

# 启动(使用es用户启动,非root)
[es@localhost elasticsearch-7.3.1]$ ./bin/elasticsearch  -d
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
[2019-08-25 08:53:34,886][INFO ][node                     ] [Peter Petruski] version[2.4.6], pid[4448], build[5376dca/2017-07-18T12:17:44Z]
[2019-08-25 08:53:34,886][INFO ][node                     ] [Peter Petruski] initializing ...
[2019-08-25 08:53:35,754][INFO ][plugins                  ] [Peter Petruski] modules [reindex, lang-expression, lang-groovy], plugins [], sites []
[2019-08-25 08:53:35,862][INFO ][env                      ] [Peter Petruski] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [14gb], net total_space [16.9gb], spins? [unknown], types [rootfs]
[2019-08-25 08:53:35,863][INFO ][env                      ] [Peter Petruski] heap size [1015.6mb], compressed ordinary object pointers [true]
[2019-08-25 08:53:35,863][WARN ][env                      ] [Peter Petruski] max file descriptors [4096] for elasticsearch process likely too low, consider increasing to at least [65536]
[2019-08-25 08:53:38,389][INFO ][node                     ] [Peter Petruski] initialized
[2019-08-25 08:53:38,389][INFO ][node                     ] [Peter Petruski] starting ...
[2019-08-25 08:53:38,562][INFO ][transport                ] [Peter Petruski] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2019-08-25 08:53:38,567][INFO ][discovery                ] [Peter Petruski] elasticsearch/Xuv6H62NTJe2-tyvh0DzpA
[2019-08-25 08:53:41,768][INFO ][cluster.service          ] [Peter Petruski] new_master {Peter Petruski}{Xuv6H62NTJe2-tyvh0DzpA}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2019-08-25 08:53:41,857][INFO ][gateway                  ] [Peter Petruski] recovered [0] indices into cluster_state
[2019-08-25 08:53:41,861][INFO ][http                     ] [Peter Petruski] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2019-08-25 08:53:41,861][INFO ][node                     ] [Peter Petruski] started

# 关闭:
[es@localhost bin]$ ps -ef | grep elastic
[es@localhost bin]$ kill -9 37951

# 防火墙设置 
[root@localhost elasticsearch]# firewall-cmd --permanent --add-port=9200/tcp
success
# 重启防火墙
[root@localhost elasticsearch]# firewall-cmd --reload
# 测试访问
[es@localhost elasticsearch]$ curl http://localhost:9200
{
  "name" : "Evilhawk",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "UY2afaj-QaqfXb7tchOZ8w",
  "version" : {
    "number" : "2.4.6",
    "build_hash" : "5376dca9f70f3abef96a77f4bb22720ace8240fd",
    "build_timestamp" : "2017-07-18T12:17:44Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.4"
  },
  "tagline" : "You Know, for Search"
}

四、外网访问配置

修改 config 下 elasticsearch.yml 配置

[es@localhost elasticsearch]$ vi config/elasticsearch.yml 
# 内容
#远程访问IP 服务器的IP地址
 network.host: 0.0.0.0
# 设置为多个主机地址 可访问
network.bind_host: ["yourhost", "localhost"]
# 开启http访问
http.port: 9200

外网访问配置
浏览访问

五、Head插件

  elasticsearch-head是elastic search集群的一个web前端。
  源代码托管在github.com,地址是:https://github.com/mobz/elasticsearch-head

集群健康值的几种状态如下:
绿色,最健康的状态,代表所有的分片包括备份都可用
黄色,基本的分片可用,但是备份不可用(也可能是没有备份)
红色,部分的分片可用,表明分片有一部分损坏。此时执行查询部分数据仍然可以查到,遇到这种情况,还是赶快解决比较好
灰色,未连接到elasticsearch服务

  1. 安装
    在线安装,Elasticsearch 5.x, 6.x, and 7.x 不支持插件安装,需要独立安装。
[es@localhost elasticsearch]$ ./bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /usr/share/elasticsearch/plugins/head

  1. 浏览访问
    http://192.168.169.128:9200/_plugin/head/

    浏览访问
  2. 独立安装
    Elasticsearch 5.x, 6.x, and 7.x 不支持插件安装,需要独立安装。

# 下载node.js
[root@localhost elasticsearch-7.3.1]# wget https://cdn.npm.taobao.org/dist/node/v12.10.0/node-v12.10.0-linux-x64.tar.xz
# 创建目录
[root@localhost elasticsearch-7.3.1]# mkdir /usr/local/node
# 解压安装包
[root@localhost elasticsearch-7.3.1]# tar -xvf node-v12.10.0-linux-x64.tar.xz -C /usr/local/node
# 验证
[root@localhost bin]# cd /usr/local/node/node-v12.10.0-linux-x64/
[root@localhost node-v12.10.0-linux-x64]# ./bin/node -v
v12.10.0
# 配置node和npm。注意node所在目录: /usr/local/node/node-v12.10.0-linux-x64/
# 创建硬链接
[root@localhost node-v12.10.0-linux-x64]# ln -s /usr/local/node/node-v12.10.0-linux-x64/bin/node /usr/bin/node
[root@localhost node-v12.10.0-linux-x64]# node -v
v12.10.0
[root@localhost node-v12.10.0-linux-x64]# ln -s /usr/local/node/node-v12.10.0-linux-x64/bin/npm /usr/bin/npm
[root@localhost node-v12.10.0-linux-x64]# npm -v
6.10.3

# 注册模块镜像 
[root@localhost elasticsearch-head]# npm set registry https://registry.npm.taobao.org

# 下载插件(一定要下载到es安装目录)
[root@localhost elasticsearch-7.3.1]# git clone git://github.com/mobz/elasticsearch-head.git
# 安装
[root@localhost elasticsearch-7.3.1]# cd /usr/elasticsearch/elasticsearch-7.3.1/elasticsearch-head/
[root@localhost elasticsearch-head]# npm install

# 配置elasticsearch,允许head插件访问
# 进入elasticsearch\config目录 打开 elasticsearch.yml
[root@localhost elasticsearch-7.3.1]# vi config/elasticsearch.yml 
# 添加内容
http.cors.enabled: true
http.cors.allow-origin: "*"

# 启动
[root@localhost elasticsearch-head]#  npm run start
# 后台运行
[root@localhost elasticsearch-head]# nohup npm run start &

切记,要重启下ES服务。

配置内容
  1. 访问测试
    http://192.168.77.132:9100


    访问测试

常见错误:

  1. npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference
    需更新ssl版本:
[root@localhost elasticsearch-head]# yum update openssl -y
[root@localhost elasticsearch-head]#  openssl version

  1. ENOENT: no such file or directory, open '/usr/elasticsearch/elasticsearch-7.3.1/package.json'
[root@localhost elasticsearch-head]# npm install

六、Marvel插件

  Marvel插件:在簇中从每个节点汇集数据。这个插件必须每个节点都得安装。
  Marvel是Elasticsearch的管理和监控工具,在开发环境下免费使用。它包含了一个叫做Sense的交互式控制台,使用户方便的通过浏览器直接与Elasticsearch进行交互。

  1. 安装
[es@localhost elasticsearch]$ ./bin/plugin install license
-> Installing license...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/license/2.4.6/license-2.4.6.zip ...
Downloading ...........................DONE
Verifying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/license/2.4.6/license-2.4.6.zip checksums if available ...
Downloading .DONE
Installed license into /usr/share/elasticsearch/plugins/license

[es@localhost elasticsearch]$ ./bin/plugin install marvel-agent
-> Installing marvel-agent...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/marvel-agent/2.4.6/marvel-agent-2.4.6.zip ...
Downloading ....................DONE
Verifying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/marvel-agent/2.4.6/marvel-agent-2.4.6.zip checksums if available ...
Downloading .DONE
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.lang.RuntimePermission setFactory
* javax.net.ssl.SSLPermission setHostnameVerifier
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
Installed marvel-agent into /usr/share/elasticsearch/plugins/marvel-agent

  1. 配置
    配置marvel.agent.exporters ( elasticsearch.yml)
[es@localhost elasticsearch]$ vi config/elasticsearch.yml 

# 文件最后添加(用于集群es-mon1与es-mon2为主机名)
marvel.agent.exporters:
  id1:
    type: http
    host: ["http://es-mon1:9200", "http://es-mon2:9200"]

七、IK分词插件

ElasticSearch 默认采用分词器, 单个字分词 ,效果很差。
  Elasticsearch-analysis-ik,这是一个将Lucence IK分词器集成到elasticsearch的ik分词器插件,并且支持自定义的词典。
地址:https://github.com/medcl/elasticsearch-analysis-ik

  1. 安装
    elasticsearch7.3版本已经不需要额外安装中文分词插件。
# 安装插件
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-plugin  install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
# 重启elasticsearch
  1. 测试IK分词
    IK分词安装后有三种分词策略:ik、ik_max_word和ik_smart。ik和ik_max_word分词效果相同,对输入文本根据词典穷尽各种分割方法是细力度分割策略。ik_smart会对输入文本根据词典以及歧义判断等方式进行一次最合理的粗粒度分割。可以在Terml中使用curl查看分词效果。

elasticsearch7.3版本已经不需要额外安装中文分词插件。


分词测试
分词测试

九、Shield插件

  Shield是Elastic公司为ElasticSearch开发的一个安全插件。在安装此插件后,Shield会拦截所有对ElasticSearch的请求,并加上认证与加密,保障ElasticSearch及相关系统的安全性。
  Shield是商业插件,需要ElasticSearch的商业许可。第一次安装许可的时候,会提供30天的免费试用权限。30天后,Shield将会屏蔽cluster health, cluster stats, index stats这几个API,其余功能不受影响。
https://www.elastic.co/cn/what-is/elastic-stack-security
elasticsearch 5.0以下的版本要用到权限控制的话需要使用shield。
下载地址: https://www.elastic.co/downloads/shield
5.0以上的版本则可以使用X-Pack,shield现在只是x-pack的一部分。

  1. shield安装
[root@localhost elasticsearch]# ./bin/plugin install shield

十、安全框架x-pack

  X-Pack是一个Elastic Stack扩展,将安全性,警报,监控,报告,机器学习和图形功能捆绑到一个易于安装的软件包中。要访问此功能,您必须 在Elasticsearch中安装X-Pack,要安装x-pack必须要和Elasticsearch的版本相匹配,如果您是在现有群集上首次安装X-Pack,则必须执行完整群集重新启动。安装X-Pack后,必须在群集中的所有节点上启用安全性和安全性才能使群集正常运行。
elasticsearch7 .1版本:基础安全免费。
官网:https://www.elastic.co/cn/downloads/x-pack

(一)安装配置

  1. 安装
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-plugin install x-pack

常见问题:
ERROR: this distribution of Elasticsearch contains X-Pack by default
默认已包含,无需安装。

  1. 启动x-pack功能
 # 注意修改elastic等账号的密码,elastic是登录es的最高权限账号
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-setup-passwords interactive  
 # 修改elasticsearch.yml配置:
[root@localhost elasticsearch-6.4.3]# vi config/elasticsearch.yml

# 添加如下2行,打开安全配置功能
xpack.security.enabled: true
# 启动SSH
# xpack.security.transport.ssl.enabled: true

查看序号: http://192.168.77.132:9200/_xpack/license

查看序号
ES安装x-pack后,默认的账户有三个,如下:
账户名 默认密码 权限
elastic changeme
kibana changeme
logstash_system changeme
curl -XPUT -u elastic 'http://localhost:9200/_xpack/security/user/elastic/_password' -d '{
  "password" : "yourpasswd"
}'s
curl -XPUT -u elastic 'http://localhost:9200/_xpack/security/user/kibana/_password' -d '{
  "password" : "yourpasswd"
}'

  1. 破解x-pack 否则报错:
It doesn't look like the X-Pack security feature is available on this Elasticsearch node.
Please check if you have installed a license that allows access to X-Pack Security feature.
  1. 密码设置
# 自动设置
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-setup-passwords auto
Initiating the setup of passwords for reserved users elastic,kibana,logstash_system,beats_system.
The passwords will be randomly generated and printed to the console.
Please confirm that you would like to continue [y/N]y


Changed password for user kibana
PASSWORD kibana = NVZBlTIlH7eb1gh0ldNO

Changed password for user logstash_system
PASSWORD logstash_system = UlGoGeLMoauElcK1Je9b

Changed password for user beats_system
PASSWORD beats_system = aVxjV7JcvZdZ8qRuTsgg

Changed password for user elastic
PASSWORD elastic = xW9dqAxThD5U4ShQV1JT

# 手动设置密码,交互式。
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-setup-passwords interactive

注意:设置密码进需要在集群中任意一个elasticsearch节点执行完成即可,如果执行第二次,将会给出如下错误提示,如果需要更新密码,则可以通过kibana上的Management > Users进行更新。

访问测试
访问测试
  1. elasticsearch-head访问
    elasticsearch.yml添加配置
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type

地址:http://192.168.77.132:9100/?auth_user=elastic&auth_password=xW9dqAxThD5U4ShQV1JT

elasticsearch-head访问

没有安装kibana时配置了,也不可,安装后好了,配置试用就可以了。
常见问题:

  1. Failed to authenticate user 'elastic' against http://192.168.77.132:9200/_xpack/security/_authenticate?pretty
    原因:已设置过密码,如果再次设置,请在kibana上的Management > Users进行更新。
Failed to authenticate user 'elastic' against http://192.168.77.132:9200/_xpack/security/_authenticate?pretty
Possible causes include:
 * The password for the 'elastic' user has already been changed on this cluster
 * Your elasticsearch node is running against a different keystore
   This tool used the keystore at /usr/elasticsearch/elasticsearch-6.4.3/config/elasticsearch.keystore

(二)破解

  1. 创建Java文件
    LicenseVerifier.java
package org.elasticsearch.license; 
import java.nio.*; import java.util.*; 
import java.security.*; 
import org.elasticsearch.common.xcontent.*; 
import org.apache.lucene.util.*; 
import org.elasticsearch.common.io.*; 
import java.io.*; 

public class LicenseVerifier { 
    public static boolean verifyLicense(final License license, final byte[] encryptedPublicKeyData) {
        return true; 
    } 
    
    public static boolean verifyLicense(final License license)     { 
        return true; 
    } 
}

XPackBuild.java

package org.elasticsearch.xpack.core;
import org.elasticsearch.common.io.*;
 import java.net.*;
 import org.elasticsearch.common.*;
 import java.nio.file.*;
 import java.io.*; 
 import java.util.jar.*; 
 public class XPackBuild { 
    public static final XPackBuild CURRENT;
    private String shortHash; 
    private String date; 
    @SuppressForbidden(reason = "looks up path of xpack.jar directly") static Path getElasticsearchCodebase() { 
        final URL url = XPackBuild.class.getProtectionDomain().getCodeSource().getLocation();
        try { return PathUtils.get(url.toURI()); }
        catch (URISyntaxException bogus) { 
            throw new RuntimeException(bogus); } 
        } 
        
    XPackBuild(final String shortHash, final String date) {
            this.shortHash = shortHash; 
            this.date = date; 
            } 
            
    public String shortHash() {
        return this.shortHash;
        } 
    public String date(){ 
        return this.date; 
        }
        
    static { 
        final Path path = getElasticsearchCodebase();
        String shortHash = null; 
        String date = null;
        Label_0157: { shortHash = "Unknown"; date = "Unknown"; 
    } 
    
    CURRENT = new XPackBuild(shortHash, date); 
    }
}

  1. 打包成class文件
    将刚创建的两个java包打包成class文件,我们需要做的就是替换这两个class文件(因里面需要引用到其他的jar,故需要用到javac -cp命令)
    elasticsearch安装目录为:
    /usr/elasticsearch/elasticsearch-6.4.3/lib/
[root@localhost new-x-pack]#  javac -cp "/usr/elasticsearch/elasticsearch-6.4.3/lib/elasticsearch-6.4.3.jar:/usr/elasticsearch/elasticsearch-6.4.3/lib/lucene-core-7.4.0.jar:/usr/elasticsearch/elasticsearch-6.4.3/modules/x-pack-core/x-pack-core-6.4.3.jar:/usr/elasticsearch/elasticsearch-6.4.3/lib/elasticsearch-core-6.4.3.jar" LicenseVerifier.java 

[root@localhost new-x-pack]#   javac -cp "/usr/elasticsearch/elasticsearch-6.4.3/lib/elasticsearch-6.4.3.jar:/usr/elasticsearch/elasticsearch-6.4.3/lib/lucene-core-7.4.0.jar:/usr/elasticsearch/elasticsearch-6.4.3/modules/x-pack-core/x-pack-core-6.4.3.jar:/usr/elasticsearch/elasticsearch-6.4.3/lib/elasticsearch-core-6.4.3.jar"  XPackBuild.java

# 查看生成
[root@localhost new-x-pack]#  ls *.class
LicenseVerifier.class  XPackBuild.class

  1. 解压覆盖
# 拷贝/x-pack-core/x-pack-core-6.4.3.jar 到当前目录
[root@localhost new-x-pack]#  cp -a /usr/elasticsearch/elasticsearch-6.4.3/modules/x-pack-core/x-pack-core-6.4.3.jar .
# 解压文件(解压后在org 目录中)
[root@localhost new-x-pack]#  jar -xvf x-pack-core-6.4.3.jar 
# 拷贝文件到目录,为了打包
[root@localhost new-x-pack]#  cp -a LicenseVerifier.class org/elasticsearch/license/
cp:是否覆盖"org/elasticsearch/license/LicenseVerifier.class"? y
[root@localhost new-x-pack]# cp -a XPackBuild.class org/elasticsearch/xpack/core/
cp:是否覆盖"org/elasticsearch/xpack/core/XPackBuild.class"? y
# 删除旧包
[root@localhost elasticsearch-6.4.3]# rm x-pack-core-6.4.3.jar 
rm:是否删除普通文件 "x-pack-core-6.4.3.jar"?y
# 删除文件
[root@localhost new-x-pack]# rm LicenseVerifier.java XPackBuild.java 
rm:是否删除普通文件 "LicenseVerifier.java"?y
rm:是否删除普通文件 "XPackBuild.java"?y

# 在新目录中重新打包
[root@localhost new-x-pack]# jar -cvf x-pack-core-6.4.3.jar *

# 覆盖旧包
[root@localhost new-x-pack]# cp -a x-pack-core-6.4.3.jar /usr/elasticsearch/elasticsearch-6.4.3/modules/x-pack-core/
cp:是否覆盖"/usr/elasticsearch/elasticsearch-6.4.3/modules/x-pack-core/x-pack-core-6.4.3.jar"? y
# 使用es用户重启elasticsearch(启动校验)
[root@localhost elasticsearch-6.4.3]# sudo -u es ./bin/elasticsearch
[root@localhost elasticsearch-6.4.3]# sudo -u es ./bin/elasticsearch -d

  1. 查询版本license
    http://192.168.77.132:9200/_xpack/license
查询版本
去官网申请license证书:
地址:https://license.elastic.co/registration

主要修改这几个地方
1.“type”:“basic” 替换为 “type”:"platinum" # 基础版变更为铂金版
2.“expiry_date_in_millis”:1561420799999 替换为 “expiry_date_in_millis”:3107746200000# 1年变为50年

  1. 上传许可:
    新建一个license.json文件,加入以下内容:
{"license":{"uid":"280afc36-d654-4b04-884a-e51da9afb978","type":"platinum","issue_date_in_millis":1526083200000,"expiry_date_in_millis":2524579200999,"max_nodes":1000,"issued_to":"shi zhenzhou (czbk)","issuer":"Web Form","signature":"AAAAAwAAAA0aa2PBU7OqnL8KuGncAAABmC9ZN0hjZDBGYnVyRXpCOW5Bb3FjZDAxOWpSbTVoMVZwUzRxVk1PSmkxaktJRVl5MUYvUWh3bHZVUTllbXNPbzBUemtnbWpBbmlWRmRZb25KNFlBR2x0TXc2K2p1Y1VtMG1UQU9TRGZVSGRwaEJGUjE3bXd3LzRqZ05iLzRteWFNekdxRGpIYlFwYkJiNUs0U1hTVlJKNVlXekMrSlVUdFIvV0FNeWdOYnlESDc3MWhlY3hSQmdKSjJ2ZTcvYlBFOHhPQlV3ZHdDQ0tHcG5uOElCaDJ4K1hob29xSG85N0kvTWV3THhlQk9NL01VMFRjNDZpZEVXeUtUMXIyMlIveFpJUkk2WUdveEZaME9XWitGUi9WNTZVQW1FMG1DenhZU0ZmeXlZakVEMjZFT2NvOWxpZGlqVmlHNC8rWVVUYzMwRGVySHpIdURzKzFiRDl4TmM1TUp2VTBOUlJZUlAyV0ZVL2kvVk10L0NsbXNFYVZwT3NSU082dFNNa2prQ0ZsclZ4NTltbU1CVE5lR09Bck93V2J1Y3c9PQAAAQAdqCi5COi5J2pKpGQUrKDVGZ3xixzTW3KTImsYnnP5yAlObcgfvVYsmYZlDGv2AmSrUCV5iUAzWABLRMo7rnQwfvlPIs8XvKIzPaND0i+Uc5SkT1+oA56PzRFrufZwY/H4yqOaKe4JvE5bVUqwngQD07FHyM2o1i3lOc3NaR1GPR0AoCGQKUQr2NXWf6YwUi/scQh+JzHbWEiIzwYgSOAnIU7wg8kK9uF5bTuEK4LiCWGX8PDYQdoJp4Qm1PUH3l7YdWbfwGWSqe/N/S4iKbUKesogHkiVRc0A0sIaAM6rZZ6Go9ozKWjZ4n0Rd2hFs21wF80GTrQPU6T8/oTXMQJL","start_date_in_millis":1526083200000}}
# 上传许可
[root@localhost kibana-6.4.3-linux-x86_64]# curl -XPUT ‘http://192.168.77.132:9200/_license’ -d @license.json

查看许可:


查看许可

十、常见错误:

  1. Likely root cause: java.nio.file.NoSuchFileException: /usr/share/elasticsearch/config
    直接在安装目录里去启动elasticsearch的话,elasticsearch是不会去/etc找配置文件的,elasticsearch只会在当前目录找config文件夹,如果安装成service的形式应该是可以找到配置文件。
 cp -r /etc/elasticsearch /usr/share/elasticsearch/config
  1. Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.
    原因:因为安全问题elasticsearch不让用root用户直接运行,所以要创建新用户
    解决:创建一个单独的用户用来运行ElasticSearch
    解决方案:
    新建用户:
[root@localhost bin]# useradd es
[root@localhost bin]# passwd es

更改用户 es 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
创建目录:

[root@localhost bin]# mkdir -p /export/servers/data
[root@localhost bin]# mkdir -p /export/servers/logs
# 添加权限
[root@localhost ~]#  chown -R es:es /usr/elasticsearch/elasticsearch-7.3.1/
[root@localhost ~]# mkdir -p /export/servers/data
[root@localhost ~]# mkdir -p /export/servers/logs
[root@localhost bin]# chown -R es:es /export/servers/data
[root@localhost bin]# chown -R es:es /export/servers/logs

切换es用户命令:su es
启动:

[es@localhost bin]$ ./elasticsearch
  1. Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/config
    原因:当前用户没有执行权限
    解决方法: chown linux用户名 elasticsearch安装目录 -R
[es@localhost elasticsearch]$ su root
密码:
[root@localhost elasticsearch]# chown -R es:es /usr/share/elasticsearch/

  1. OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N

在虚拟机的设置中,将处理器的处理器核心数量改成2,重新执行启动命令后,能够正常运行。若还是未能执行成功,可进一步将处理器数量也改成2。


修改CPU数量
  1. future versions of Elasticsearch will require Java 11; your Java version from [/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre] does not meet this requirement
    原因:必须使用java 11版本。

  2. bash: ./bin/elasticsearch: 权限不够

[root@localhost ~]# chmod 755 /usr/elasticsearch/elasticsearch-7.3.1/bin

  1. ERROR: [3] bootstrap checks failed
    [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
    [2]: max number of threads [3802] for user [es] is too low, increase to at least [4096]
    [3]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
解决:切换到root用户,编辑limits.conf 添加类似如下内容
[root@localhost bin]# vi /etc/security/limits.conf 
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
解决:切换到root用户,进入limits.d目录下修改配置文件。
vi /etc/security/limits.d/90-nproc.conf 
修改如下内容:
* soft nproc 1024
#修改为
* soft nproc 2048
解决:切换到root用户修改配置sysctl.conf
[root@localhost bin]# vi /etc/sysctl.conf 
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p

说明:* 代表针对所有用户
noproc 是代表最大进程数
nofile 是代表最大文件打开数

  1. the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
    修改:elasticsearch.yml
    添加:
cluster.initial_master_nodes: ["node-1"]

保留一个节点。

  1. SpringBoot启动使用elasticsearch启动异常:java.lang.IllegalStateException: Received message from unsupported version: [6.4.3] minimal compatible version is: [6.8.0]
    表示从不支持的版本接收到的消息:[6.4.3]最小兼容版本是:[6.8.0]
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.M5</version>
        <relativePath/>
 </parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
6.4.3

7.3.1必须使用:SpringBoot 2.2.0.M5版本

  1. OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
    解决:
    更新JDK版本为11。
# 安装JDK
[root@localhost elasticsearch-7.3.1]# yum install -y java-11-openjdk java-11-openjdk-devel
# 默认安装路径
[root@localhost elasticsearch-7.3.1]# ls /usr/lib/jvm
# 使用alternatives切换Java版本
[root@localhost alternatives]# alternatives --config java  

共有 2 个提供“java”的程序。

  选项    命令
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre/bin/java)
   2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.4.11-1.el7_7.x86_64/bin/java)

按 Enter 保留当前选项[+],或者键入选项编号:2
[root@localhost alternatives]# java -version
openjdk version "11.0.4" 2019-07-16 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.4+11-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.4+11-LTS, mixed mode, sharing)

# 卸载JDK1.8
[root@localhost elasticsearch-7.3.1]# yum remove java-1.8.0-openjdk* -y


  1. io.netty.handler.codec.DecoderException: javax.net.ssl.SSLHandshakeException: no cipher suites in common
    如果是6.x 的版本安装 xpack 强制要 SSL 加密传输。
    配置证书:
    1)生成证书
 # 默认直接点击进行下一步,密码也可以不输,全部回车
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-certutil ca

 # 默认直接点击进行下一步,密码也可以不输,全部回车
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

这时在es目录下会生成elastic-certificates.p12、elastic-stack-ca.p12

证书
2)
# 创建目录:
[root@localhost elasticsearch-6.4.3]# mkdir config/certs/
# 拷贝证书
[root@localhost elasticsearch-6.4.3]# mv elastic-certificates.p12 config/certs/
# 授权给es用户
[root@localhost elasticsearch-6.4.3]# chown -R es:es /usr/elasticsearch/elasticsearch-6.4.3/

# 开启x-pack验证
xpack.security.enabled: true
# 开启ssl
xpack.security.transport.ssl.enabled: true
# 配置证书
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
 # 设置内置用户elastic的密码(会提示输入密码,这个就是elastic用户的密码)
[root@localhost elasticsearch-6.4.3]# ./bin/elasticsearch-keystore add bootstrap.password
Enter value for bootstrap.password: 
# 重新启动es,设置内置用户密码

上一篇下一篇

猜你喜欢

热点阅读