ElasticSearch + LogStash + Kibana 整合(7.0)

基于CentOS7.0整合说明,完成Oracle数据库实时同步操作.

环境准备

  • Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。 作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。

    说人话:存数据用滴…比一般的数据库快,即使数据量再大,也可以拉上:二娃、三娃、四娃、五娃…组成金刚葫芦娃

1
下载地址: https://www.elastic.co/cn/products/elasticsearch
  • Kibana 是一个免费且开放的用户界面,能够让您对 Elasticsearch 数据进行可视化,并让您在 Elastic Stack 中进行导航。您可以进行各种操作,从跟踪查询负载,到理解请求如何流经您的整个应用,都能轻松完成。
1
下载地址: https://www.elastic.co/cn/products/kibana
  • Logstash 是免费且开放的服务器端数据处理管道,能够从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的“存储库”中。

    说人话:勤劳的数据搬运工!!!

1
下载地址: https://www.elastic.co/downloads/logstash
  • WebUI:类似一个查询分析器,方便查数据,类似仓管,还可以监控数据和服务器硬件情况,其查询功能不想吐槽…推荐使用
    1
    下载地址:  https://github.com/appbaseio/dejavu/

注意事项

1
建议虚拟机配置内存大小至少为4G,不然...球都没得玩啊@@

ElasticSearch 环境搭建

创建目录后上传 ElasticSearch、Kibana、Logstash 到tools目录

1
2
3
4
5
6
xshell、finalshell来一套,上传还是filezilla好...

[root@localhost tools]# mkdir tools
[root@localhost tools]# ls
elasticsearch-7.0.0-linux-x86_64.tar.gz kibana-7.0.0-linux-x86_64.tar.gz
[root@localhost tools]#

解压 elasticsearch-7.0.0-linux-x86_64.tar.gz

1
2
3
4
5
6
[root@localhost tools]# tar -zxvf elasticsearch-7.0.0-linux-x86_64.tar.gz 
elasticsearch-7.0.0/
elasticsearch-7.0.0/lib/
elasticsearch-7.0.0/lib/elasticsearch-7.0.0.jar
elasticsearch-7.0.0/lib/elasticsearch-x-content-7.0.0.jar
...

迁移解压后的文件夹

1
2
3
4
5
[root@localhost tools]# mv elasticsearch-7.0.0 ../
[root@localhost ~]# ls
anaconda-ks.cfg elasticsearch-7.0.0 tools

其实...move不move实属洁癖问题...生产环境中还是要有点洁癖滴~

ElasticSearch 不能使用root用户运行,所以需要创建用户

1
2
3
4
[root@localhost ~]# useradd chenliang
[root@localhost ~]# groupadd sirm
[root@localhost ~]# usermod -G sirm chenliang
[root@localhost ~]# chown -R chenliang:sirm /root/

ElasticSearch 配置修改

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# cd elasticsearch-7.0.0/config/
[root@localhost config]# ls
elasticsearch.yml jvm.options log4j2.properties role_mapping.yml roles.yml users users_roles
[root@localhost config]#
[root@localhost config]# vim elasticsearch.yml

#以下配置必修改
cluster.name: my-es
node.name: node-1
network.host: 192.168.122.128
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

切换用户启动 ElasticSearch

1
2
3
4
5
6
7
[root@localhost ~]# su chenliang
[chenliang@localhost root]$
[chenliang@localhost root]$ cd elasticsearch-7.0.0/bin/
[chenliang@localhost bin]$ ./elasticsearch
...

会报错

错误处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

#切换到root
[root@localhost ~]# vim /etc/security/limits.conf
chenliang soft nofile 65536
chenliang hard nofile 65536
chenliang soft nproc 4096
chenliang hard nproc 4096
#保存
vm.max_map_count [65530] is too low

#切换到root
[root@localhost ~]# vim /etc/sysctl.conf
#添加
vm.max_map_count=655360
#退出保存

[root@localhost ~]# sysctl -p
vm.max_map_count = 655360

开启端口访问

1
2
3
4
5
6
[root@localhost ~]# firewall-cmd --zone=public --add-port=9200/tcp --permanent
success

[root@localhost ~]# service network restart

记得重启虚拟机,别问我为什么知道这么多...爬坑你试试咯???

ElasticSearch 环境搭建完成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost ~]# curl http://192.168.122.128:9200
{
"name" : "node-1",
"cluster_name" : "my-es",
"cluster_uuid" : "5u32r6JSS8aJb7pqaqESyw",
"version" : {
"number" : "7.0.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "b7e28a7",
"build_date" : "2019-04-05T22:55:32.697037Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.7.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
[root@localhost ~]#

肯国七哟累醒!
You Know ,It's just the beginning of the nightmare !!!

Kibana 环境搭建

解压 kibana-7.0.0-linux-x86_64.tar.gz

1
2
3
4
5
[root@localhost tools]# tar -zxvf kibana-7.0.0-linux-x86_64.tar.gz 
kibana-7.0.0-linux-x86_64/node_modules/x-pack/plugins/monitoring/public/components/logstash/pipeline_viewer/views/detail_drawer.js
kibana-7.0.0-linux-x86_64/node_modules/x-pack/plugins/monitoring/public/components/logstash/pipeline_viewer/views/index.js
kibana-7.0.0-linux-x86_64/node_modules/x-pack/plugins/monitoring/public/components/logstash/pipeline_viewer/views/metric.js
...

迁移解压后的文件夹

1
2
3
4
5
[root@localhost tools]# mv kibana-7.0.0-linux-x86_64 ../
[root@localhost tools]# cd ..
[root@localhost ~]# ls
anaconda-ks.cfg elasticsearch-7.0.0 kibana-7.0.0-linux-x86_64 tools
[root@localhost ~]#

修改 Kibana 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# cd kibana-7.0.0-linux-x86_64/config/
[root@localhost config]# ls
kibana.yml
[root@localhost config]#
[root@localhost config]# vim kibana.yml

#以下必须修改
server.port: 5601
server.host: "192.168.122.128"
elasticsearch.hosts: ["http://192.168.122.128:9200"]

#保存退出

写我的服务器地址,你问我为什么运行不起来? OMG(本人不打英雄联盟)

开启端口访问

1
2
3
4
[root@localhost ~]# firewall-cmd --zone=public --add-port=5601/tcp --permanent
success

[root@localhost ~]# service network restart

启动 Kibana

1
2
3
4
5
[root@localhost bin]# ls
kibana kibana-keystore kibana-plugin
[root@localhost bin]# ./kibana

无法访问请重启虚拟机,网管必杀技不想再重复了...

Ik中文分词器 环境配置

下载 analysis-ik 压缩包

1
https://github.com/medcl/elasticsearch-analysis-ik

解压上传到 ElasticSearch->plugins目录

1
2
3
4
5
6
[root@localhost elasticsearch-analysis-ik-7.0.0]# ls
commons-codec-1.9.jar config httpclient-4.5.2.jar plugin-descriptor.properties
commons-logging-1.2.jar elasticsearch-analysis-ik-7.0.0.jar httpcore-4.4.4.jar plugin-security.policy

[root@localhost elasticsearch-analysis-ik-7.0.0]# pwd
/root/elasticsearch-7.0.0/plugins/elasticsearch-analysis-ik-7.0.0

Kibana 测试效果

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
Kibana里面试试咯?

#Request
POST _analyze?
{
"text":"王思聪前女友",
"analyzer":"ik_smart"
}

#Response
{
"tokens" : [
{
"token" : "王思聪",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "前女友",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
}
]
}

想屁吃呢? 不想维护词典,还想白嫖个女朋友吧?

自定义分词

1
2
3
1、创建分词文件,例如 my.dic
2、编辑该文件,添加如下内容然后保存
回手掏

修改 IKAnalyzer.cfg.xml 文件

1
2
3
4
5
6
7
8
<entry key="ext_dict">my.dic</entry>
添加词库:
前女友
女朋友
老婆
王思聪
王思聪前女友
......

部署

1
2
3
1、关闭ElasticSearch后,上传该文件到目录中
/root/elasticsearch-7.0.0/plugins/elasticsearch-analysis-ik-7.0.0/config
2、重启ElasticSearch ,再次必杀技!!!

结果验证

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
#Request
POST _analyze?
{
"text":"王思聪前女友",
"analyzer":"ik_smart"
}

#Response
{
"tokens" : [
{
"token" : "王思聪前女友",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "王思聪",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "前女友",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},.......
]
}

LogStash 环境配置

上传 LogStash 到tools目录

1
2
3
[root@localhost tools]# ls
elasticsearch-7.0.0-linux-x86_64.tar.gz kibana-7.0.0-linux-x86_64.tar.gz logstash-7.0.0.tar.gz
[root@localhost tools]#

解压 logstash-7.0.0.tar.gz

1
2
[root@localhost tools]# tar -zxvf logstash-7.0.0.tar.gz
...

迁移解压后的文件夹

1
2
3
[root@localhost tools]# mv logstash-7.0.0 ../
[root@localhost ~]# ls
anaconda-ks.cfg elasticsearch-7.0.0 logstash-7.0.0 tools

LogStash 无需修改配置文件

1
 

LogStash 插件安装

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost bin]# ls
benchmark.sh ingest-convert.sh logstash-keystore logstash-plugin pqrepair system-install
cpdump logstash logstash-keystore.bat logstash-plugin.bat ruby
dependencies-report logstash.bat logstash.lib.sh pqcheck setup.bat

#操作数据库需要安装 logstash-input-jdbc 插件,安装方式进入logstash的bin目录找到logstash-plugin命令,输入以下命令等待安装完成即可,其他插件安装方式一样


[root@localhost bin]# ./logstash-plugin install logstash-input-jdbc


#更多插件请自行百度,没有免费的午餐...

LogStash 数据库同步的准备

1
2
3
4
5
6
1、如DB为Oracle,请下载 ojdbc6.jar,并存放到服务器目录
https://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html

2、例如
[root@localhost es-tools]# ls
ojdbc6.jar

LogStash DB操作自定义配置文件,名称可自定义以 .conf 结尾,建议存放路径为LogStash->config

1
2
3
[root@localhost config]# pwd
/root/logstash-7.0.0/config
[root@localhost config]# vim oracle.conf

LogStash 核心配置

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
input {
jdbc {
# jdbc配置
jdbc_driver_library => "/root/es-tools/ojdbc6.jar"
jdbc_driver_class => "Java::oracle.jdbc.driver.OracleDriver"
jdbc_connection_string => "jdbc:oracle:thin:@127.0.0.1:1521/orcl"
jdbc_user => "scott"
jdbc_password => "tiger"
jdbc_page_size => 50000
# 时间配置
jdbc_default_timezone => "Etc/UTC"
# 定时任务配置
schedule => "* * * * *"
# 配置增量更新
# 是否记录上次执行结果, 如果为真,将会把上次执行到的 tracking_column 字段的值记录下来,保存到 last_run_metadata_path 指定的文件中
record_last_run => true
# 是否需要记录某个column 的值,如果record_last_run为真,可以自定义我们需要 track 的 column 名称,此时该参数就要为 true. 否则默认 track 的是 timestamp 的值.
use_column_value => true
# 如果 use_column_value 为真,需配置此参数. track 的数据库 column 名,该 column 必须是递增的. 一般是mysql主键
tracking_column => "objid"
last_run_metadata_path => "/root/logstash-7.0.0/config/ir_templateparsemethod_last_id"
# 是否清除 last_run_metadata_path 的记录,如果为真那么每次都相当于从头开始查询所有的数据库记录
clean_run => "false"
# sql配置
statement => "SELECT * FROM IR_TEMPLATEPARSEMETHOD WHERE OBJID > :sql_last_value"
# type => "ir_templateparsemethod"
}
}

filter {
mutate {
# 移除字段
remove_field => "@version"
remove_field => "@timestamp"
}
}

output {
elasticsearch {
# 索引名称(类似关系型数据库中的数据库名)
index => "ir_templateparsemethod"
# 文档类型(类似关系型数据库中某个表)
document_type => "templateparsemethod"
# 文档编号(类似关系型数据库中某个表中id列)
document_id => "%{objid}"
# ES 地址
hosts => "192.168.122.128:9200"
}

stdout {
# 控制台输出方便监控结果
codec => json_lines
}
}

LogStash 启动,oracle.conf为刚自定义的配置文件,等待即可看到输出结果

1
[root@localhost bin]# ./logstash -f ../config/oracle.conf

Kibana 中查看结果

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
GET /ir_templateparsemethod/_search

#response
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 27,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ir_templateparsemethod",
"_type" : "templateparsemethod",
"_id" : "100001",
"_score" : 1.0,
"_source" : {
"entityname" : "reporttype",
"methodname" : "公司报告(普通类)",
"updatepanelname" : null,
"name" : "公司报告(普通类)",
"enableflag" : 1,
"version" : 1,
"objid" : 100001,
"description" : "公司报告(普通类)",
"updatetimestamp" : "2012-04-18T18:21:18.000Z",
"classname" : "com.sinitek.sirm.web.rschreport.innerreport.utils.TemplateParse0101",
"createtimestamp" : "2012-04-18T18:21:18.000Z"
}
},
{
"_index" : "ir_templateparsemethod",
"_type" : "templateparsemethod",
"_id" : "100002",
"_score" : 1.0,
"_source" : {
"entityname" : "reporttype",
"methodname" : "公司报告(保险类)",
"updatepanelname" : null,
"name" : "公司报告(保险类)",
"enableflag" : 1,
"version" : 1,
"objid" : 100002,
"description" : "公司报告(保险类)",
"updatetimestamp" : "2012-04-18T18:21:07.000Z",
"classname" : "com.sinitek.sirm.web.rschreport.innerreport.utils.TemplateParse0102",
"createtimestamp" : "2012-04-18T18:21:07.000Z"
}
}....
]
}
}

数据库中添加数据,测试是否能正常收集数据

1
2
3
4
5
INSERT INTO IR_TEMPLATEPARSEMETHOD (OBJID, NAME, CLASSNAME, METHODNAME, DESCRIPTION, ENABLEFLAG, CREATETIMESTAMP, UPDATETIMESTAMP, VERSION, ENTITYNAME, UPDATEPANELNAME)
VALUES ((SELECT NVL(MAX(OBJID), 0)+1 FROM IR_TEMPLATEPARSEMETHOD), '测试类报告33(DEMO)', 'com.sinitek.sirm.web.rschreport.innerreport.utils.TemplateParse0250', '测试类报告33(DEMO)', '测试类报告33DEMO)', 1, SYSDATE, SYSDATE, 1, 'TEMPLATEPARSEMETHOD', 'WriterUpdater.UI.CompanyreportCommentPanel');

#查看logstash 日志
[2019-04-23T16:25:00,185][INFO ][logstash.inputs.jdbc ] (0.001897s) SELECT * FROM IR_TEMPLATEPARSEMETHOD WHERE OBJID > 100034