CentOS7上安装配置破解Elasticsearch+Kibana 6.4.2-6.5.1全过程

最近正在学习服务器应用平台的搭建的相关知识。有幸从朋友与书上了解到Elastic套件的使用,我花了两天的时间把最新的套件部署在我的服务器上,中间踩了数不清的坑。我把整个过程都记录了下来与各位有需要的朋友们分享一下。

Update
经过测试该安装破解方案已兼容Elasticsearch6.4.2至Kibana6.5.1版本

环境说明
操作系统:CentOS 7.5 腾讯云公共镜像
最低配置:1核2G(容易崩溃)
建议配置:2核4G(比较稳定)
推荐配置:越强越好(有钱真好)
软件来源:官方yum源,官网网站:https://www.elastic.co/
本文中使用的代码方式已在腾讯云服务器实际测试过,安装过程采用yum安装,若用rpm安装应该差异不大。
最近一次成功配置服务的时间为:2018-10-22,若因为版本更新导致本文的配置方案有初入或者失效的请见谅。
若有配置维护等问题欢迎讨论(其实我也是刚刚入门),联系我:[email protected]
准备工作
首先你可以给自己的主机起一个好听的名字,方便在未来区分不同的主机
hostnamectl set-hostname xxx
最好把系统上已有的应用更新到最新版本
yum update -y
Elasticsearch需要Java 1.8.0_131或更高版本,方便起见可以直接安装OpenJDK,如果有特别需要的朋友也可以选择OracleJDK
yum install java-1.8.0* -y
从现在开始我们就要开始安装Elastic套件了,注意细节、提示与顺序

一、安装Elasticsearch 6.4.2
该过程的官方文档请参考如下链接
安装过程概览:https://www.elastic.co/products
Elasticsearch下载:https://www.elastic.co/downloads/elasticsearch
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/index.html

根据文档的指引有以下步骤:

导入GPG密钥避免在安装过程中提示密钥信任问题
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
在源目录/etc/yum.repos.d/中添加源elasticsearch.repo
可直接执行vim /etc/yum.repos.d/elasticsearch.repo,并在添加完成后保存
向elasticsearch.repo中写入源信息:
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
1
2
3
4
5
6
7
8
在源添加完成后更新源数据并应通过yum安装Elasticsearch
yum update -y && yum install elasticsearch -y
二、初步配置Elasticsearch 6.4.2
该过程的官方文档请参考如下链接
配置说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html

根据文档的提示,我提取出一些关键信息

默认设置已经足够入门使用,应该尽量少的修改配置文件
yum安装的Elasticsearch运行目录:/usr/share/elasticsearch/
yum安装的Elasticsearch配置文件目录:/etc/elasticsearch/
elasticsearch.yml 文件用于配置Elasticsearch
jvm.options 文件用于配置Elasticsearch JVM设置
log4j2.properties 文件用于配置Elasticsearch日志记录
你可能想修改:
需要修改主机IP为公网IP、内网IP或任意IP:network.host: 0.0.0.0
修改端口:http.port: 9200
数据存放位置:path.data: /var/lib/elasticsearch
日志存放位置:path.logs: /var/log/elasticsearch
修改运行时内存限制:见下文
程序自启动:systemctl enable elasticsearch.service
启动主程序:systemctl start elasticsearch.service
关于修改运行时内存
官方文档重点翻译:

您应该很少需要更改Java虚拟机(JVM)选项。如果需要修改,最可能的更改是设置堆大小。设置JVM选项(包括系统属性和JVM标志)的首选方法是通过jvm.options配置文件修改。
您最好将最小堆大小(Xms)和最大堆大小(Xmx)设置为彼此相等。
Elasticsearch可用的堆越多,它可用于缓存的内存就越多。但是请注意,过多的堆可能会使您的机器陷入长时间的垃圾收集暂停。
设置Xmx为不超过物理RAM的50%,以确保有足够的物理RAM留给内核文件系统缓存。

官方关于JVM的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/jvm-options.html
此配置文件的默认位置在/etc/elasticsearch/jvm.options,视情况修改Xms和Xms,过多或过少的占用内存可能会导致程序崩溃或无法启动。
示例值:

-Xms2g
-Xmx2g
1
2
尝试启动Elasticsearch
Elasticsearch 安装完成并初步配置后可以先不急着启动,可以使用命令systemctl start elasticsearch.service启动Elasticsearch。
若想测试并验证安装结果,可以启动后访问http://<host_ip>:<host_part>(默认地址http://域名或IP:9200)进行验证,注意避免因为本机IP配置错误而导致无法连接的情况。
如能正常访问将显示如下格式的json数据

{
"name" : "qEgqyT5",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "wow17Li0SK-hgw-bOszN9g",
"version" : {
"number" : "6.4.2",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "04711c2",
"build_date" : "2018-09-26T13:34:09.098244Z",
"build_snapshot" : false,
"lucene_version" : "7.4.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
三、安装kibana 6.4.2
该过程的官方文档请参考如下链接
Kibana下载:https://www.elastic.co/downloads/kibana
官方文档:https://www.elastic.co/guide/en/kibana/6.4/index.html

根据文档的指引有以下步骤:

通过yum安装Kibana:yum install kibana -y
配置文件修改:/etc/kibana/kibana.yml
# 本机IP根据个人需要填写公网IP、内网IP或任意IP
server.host: "0.0.0.0"

#配置Kibana所连接的ES集群链接
elasticsearch.url: "http://localhost:9200"
1
2
3
4
5
设置软件开机自启并启动程序
systemctl enable kibana.service
systemctl start kibana.service
1
2
尝试启动Kibana
Kibana启动前必须配置并启动Elasticsearch,否则网页上会出现错误提示。配置并启动后可通过http://<host_ip>:<host_part>访问Kibana,可以用浏览器进行访问。默认地址http://域名或IP:5601,效果如图。

四、Beats与Logstash安装
可以根据个人需要安装Beats与Logstash,通过上文中配置的yum进行安装,组建的配置普遍的都比较简单,一般都是设置主机地址和证书即可。

详细内容可参考官方文档:https://www.elastic.co/guide/en/beats/metricbeat/current/index.html
Beats简介:https://www.elastic.co/cn/products/beats
Logstash简介:https://www.elastic.co/cn/products/logstash

简单举例
安装metricbeat
yum install metricbeat -y
配置metricbeat
文件目录:/etc/metricbeat/metricbeat.yml
配置elasticsearch和kibana的主机地址并开启面板,修改内容:
output.elasticsearch:
hosts: ["xxx.xxx.xxx.xxx:9200"]

setup.kibana:
host: "xxx.xxx.xxx.xxx:5601"

setup.dashboards.enabled: true
1
2
3
4
5
6
7
开机启动和启动程序
设置开机启动:chkconfig --add metricbeat
手动启动程序:systemctl start metricbeat
五、设置x-pack
若想破解白金版服务,需要启动安全设置,也就是要在每个节点中安装证书

该过程的官方文档请参考如下链接
https://www.elastic.co/guide/en/elasticsearch/reference/current/configuring-security.html

在以下内容中我将主要陈述该版本的方法和之前版本的不同点,理由只简单陈述

安装x-pack
6.4版本不需要安装x-pack
因为在6.4的版本中x-pack已经是一个内置的组建了,不必像网上的其他教程那样自己安装x-pack了。
先进入试用模式
我遇到的很奇怪的问题,若先配置证书再设置密码就会导致Kibana无法连接到Elasticsearch,出现以下提示。
设置密码时:
Unexpected response code [403] from calling GET http://10.10.1.10:9200/_xpack/security/_authenticate?pretty
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.

ERROR: X-Pack Security is not available.
1
2
3
4
5
打开Kibana时会出现
Cannot connect to the Elasticsearch cluster currently configured for Kibana.
1
如图

为避免此问题,请先点击试用再配置x-pack的相关证书。


生成CA证书
路径陷阱要小心
网络上大部分教程所描述的certutil程序脚本,在路径/usr/share/elasticsearch/bin/x-pack/中。但是,在6.4中已经不可用 (文件存在但是运行时会提示不可用),新的CA证书生成程序在/usr/share/elasticsearch/bin/目录中,名为elasticsearch-certutil
我们可以进入/usr/share/elasticsearch/bin/目录执行如下命令来生成证书。
./elasticsearch-certutil ca --ca-dn "CN=WolfBolin Elatic CA" --out /etc/elasticsearch/certs/wolfbolin-elastic-ca.p12
内容和参数的含义可以自行百度,百度上的说明还是能看明白的。若需要使用TLS/SSL证书可以参考官方文档。
生成cert证书
同样的,可以在/usr/share/elasticsearch/bin/目录执行如下命令来生成证书。
./elasticsearch-certutil cert -ca /etc/elasticsearch/certs/wolfbolin-elastic-ca.p12 --out /etc/elasticsearch/certs/wolfbolin-elastic-certificates.p12
该证书生成程序会以交互式的方式完成证书的生成,建议cert证书生成时不带密码,以降低配置复杂性(输入密码时直接回车就好)
合理的保存证书
在保存证书时有两点需要注意:
cert证书中包含了CA证书的密码,所以不应被随意读取,注意权限的调整
也许是由于Java或程序自身的限制,Elasticsearch无法读取非配置目录/etc/elasticsearch/下的文件,所以你应该将cert证书储存在/etc/elasticsearch/certs/文件夹中,并给予适当的权限,我的设置是chmod 660 /etc/elasticsearch/certs/*
若访问权限不足会在日志中出现java.nio.file.AccessDeniedException:的报错提示
在Elasticsearch中配置证书文件
修改配置文件/etc/elasticsearch/elasticsearch.yml,在文件结尾添加:
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/wolfbolin-elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/wolfbolin-elastic-certificates.p12
1
2
3
4
注意:两个证书是一样的,都是生成的cert证书。证书的名字和路径别弄错了!!!
若证书有密码可参考文档https://www.elastic.co/guide/en/elasticsearch/reference/current/configuring-tls.html#tls-transport

配置完成后重启Elasticsearch
systemctl restart elasticsearch.service

若在重启过程中出现错误与导致不能启动的,可以分析日志/var/log/elasticsearch/elasticsearch.log查找原因。

阶段性成果
在配置了安全证书后,若尝试访问则会在网页上提示,此时Kibana是不可访问Elasticsearch的

Cannot connect to the Elasticsearch cluster currently configured for Kibana.
Refer to the Kibana logs for more details and refresh to try again.
1
2
设置用户密码
网上其他教程上使用的程序setup-passwords在6.4中已不可用(文件存在但是运行时会提示不可用),新的密码设置程序在/usr/share/elasticsearch/bin/目录中,名为setup-passwords
我们可以进入/usr/share/elasticsearch/bin/目录执行如下命令来生成密码。
./elasticsearch-setup-passwords auto(自动生成)或./elasticsearch-setup-passwords interactive(手动设置)
若是自动生成,过程提示如下,如报错请参考第二步。
[root@sbox-wolfbolin 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 = nEeVHfxms4Q4S6mWmzzH

Changed password for user logstash_system
PASSWORD logstash_system = zmb8xXkFk7KlLWYPHfO0

Changed password for user beats_system
PASSWORD beats_system = ejSOoRx87tx43IfokIot

Changed password for user elastic
PASSWORD elastic = RFWJ2dN0crlHk0ebUHN4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
该凭据是其他应用程序连接Elasticsearch的凭据,请保存好

将密码部署到应用
编辑Kibana配置文件/etc/kibana/kibana.yml
elasticsearch.username:"elastic"
elasticsearch.password:"RFWJ2dN0crlHk0ebUHN4"
1
2
此帐号密码是Kibana连接Elasticsearch的凭据,若使用kibana账户则无法链接。

将密码部署到收集器
以metricbeat为例,修改配置文件/etc/metricbeat/metricbeat.yml,安装如下格式修改相关字段,密码按照上述自己修改的密码填写:
output.elasticsearch:
hosts: ["myEShost:9200"]
username: "beats_system"
password: "ejSOoRx87tx43IfokIot"
setup.kibana:
host: "kibana_host:5601"
username: "kibana"
password: "nEeVHfxms4Q4S6mWmzzH"
1
2
3
4
5
6
7
8
重启应用以刷新配置:systemctl restart metricbeat

重启Kibana
重启应用以刷新配置:systemctl restart kibana.service
此时刷新网页即可看到Kibana已经需要帐号密码登录了,此时使用刚刚生成的kibana账户登录。

六、 破解x-pack
相关说明
不同于手工安装的x-pack,Elasticsearch6.4内包含的x-pack位于modules/x-pack-core中,即:/usr/share/elasticsearch/modules/x-pack-core/x-pack-core-6.4.2.jar文件。

反编译jar文件
在这个步骤中你完全可以跳过反编译的过程,直接使用我文章中给出的Java程序进行编译和替换。但如果有需要的可以该文件传出来并用luyten反编译软件对jar包进行反编译,luyten项目地址:https://github.com/deathmarine/Luyten

修改x-pack源码
我们重点关心项目中的两个文件

org.elasticsearch.license.LicenseVerifier.java
org.elasticsearch.xpack.core.XPackBuild.java
两个文件文件相较于之前的版本有一定的变化,但是不影响破解过程。原始文件我就不贴了,直接给出修改后的Java文件。你可以在本地新建一个同名的Java文件并将上面给出的代码拷贝到文件中,这样你就拥有了两个修改过的x-pack程序的Java源码文件。
org.elasticsearch.license.LicenseVerifier.java
package org.elasticsearch.license;

import java.nio.*;
import org.elasticsearch.common.bytes.*;
import java.security.*;
import java.util.*;
import org.elasticsearch.common.xcontent.*;
import org.apache.lucene.util.*;
import org.elasticsearch.core.internal.io.*;
import java.io.*;

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

public static boolean verifyLicense(final License license) {
return true;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
org.elasticsearch.xpack.core.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
编译java程序
你需要将这两个文件编译为class文件,编译依赖的文件在命令中已经给出。
编译LicenseVerifier.java
javac -cp "/usr/share/elasticsearch/modules/x-pack-core/*:/usr/share/elasticsearch/lib/*" LicenseVerifier.java
编译XPackBuild.java
javac -cp "/usr/share/elasticsearch/modules/x-pack-core/*:/usr/share/elasticsearch/lib/*" XPackBuild.java
提示:如果编译迟迟不能结束,你可能需要将Elasticsearch关闭再重新编译即可。
编译替换的过程需要关闭Elasticsearch。
获取源文件
在完成编译程序之后需要将编译好的程序添加到x-pack-core-6.4.2.jar文件中,我们可以采用先解压再替换在压缩的方式构建新的jar文件。进入文件所在目录,备份文件,拷贝出来
cd /usr/share/elasticsearch/modules/x-pack-core/
cp x-pack-core-6.4.2.jar x-pack-core-6.4.2.jar.bak
cp x-pack-core-6.4.2.jar /home/x-pack-core-6.4.2.jar
1
2
3
解压jar文件
解压jar文件:jar -xvf x-pack-core-6.4.2.jar该命令会解压到当前目录
unzip x-pack-core-6.4.2.jar -d ./x-pack-core-6.4.2该命令可以指定目录
替换class文件
将刚刚编译好的破解的class替换到相同的位置中
cp LicenseVerifier.class ./x-pack-core-6.4.2/org/elasticsearch/license/
cp XPackBuild.class ./x-pack-core-6.4.2/org/elasticsearch/xpack/core/
重新打包jar包
压缩jar文件:jar -cvf x-pack-core-6.4.2.crack.jar -C x-pack-core-6.4.2/ . (不可忽略那个英文句号)
替换x-pack文件
将我们生成的被破解的jar替换到Elasticsearch的目录中:
cp x-pack-core-6.4.2.crack.jar /usr/share/elasticsearch/modules/x-pack-core/x-pack-core-6.4.2.jar
另外需要注意需要替换集群中所有的x-pack-core-6.4.2.jar
重启Elasticsearch
systemctl restart elasticsearch.service
七、 升级为铂金版
申请许可证
在官网上申请一个许可证:https://register.elastic.co/marvel_register
新申请的许可证都是普通的版本,我们需要修改一下文件中的信息让软件认为我们是铂金版。而且因为我们破解了验证证书的jar文件,所以软件无法验证我们的证书是不是真的。
普通的证书内容如下:
{
"license": {
"uid": "c6570128-85c2-4f72-8d8f-b1425455b9ee",
"type": "basic",
"issue_date_in_millis": 1540080000000,
"expiry_date_in_millis": 1571702399999,
"max_nodes": 100,
"issued_to": "elastic",
"issuer": "elastic",
"signature": "AAAAAwAAAA07qIy5rp9i1qa5VS3vAAAB...",
"start_date_in_millis": 1540080000000
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
修改type字段为platinum,表示铂金版
修改expiry_date_in_millis字段为2147482800000,表示时间尽头
修改max_nodes字段为1000,表示集群数量

{
"license": {
"uid": "c6570128-85c2-4f72-8d8f-b1425455b9ee",
"type": "basic",
"issue_date_in_millis": 1540080000000,
"expiry_date_in_millis": 1571702399999,
"max_nodes": 100,
"issued_to": "elastic",
"issuer": "elastic",
"signature": "AAAAAwAAAA07qIy5rp9i1qa5VS3vAAAB...",
"start_date_in_millis": 1540080000000
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
注:“2147482800” 表示 “北京时间2038-1-19 11:00:00”

更新许可证
修改成功后就可以在Kibana页面中上传新的许可证了
注意:上传许可证时必须使用elastic帐号登录并更新许可证

猜你喜欢

转载自www.cnblogs.com/ExMan/p/11374852.html