在数据量大的企业级实践中,Elasticsearch显得非常常见,特别是数据表超过千万级后,无论怎么优化,还是有点力不从心!使用中,最首先的问题就是怎么把千万级数据同步到Elasticsearch中,在一些开源框架中知道了,有专门进行同步的!那就是Logstash。在思考,同步完怎么查看呢,这时Kibana映入眼帘,可视化的界面,让使用更加的得心应手哈!!这就是三剑客ELK。不过大多时候都是进行日志采集的,小编没有用,只是用来解决一个表的数据量大,查询慢的!后面小编在专门搭建日志采集的ELK。
Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,Elasticsearch 会集中存储您的数据,让您飞快完成搜索,微调相关性,进行强大的分析,并轻松缩放规模。
Kibana 是一个免费且开放的用户界面,能够让您对 Elasticsearch 数据进行可视化,并让您在 Elastic Stack 中进行导航。您可以进行各种操作,从跟踪查询负载,到理解请求如何流经您的整个应用,都能轻松完成。
Logstash 是免费且开放的服务器端数据处理管道,能够从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的“存储库”中。
现在最新版就是8.5,最新的教程少和问题未知,小编选择7版本的,求一手稳定哈!
于是去hub.Docker查看了一下,经常用的版本,最终确定为:7.17.7
dockerHub官网地址:https://hub.docker.com/_/elasticsearch
官方规定:
安装 Elastic Stack 时,您必须在整个堆栈中使用相同的版本。例如,如果您使用的是 Elasticsearch 7.17.7,则安装 Beats 7.17.7、APM Server 7.17.7、Elasticsearch Hadoop 7.17.7、Kibana 7.17.7 和 Logstash 7.17.7
sudo docker pull mysql:5.7
sudo docker run -p 3306:3306 --name mysql
-v /mydata/mysql/log:/var/log/mysql
-v /mydata/mysql/data:/var/lib/mysql
-v /mydata/mysql/conf:/etc/mysql
-e MYSQL_ROOT_PASSword=root
-d mysql:5.7
####这里往下是解释,不需要粘贴到linux上#############
--name 指定容器名字
-v 将对应文件挂载到linux主机上
-e 初始化密码
-p 容器端口映射到主机的端口(把容器的3306映射到linux中3306,这样windows上就可以访问这个数据库)
-d 后台运行
vim /mydata/mysql/conf/my.cnf # 创建并进入编辑
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_cnotallow='SET collation_connection = utf8_unicode_ci'
init_cnotallow='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
docker restart mysql
DROP TABLE IF EXISTS `sys_log`;
CREATE TABLE `sys_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '日志主键',
`title` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '模块标题',
`business_type` int(2) NULL DEFAULT 0 COMMENT '业务类型(0其它 1新增 2修改 3删除)',
`method` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '方法名称',
`request_method` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '请求方式',
`oper_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '操作人员',
`oper_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '请求URL',
`oper_ip` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '主机地址',
`oper_time` datetime(0) NULL DEFAULT NULL COMMENT '操作时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1585197503834284034 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '操作日志记录' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
es挂载:
mkdir -p /mydata/elk/elasticsearch/{config,plugins,data,logs}
kibana挂载:
mkdir -p /mydata/elk/kibana/config
logstash挂载:
mkdir -p /mydata/elk/logstash/config
vim /mydata/elk/elasticsearch/config/elasticsearch.yml
输入下面命令:
http.host: 0.0.0.0
xpack.security.enabled: false
http.host:任何地址都可以访问。
xpack.security.enabled:关闭密码认证
vim /mydata/elk/kibana/config/kibana.yml
内容:
server.host: 0.0.0.0
elasticsearch.hosts: [ "http://192.168.239.131:9200" ]
elasticsearch.hosts:指向es地址。
vim /mydata/elk/logstash/config/logstash.yml
内容:
http.host: 0.0.0.0
xpack.monitoring.elasticsearch.hosts: [ "http://192.168.239.131:9200" ]
记录存放:
touch log
chmod 777 log
vim /mydata/elk/logstash/config/logstash.conf
内容:
jdbc_driver_library:指定必须要自己下载mysql-connector-JAVA-8.0.28.jar,版本自己决定,下载地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java;
statement:如果sql长,可以指定sql文件,直接指定文件所在位置,这里的位置都为容器内部的地址;
last_run_metadata_path:上次记录存放文件对应上方的log。
input {
stdin {
}
jdbc {
jdbc_connection_string => "jdbc:mysql://192.168.239.131:3306/test?useUnicode=true&characterEncoding=utf8&serverTimeznotallow=UTC"
jdbc_user => "root"
jdbc_password => "root"
jdbc_driver_library => "/usr/share/logstash/config/mysql-connector-java-8.0.28.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "300000"
statement => "SELECT id, title, business_type, method, request_method, oper_name, oper_url, oper_ip, oper_time FROM sys_log"
schedule => "*/1 * * * *"
use_column_value => false
tracking_column_type => "timestamp"
tracking_column => "oper_time"
record_last_run => true
jdbc_default_timezone => "Asia/Shanghai"
last_run_metadata_path => "/usr/share/logstash/config/log"
}
}
output {
elasticsearch {
hosts => ["192.168.239.131:9200"]
index => "sys_log"
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
流水线指定上面的配置文件:
vim /mydata/elk/logstash/config/pipelines.yml
内容:
- pipeline.id: sys_log
path.config: "/usr/share/logstash/config/logstash.conf"
最终/mydata/elk/logstash/config/下的文件。
防止保存没有修改权限,可以把上面建的文件夹和文件赋予修改权限:
chmod 777 文件名称
在elk目录创建:
vim docker-compose.yml
内容如下:
version: '3'
services:
elasticsearch:
image: elasticsearch:7.17.7
container_name: elasticsearch
ports:
- "9200:9200"
- "9300:9300"
environment:
- cluster.name=elasticsearch
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- /mydata/elk/elasticsearch/plugins:/usr/share/elasticsearch/plugins
- /mydata/elk/elasticsearch/data:/usr/share/elasticsearch/data
- /mydata/elk/elasticsearch/logs:/usr/share/elasticsearch/logs
kibana:
image: kibana:7.17.7
container_name: kibana
ports:
- "5601:5601"
depends_on:
- elasticsearch
environment:
I18N_LOCALE: zh-CN
volumes:
- /mydata/elk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
logstash:
image: logstash:7.17.7
container_name: logstash
ports:
- "5044:5044"
volumes:
- /mydata/elk/logstash/config:/usr/share/logstash/config
depends_on:
- elasticsearch
一定要在docker-compose.yml所在目录执行命令!
运行:
docker compose up -d
完成后可以跳到5进行查看kibana!
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" -v /mydata/elk/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /mydata/elk/elasticsearch/data:/usr/share/elasticsearch/data -v /mydata/elk/elasticsearch/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:7.17.7
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://192.168.239.131:9200 -p 5601:5601 -d kibana:7.17.7
docker run -d -p 5044:5044 -v /mydata/elk/logstash/config:/usr/share/logstash/config --name logstash logstash:7.17.7
http://192.168.239.131:5601/App/home#/
PUT /sys_log
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"index": {
"max_result_window": 100000000
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"business_type": {
"type": "integer"
},
"title": {
"type": "text"
},
"method": {
"type": "text"
},
"request_method": {
"type": "text"
},
"oper_name": {
"type": "text"
},
"oper_url": {
"type": "text"
},
"oper_ip": {
"type": "text"
},
"oper_time": {
"type": "date"
},
"id": {
"type": "long"
}
}
}
}
新增几条记录,然后查看Logstash日志
docker logs -f logstash
我们去kibana看一下是否已存在:
输入命令:
GET /sys_log/_search
{
"query": {
"match_all": {}
}
}
我们看到存在6条,和mysql一致!