1.获取镜像
ELK镜像已经被docker官方收录,因此只需要一个命令就可以获取镜像,如下
docker pull sebp/elk
2.创建容器
使用镜像创建容器,为了保证ELK能够正常运行,加了-e参数限制使用最小内存及最大内存,命令如下:
docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -e ES_MIN_MEM=128m -e ES_MAX_MEM=1024m -v /Users/yang/dockerFile:/data -it --name elk sebp/elk
-v /Users/file:/data 是将本地计算机的/Users/file目录映射到docker虚拟机的data目录,后续使用该目录保存nginx日志测试
3.验证容器是否安装成功
(1)验证Kibana是否启动成功,在浏览器里访问http://localhost:5601/ ,出现如下界面则启动成功
(2)验证ES服务是否启动成功,在浏览器里访问http://localhost:9200/_search?pretty,出现如下界面则安装成功
4.容器的启动、关闭
启动:docker start elk
关闭:docker stop elk
5.进入容器虚拟机
docker exec -it elk bash
6.启动logstash
/opt/logstash/bin/logstash -e 'input {file {path => "/data/logs/access.log"}}output {elasticsearch { hosts => ["127.0.0.1:9200"] }stdout { codec => rubydebug }}'
注意:如果看到这样的报错信息 Logstash could not be started because there is already another instance using the configured data directory. If you wish to run multiple instances, you must change the “path.data” setting. 请执行命令:service logstash stop 然后在执行就可以了。
7.修改nginx配置
配置如下:
log_format json '{"@timestamp":"$time_iso8601","@version":"1","host":"$server_addr","client":"$remote_addr", "size":"$body_bytes_sent","responsetime":"$request_time","domain":"$host","url":"$uri","status":"$status"}';
access_log /Users/yang/dockerFile/logs/access.log json;
8.访问nginx,检查ES数据
刷新http://localhost:9200/_search?pretty页面,查看是否有日志信息,如图