Elasticsearch整合Kibana和Logstash环境搭建
Install Elasticsearch with Docker
Pulling the image
1 | docker pull docker.elastic.co/elasticsearch/elasticsearch |
Starting a single node cluster with Docker
采用Docker启动一个单节点的Elasticsearch
集群:
1 | -d:后台启动 -p:开放端口 --name:设置名称 |
Starting a multi-node cluster with Docker Compose
通过Docker Compose
创建一个包含3个节点的Elasticsearch
集群 :
创建一个
docker-compose.yml
file: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
70version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data03:/usr/share/elasticsearch/data
networks:
- elastic
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
elastic:
driver: bridgeRun
docker-compose
来启动集群:1
docker-compose up
Install Kibana with Docker
Pulling the image
1 | docker pull docker.elastic.co/kibana/kibana |
Configuring Kibana
新建配置文件kibana.yml
,很多默认的配置,只需要修改几个配置即可:
1 | # Kibana is served by a back end server. This setting specifies the port to use. |
编排docker-compose
新建文件docker-compose.yml
,配置特权方式和端口,映射配置kibana.yml文件:
1 | version: '2' |
启动Kibana
1 | docker-compose up -d |
Install Logstash with Docker and Auto-Sync Mysql data
Pulling the image
1 | docker pull docker.elastic.co/logstash/logstash:7.5.1 |
自定义docker镜像
新建
Dockerfile
文件配置
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13FROM docker.elastic.co/logstash/logstash:7.5.1
#安装input插件
RUN logstash-plugin install logstash-input-jdbc
#安装output插件
RUN logstash-plugin install logstash-output-elasticsearch
#安装json_lines插件
RUN logstash-plugin install logstash-codec-json_lines
#容器启动时执行的命令.(CMD 能够被 docker run 后面跟的命令行参数替换)
CMD ["-f","/usr/share/logstash/config/mysql.conf"]构建镜像
1
docker build -t my_logstash .
创建配置相关的文件
创建配置文件夹,所有配置文件都放在这个文件夹下面
1
mkdir /data/logstash/config
logstash.yml (空文件就行)
log4j2.properties
1
2logger.elasticsearchoutput.name = logstash.outputs.elasticsearch
logger.elasticsearchoutput.level = debugpipelines.yml
1
2
3- pipeline.id: my-logstash
path.config: "/usr/share/logstash/config/*.conf"
pipeline.workers: 3mysql.conf
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
33input {
jdbc {
jdbc_connection_string =>
"jdbc:mysql://localhost:3306/<database_name>?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"
## 数据库账号和密码
jdbc_user => "root"
jdbc_password => "root"
## 数据库驱动的JAR位置
jdbc_driver_library => "/usr/share/logstash/config/mysql-connector-java-5.1.47.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
## 查询的数据,根据数据库自己定义
statement => "select * from tb_user"
## 每分钟执行一次
schedule => "* * * * *"
}
}
output {
elasticsearch {
## elasticsearch 地址
hosts => "localhost:9200"
## 索引名称
index => "index-user"
## 主键
document_id => "%{id}"
## 索引类型
document_type => "user"
}
stdout {
codec => json_lines
}
}数据库驱动jar包
1
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar
特权方式启动Logstash
1 | docker run -d --privileged=true -p 4560:4560 -p 9600:9600 -it --name=my_logstash -v /data/logstash/config/:/usr/share/logstash/config/ my_logstash |
查看日志:
1 | docker logs -f -t --tail 10 my_logstash |