逻辑图
1、下载镜像文件
docker pull elasticsearch:5.6.11
docker pull kibana:5.6.11
docker pull logstash:5.6.15
2、创建实例
1、ElasticSearch
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms256m -Xmx256m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data -d elasticsearch:5.6.11
特别注意:
-e ES_JAVA_OPTS="-Xms256m -Xmx256m" \ 测试环境下,设置ES的初始内存和最大内存,否则导致过大启动不了ES
访问地址:
http://192.168.217.130:9200/
一开始es起来后老是自动停掉 然后把虚拟机内存分配3g就可以了。。
2、Kibana
docker run --name kibana -e ELASTICSEARCH_URL=http://192.168.217.130:9200 -p 5601:5601 \
-d kibana:5.6.11
访问地址:
http://192.168.217.130:5601
3、Logstash
1)、在 /mydata/logstash中创建logstash.conf文件:文件内容如下
input {
tcp {
port => 4560
codec => json_lines
}
}
output{
elasticsearch {
hosts => ["192.168.217.130:9200"]
index => "applog"
}
stdout { codec => rubydebug }
}
注意:
hosts一定不要写127或者localhost;这样docker容器内部127没有es实例,连不上
docker run -d -p 4560:4560 \
-v /mydata/logstash/logstash.conf:/etc/logstash.conf \
--link elasticsearch:elasticsearch \
--name logstash logstash:5.6.15 \
logstash -f /etc/logstash.conf
安装插件:
https://github.com/logstash-plugins
docker exec -it logstash /bin/bash (进入容器内容)
cd /usr/share/logstash/bin (可以whereis logstash找到这个位置)
logstash-plugin install logstash-codec-json_lines
我们对容器做了改变;为了以后方便,可以将这个容器再打包成新的镜像;
可以将修改好的容器制作为镜像,方便下次使用(也可直接推送到镜像仓库);
docker commit logstash logstash_gmall:0.0.1
上传Docker镜像到阿里云(阿里云仓库管理里面有步骤):
l 命令行登录到阿里云的doker仓库,–username为阿里云的用户名
docker login --username=forsumlove registry.cn-hangzhou.aliyuncs.com
l 为本地镜像添加tag
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/lfy/gmall:[镜像版本号]
docker tag fb11fcb28ea0 registry.cn-hangzhou.aliyuncs.com/lfy/gmall:logstatsh_0.0.1
l push到docker仓库
docker push registry.cn-hangzhou.aliyuncs.com/lfy/gmall:[镜像版本号]
docker push registry.cn-hangzhou.aliyuncs.com/lfy/gmall:logstatsh_0.0.1
登录阿里云查看已上传的镜像列表:https://cr.console.aliyun.com/#/imageList
l 从阿里云自己的仓库拉取
docker pull registry.cn-hangzhou.aliyuncs.com/lfy/gmall:[镜像版本号]
4、log日志文件
在项目中添加日志文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<!--应用名称-->
<property name="APP_NAME" value="gmall-pms"/>
<!--日志文件保存路径-->
<property name="LOG_FILE_PATH" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/logs}"/>
<contextName>${APP_NAME}</contextName>
<!--每天记录日志到文件appender-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_PATH}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!--输出到logstash的appender-->
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>192.168.217.130:4560</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<!--日志级别 DEBUG-INFO-WARN-ERROR -->
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="LOGSTASH"/>
</root>
</configuration>
5、建立索引
这个applog是在logstash.conf中配置的