Open Distro 项目将 Elasticsearch 和 Kibana 的开源发行版与 Apache-2.0 许可插件捆绑在一起,为用户提供企业级功能、安全性和分析工具。自推出以来的两年中,世界各地的开发者都使用 Open Distro 来支持他们的应用程序。

开源 Elasticsearch 和 Kibana 7.10.2 即将结束生命周期,不再接受积极的开发、安全补丁或错误修复。所有用户都应该运行能够及时收到安全补丁的软件。启动 OpenSearch 项目的目的是为开源 Elasticsearch 和 Open Distro 用户提供一条前进的道路,以确保他们始终能够获得安全性和新的创新。

​ 以上引用官网文档《https://opendistro.github.io/for-elasticsearch/#prod》

x000 下载软件包

K-NN是Amazon公司开发并开源的一款支持ElasticSearch的相似向量检索插件,而且搜索速度和效果比ElasticSearch原生的插件更好,为产品推荐、欺诈检测、图像和视频搜索、相关文档搜索等场景提供支持。

根据官方的描述,k-NN非常符合我们的业务场景——图像的黑样本检测。离线或者在线黑样本图像入库并打标签属性,入库时提取图像特征(256维度)并将其和标签属性一起保存到ElasticSearch中,业务的每一笔调用(调用ES检索之前先通过算法提取特诊,实际上是特征维度的向量检索)通过检索ES黑样本库,判定检索的图像是否在黑样本中。

和k-NN插件能够配套的ElasticSearch是社区版,由Amazon维护,属于Open Distro 项目,因此软件名为opendistro-for-elasticsearch, 现在改为OpenSearch。官网为:https://opendistro.github.io/for-elasticsearch-docs/docs/elasticsearch/,看清楚咯,不是ElasticSearch的官网。

我们的服务是X86位的架构,操作系统为CentOS 7x 64位,因此我们选择x64的软件包。在决定安装之前,我们需要知道我们应该用哪个版本:目前opendistro-for-elasticsearch的版本为1.13.0.0,主要包括下面软件包:

包名 版本 类型
Elasticsearch (Apache 2.0 licensed code only) v7.10.2 主包
Alerting Plugin v1.13.1.0 插件
Anomaly Detection Plugin v1.13.0.0 插件
Asynchronous Search Plugin v1.13.0.1 插件
Index Management Plugin v1.13.2.0 插件
Job Schedular Plugin v1.13.0.0 插件
k-NN Plugin v1.13.0.0 插件
Performance Analyzer v1.13.0.0 插件
Reports Scheduler Plugin v1.13.0.0 插件
SQL & Piped Processing Language Plugin v1.13.2.0 插件
Security Plugin v1.13.1.0 插件

根据我们已运行项目安装版本,ElasticSearch7.10.2满足要求,搭配的k-NN为1.13.0.0,我们只需要下载opendistroforelasticsearch-1.13.3-linux-x64.tar.gz该软件包即可满足。

图1 SaaS中ES的配置信息

下载压缩包

1
curl https://d3g5vo6xdbdb9a.cloudfront.net/tarball/opendistro-elasticsearch/opendistroforelasticsearch-1.13.3-linux-x64.tar.gz -o opendistroforelasticsearch-1.13.3-linux-x64.tar.gz

下载校验包

1
curl https://d3g5vo6xdbdb9a.cloudfront.net/tarball/opendistro-elasticsearch/opendistroforelasticsearch-1.13.3-linux-x64.tar.gz.sha512 -o opendistroforelasticsearch-1.13.3-linux-x64.tar.gz.sha512

校验压缩包

1
2
shasum -a 512 -c opendistroforelasticsearch-1.13.3-linux-x64.tar.gz.sha512
# 如果没有安装shasum,Centos需要安装perl-Digest-SHA

输出:opendistroforelasticsearch-1.13.3-linux-x64.tar.gz: OK 表示该包没有问题!

0x001 安装OpenSearch

安装用户:admin,软件包目录:/home/admin/softwares/

解压opendistroforelasticsearch-1.13.3-linux-x64.tar.gz:tar -zvf opendistroforelasticsearch-1.13.3-linux-x64.tar.gz,并在目录下运行安装脚本./opendistro-tar-install.sh

图2 JDK版本问题

意思是当前环境jdk的版本是1.8,es需要的版本是java 11,不满足要求。实际上,我们下载的ES包中已经包含了openJDK,我们使用内置的JDK即可。因此安装过程中,现在会话Session中把已有的JAVA_HOME和CLASSPATH两个环境变量设置为空。

1
2
unset JAVA_HOME
unset CLASSPATH

我们新打开一个命令控制台,执行下面命令看看ES是否启动成功:

1
2
curl -XGET https://localhost:9200 -u 'admin:admin' --insecure
curl -XGET https://localhost:9200/_cat/plugins?v -u 'admin:admin' --insecure

出现错误:curl: (56) Received HTTP code 503 from proxy after CONNECT

错误原因:我们的机器上设置了代理,通过命令查看:env | grep -I proxy

all_proxy=http://10.57.22.219:3128/

socket_proxy=http://10.57.22.219:3128/

https_proxy=http://10.57.22.219:3128/

http_proxy=http://10.57.22.219:3128/

解决方法:

1
2
3
4
unset all_proxy
unset socket_proxy
unset https_proxy
unset http_proxy

再次访问:curl -XGET https://localhost:9200 -u 'admin:admin' --insecure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[admin@node1 ~]$ curl -XGET https://localhost:9200 -u 'admin:admin' --insecure
{
"name" : "node1",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "XSIZV5uCSgKsZy5snydzGw",
"version" : {
"number" : "7.10.2",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
"build_date" : "2021-01-13T00:42:12.435326Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

查看插件版本:

1
2
3
4
5
6
7
8
9
10
11
12
[admin@node1 ~]$ curl -XGET https://localhost:9200/_cat/plugins?v -u 'admin:admin' --insecure
name component version
node1 opendistro-alerting 1.13.1.0
node1 opendistro-anomaly-detection 1.13.0.0
node1 opendistro-asynchronous-search 1.13.0.1
node1 opendistro-index-management 1.13.2.0
node1 opendistro-job-scheduler 1.13.0.0
node1 opendistro-knn 1.13.0.0
node1 opendistro-performance-analyzer 1.13.0.0
node1 opendistro-reports-scheduler 1.13.0.0
node1 opendistro-sql 1.13.2.0
node1 opendistro_security 1.13.1.0

0x002 配置ES

单机配置

配置ES可以在config/elasticsearch.yml或者在启动命令中通过参数-E指定,例如:

1
./opendistro-tar-install.sh -Ecluster.name=prod-siamat -Enode.name=siamat-1 -Ehttp.host=0.0.0.0 -Ediscovery.type=single-node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[admin@node1 ~]$ curl -XGET https://localhost:9200 -u 'admin:admin' --insecure
{
"name" : "simat-1",
"cluster_name" : "siamat",
"cluster_uuid" : "XSIZV5uCSgKsZy5snydzGw",
"version" : {
"number" : "7.10.2",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
"build_date" : "2021-01-13T00:42:12.435326Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

配置文件,其他的配置使用默认即可:

1
2
3
4
5
6
7
8
9
10
11
# 集群名称:
cluster.name: siamat
# 节点名称:
node.name: simat-1
# 网络配置
network.host: 0.0.0.0
# 端口配置
http.port: 9200
# 服务发现
discovery.type: single-node
discovery.seed_hosts: ["0.0.0.0:9200"]

集群配置

ES单机上的集群配置,需要复制ES解压后的包(想要几个节点需要复制几份)。我们需要配置三个节点的集群,那么我们复制三分ES软件包,分别命名:opendistroforelasticsearch-1.13.3 opendistroforelasticsearch-1.13.3_9201_9301、和opendistroforelasticsearch-1.13.3_9202_9302,命名规则后面为http端口号_TCP端口号

修改软件包config/elasticsearch.yml配置文件,根据我们包的命名规则分别修改对应包下的http端口TCP端口节点名称以及节点属性是master还是data,其他的配置项保持一致。

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ======================== Elasticsearch Configuration =========================
# ---------------------------------- Cluster -----------------------------------
# 集群配置
# 集群名称,所有集群,名称必须一致,。如果不配置该项,系统默认取elasitcsearch
cluster.name: siamat
# ------------------------------------ Node ------------------------------------
# 节点配置
# 节点名称,同一集群的节点名称不能相同,如果不配置,会随机分配一个名称
node.name: simat-1
# 是否是master节点,true-是,false-否,该属性可以不设置,节点之间自主选举
node.master: true
# 该节点是否存储数据,true-是,false-否
node.data: true
node.max_local_storage_nodes: 3
#
# 节点的部落属性,这是一个比集群更大的范围,不设置,默认即可
#node.attr.rack: r1
# ----------------------------------- Paths ------------------------------------
# 设置数据和日志的存放路径,不设置,默认在ES的主目录下面创建
#path.data: /path/to/data
#path.logs: /path/to/logs
# ----------------------------------- Memory -----------------------------------
# 内存配置
# 启动后是否锁定内存,提高ES的性能
bootstrap.memory_lock: true
# ---------------------------------- Network -----------------------------------
#
# 设置网络,可以绑定IP4或者IP6的地址
# 设置http的端口,默认为9200,注意,同一台服务器的其他节点需要修改
http.port: 9200
# 设置对外的网关IP,默认为本地回环
network.host: 0.0.0.0
network.bind_host: 0.0.0.0
network.publish_host: 0.0.0.0
# 设置TCP传输端口
# Discovery部分的设置,集群内节点发现走的就是这个端口,发现后,节点之前传输数据也是走这个TCP端口,
# 另外,官方提供的ES JAVA API也是通过这个端口传输数据的
transport.tcp.port: 9300
# --------------------------------- Discovery ----------------------------------
#
# 配置集群节点之间的连接或者说集群节点的发现
# discovery.seed_hosts 自动发现设置,配置之后集群的主机之间可以自动发现,对应旧版本的discovery.zen.ping.unicast.hosts
discovery.seed_hosts: ["0.0.0.0:9300", "0.0.0.0:9301","0.0.0.0:9302"]
#discovery.zen.ping.unicast.hosts: ["0.0.0.0:9300", "0.0.0.0:9301","0.0.0.0:9302"]
#discovery.zen.minimum_master_nodes: 2
# 在新集群搭建初期,总会出现某几个节点与其他节点通信异常导致节点频繁加入、退出集群。这个过程是自动执行的。通过配置discovery.zen.ping_timeout来控制节点加入某个集群或者开始选举的响应时间(默认3s)。
discovery.zen.ping_timeout: 60s

# 基于文件的自动发现设置,配置之后集群的主机之间可以自动发现。
# discovery.seed_providers: /path/to/seed_provider
#
# 7.0引入的配置项,设置一系列符合主节点条件的节点的主机名或 IP 地址来引导启动集群。
# 如果手动设置了node.name,还可以设置为此节点的名称
#cluster.initial_master_nodes: ["simat-1", "simat-2", "simat-3"]

# ---------------------------------- Gateway -----------------------------------
# 网关配置
# Block initial recovery after a full cluster restart until N nodes are started:
# 设置集群中N个节点启动时进行数据恢复,默认为1
gateway.recover_after_nodes: 2
# 设置初始化数据恢复进程的超时时间,默认是5分钟。
gateway.recover_after_time: 3m
# 设置集群中节点的数量,默认为2,一旦这N个节点启动,就会立即进行数据恢复。
gateway.expected_nodes: 3

# ---------------------------------- Various -----------------------------------
#
# 在删除索引时,是否需要明确指定名称.该值为false时,则可通过正则或_all删除
action.destructive_requires_name: true
# 这个关闭了自动创建索引。为的也是安全考虑,否则即使是内网,也有很多扫描程序,一旦开启,扫描程序会自动给你创建很多索引。
action.auto_create_index: false
# 禁用访问http地址时需要增加账户验证
opendistro_security.disabled: true

# 开启跨域访问支持,默认为false
#http.cors.enabled: true
# 跨域访问允许的域名地址,(允许所有域名)以上使用正则
#http.cors.allow-origin: /.*/

######## Start OpenDistro for Elasticsearch Security Demo Configuration ########
# WARNING: revise all the lines below before you go into production
opendistro_security.ssl.transport.pemcert_filepath: esnode.pem
opendistro_security.ssl.transport.pemkey_filepath: esnode-key.pem
opendistro_security.ssl.transport.pemtrustedcas_filepath: root-ca.pem
opendistro_security.ssl.transport.enforce_hostname_verification: false
# 禁用https
opendistro_security.ssl.http.enabled: false
opendistro_security.ssl.http.pemcert_filepath: esnode.pem
opendistro_security.ssl.http.pemkey_filepath: esnode-key.pem
opendistro_security.ssl.http.pemtrustedcas_filepath: root-ca.pem
opendistro_security.allow_unsafe_democertificates: true
opendistro_security.allow_default_init_securityindex: true
opendistro_security.authcz.admin_dn:
- CN=kirk,OU=client,O=client,L=test, C=de

opendistro_security.audit.type: internal_elasticsearch
opendistro_security.enable_snapshot_restore_privilege: true
opendistro_security.check_snapshot_restore_write_privileges: true
opendistro_security.restapi.roles_enabled: ["all_access", "security_rest_api_access"]
opendistro_security.system_indices.enabled: true
opendistro_security.system_indices.indices: [".opendistro-alerting-config", ".opendistro-alerting-alert*", ".opendistro-anomaly-results*", ".opendistro-anomaly-detector*", ".opendistro-anomaly-checkpoints", ".opendistro-anomaly-detection-state", ".opendistro-reports-*", ".opendistro-notifications-*", ".opendistro-notebooks", ".opendistro-asynchronous-search-response*"]
cluster.routing.allocation.disk.threshold_enabled: false
######## End OpenDistro for Elasticsearch Security Demo Configuration ########

各节点配置好之后启动:./opendistro-tar-install.sh -d

在ES启动时发现如下错误:

org.elasticsearch.transport.RemoteTransportException: [simat-1][172.17.8.1:9300][internal:cluster/coordination/join]

Caused by: java.lang.IllegalArgumentException: can’t add node {simat-2}{L9o4bu-kSg6d5SDhZd5a0w}{iFLfAd-MQCirpfW5VjzH2A}{172.17.8.1}{172.17.8.1:9301}{dir}, found existing node {simat-1}{L9o4bu-kSg6d5SDhZd5a0w}{P7M5kFcjRwG2Gghsm9VCfw}{172.17.8.1}{172.17.8.1:9300}{dimr} with the same id but is a different node instance

图3 ES启动报错

问题出现的原因是,复制虚拟机时,elsticsearch时,将elsticsearch文件夹下的data文件夹一并复制了。而在前面测试时,data文件夹下已经产生了data数据,于是报上面的错误。

解决办法:删除elsticsearch文件夹下的data/nodes文件夹下的节点数据,重启即可。

1
2
3
4
5
6
7
[admin@node1 data]$ cd nodes/
[admin@node1 nodes]$ ls
0
[admin@node1 nodes]$ cd ../
[admin@node1 data]$ ls
batch_metrics_enabled.conf logging_enabled.conf nodes performance_analyzer_enabled.conf rca_enabled.conf
[admin@node1 data]$ rm -rf nodes/

启动后,测试各节点是否正常启动。如果配置没有禁用ssl以及鉴权认证,则需要访问:curl -XGET https://localhost:9201 -u 'admin:admin' --insecure,否则直接curl -XGET http://localhost:9201 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[admin@node1 ~]$ curl -XGET https://localhost:9201 -u 'admin:admin' --insecure
{
"name" : "simat-2",
"cluster_name" : "siamat",
"cluster_uuid" : "XSIZV5uCSgKsZy5snydzGw",
"version" : {
"number" : "7.10.2",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
"build_date" : "2021-01-13T00:42:12.435326Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[admin@node1 ~]$ curl -XGET https://localhost:9202 -u 'admin:admin' --insecure
{
"name" : "simat-3",
"cluster_name" : "siamat",
"cluster_uuid" : "XSIZV5uCSgKsZy5snydzGw",
"version" : {
"number" : "7.10.2",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
"build_date" : "2021-01-13T00:42:12.435326Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

0x003 系统参数调整

修改JVM内存大小

将ES软件包中的jvm.options中的JVM的内存进行修改。

  • /home/admin/softwares/elasticsearch/opendistroforelasticsearch-1.13.3/config/jvm.options
  • /home/admin/softwares/elasticsearch/opendistroforelasticsearch-1.13.3_9201_9301/config/jvm.options
  • /home/admin/softwares/elasticsearch/opendistroforelasticsearch-1.13.3_9201_9301/config/jvm.options
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms8g
-Xmx8g

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30

## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m

## OpenDistro Performance Analyzer
-Dclk.tck=100
-Djdk.attach.allowAttachSelf=true
-Djava.security.policy=/home/admin/softwares/elasticsearch/opendistroforelasticsearch-1.13.3/plugins/opendistro-performance-analyzer/pa_config/es_security.policy

注意:修改ES三个节点的JVM运行内存,这个可以根据需要更改,但是**-Xms-Xmx**的值必须一样,不然启动报错*。

如果ES集群已运行,修改后,需要重新启动ES集群方可生效。

修改linux的最大打开文件数

可能出现的错误:

  1. max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
  2. max number of threads [3539] for user [elastic] is too low, increase to at least [4096]
  3. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
  • 修改Linux最大打开文件数
1
2
3
4
5
6
7
8
9
10
[admin@node1 elasticsearch]$ vim /etc/security/limits.conf
## 主要配置这两行nofile 文件描述符 和 nproc 最大线程数
* soft nofile 1000000
* hard nofile 1000000
* hard nproc 65535
* soft nproc 65535
* hard core unlimited
* soft core unlimited
* hard stack 32768
* soft stack 20470

这一步需要重启linux使配置生效

1
[admin@node1 elasticsearch]$reboot
  • 修改sysctl.conf文件
1
2
3
4
[admin@node1 elasticsearch]$ vim /etc/sysctl.conf
# 查看是否有该配置,对其进行修改
vm.max_map_count=1048575
[admin@node1 elasticsearch]$ sysctl -p

配置完,重启ES方可生效。

iptable以及防火墙

如果需要在其他机器上访问ES,则开放防火墙9200、9201、9202端口:

1
2
3
4
# 允许所有ip访问本机的9200、9201、9202端口
iptables -A INPUT -p tcp --dport 9200 -j ACCEPT
iptables -A INPUT -p tcp --dport 9201 -j ACCEPT
iptables -A INPUT -p tcp --dport 9202 -j ACCEPT

具体的防火墙规则可以参考:【利用iptables实现简单的ES http端口访问限制