拉取dockerhub中的logstash镜像
docker pull logstash:版本号
构建镜像文件
FROM logstash
#安装input插件
RUN logstash-plugin install logstash-input-jdbc
#安装output插件
RUN logstash-plugin install logstash-output-elasticsearch
#容器启动时执行的命令.(CMD 能够被 docker run 后面跟的命令行参数替换)
CMD ["-f", "/some/config-dir/logstash-mysql-es.conf"]
构建镜像
docker build -t my-logstash
同步MySQL需要MySQL驱动。
为了挂载目录的时候,只挂载一个目录。我们把mysql驱动与配置文件放到同一个目录下
创建config目录,创建配置文件logstash-mysql-es.conf
文件模版:
input {
stdin { }
jdbc {
#注意mysql连接地址一定要用ip,不能使用localhost等
# mysql jdbc connection string to our backup databse
jdbc_connection_string => ""
# the user we wish to excute our statement as
jdbc_user => "root"
jdbc_password => "root"
# the path to our downloaded jdbc driver
jdbc_driver_library => ""
# the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50"
#以下对应着要执行的sql的绝对路径。
#statement_filepath => ""
statement => ""
#定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新(测试结果,不同的话请留言指出)
schedule => "* * * * *"
}
}
#处理中文乱码问题
codec => plain { charset => "UTF-8"}
#是否记录上次运行的结果
record_last_run => true
#记录上次运行结果的文件位置
last_run_metadata_path => ""
#是否使用数据库某一列的值,
use_column_value => true
tracking_column => "createtime"
#numeric或者timestamp
tracking_column_type => timestamp
#如果为true则会清除 last_run_metadata_path 的记录,即重新开始同步数据
#clean_run => false
}
}
output {
stdout {
codec => json_lines
}
elasticsearch {
#注意mysql连接地址一定要用ip,不能使用localhost等
#ESIP地址与端口
hosts => "localhost:9200"
#ES索引名称(自己定义的)
index => ""
#自增ID编号
document_id => "%{id}"
document_type => ""
}
启动容器
docker run -d --name logstash_mysql -v /root/logstash/config:/some/config-dir/ bc8551a7b495