利用ELK搭建Docker容器化应用日志中心

应用一旦容器化以后,需要考虑的就是如何采集位于Docker容器中的应用程序的打印日志供运维分析。典型的比如SpringBoot应用的日志收集。

本文即将阐述如何利用ELK日志中心来收集容器化应用程序所产生的日志,并且可以用可视化的方式对日志进行查询与分析,其架构如下图所示:

架构图

镜像准备

镜像准备

  • ElasticSearch镜像
  • Logstash镜像
  • Kibana镜像
  • Nginx镜像(作为容器化应用来生产日志)

开启Linux系统Rsyslog服务

修改Rsyslog服务配置文件:

<span class="hljs-attribute" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">vim</span> /etc/rsyslog.conf<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
<span class="hljs-attribute" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">systemctl</span> restart rsyslog<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

开启下面三个参数:

$ModLoad imtcp<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />$InputTCPServerRun <span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">514</span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />*.* @@localhost<span class="hljs-symbol" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">:</span><span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">4560</span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
<span class="hljs-attribute" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">systemctl</span> restart rsyslog<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

开启3个参数

意图很简单:让Rsyslog加载imtcp模块并监听514端口,然后将Rsyslog中收集的数据转发到本地4560端口!

然后重启Rsyslog服务:

<span class="hljs-attribute" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">systemctl</span> restart rsyslog<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
<span class="hljs-attribute" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">systemctl</span> restart rsyslog<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

查看rsyslog启动状态:

<span class="hljs-attribute" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">netstat</span> -tnl<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
<span class="hljs-attribute" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">netstat</span> -tnl<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

netstat -tnl

部署ElasticSearch服务

docker run -d  -p <span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">9200</span><span class="hljs-symbol" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">:</span><span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">9200</span> <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /> -v ~<span class="hljs-regexp" style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">/elasticsearch/data</span><span class="hljs-symbol" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">:/usr/share/elasticsearch/data</span> <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /> --name elasticsearch elasticsearch<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
docker run -d  -p <span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">9200</span><span class="hljs-symbol" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">:</span><span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">9200</span> <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /> -v ~<span class="hljs-regexp" style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">/elasticsearch/data</span><span class="hljs-symbol" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">:/usr/share/elasticsearch/data</span> <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /> --name elasticsearch elasticsearch<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

ES启动成功效果

部署Logstash服务

添加 ~/logstash/logstash.conf 配置文件如下:

input {<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />  syslog {<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />    type => <span class="hljs-string" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">"rsyslog"</span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />    port => <span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">4560</span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />  }<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />}<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />output {<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />  elasticsearch {<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />    hosts => [ <span class="hljs-string" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">"elasticsearch:9200"</span> ]<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />  }<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />}<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
docker run -d -p 4560:4560 <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">-v ~/logstash/logstash.conf:/etc/logstash.conf </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--link elasticsearch:elasticsearch </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--name logstash logstash </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />logstash -f /etc/logstash.conf<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

配置中我们让Logstash从本地的Rsyslog服务中取出应用日志数据,然后转发到ElasticSearch数据库中!

配置完成以后,可以通过如下命令来启动Logstash容器:

docker run -d -p 4560:4560 <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">-v ~/logstash/logstash.conf:/etc/logstash.conf </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--link elasticsearch:elasticsearch </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--name logstash logstash </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />logstash -f /etc/logstash.conf<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
docker run -d -p 4560:4560 <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">-v ~/logstash/logstash.conf:/etc/logstash.conf </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--link elasticsearch:elasticsearch </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--name logstash logstash </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />logstash -f /etc/logstash.conf<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

部署Kibana服务

docker run -d -p 5601:5601 <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--link elasticsearch:elasticsearch </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">-e ELASTICSEARCH_URL=http://elasticsearch:9200 </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--name kibana kibana</span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
docker run -d -p 5601:5601 <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--link elasticsearch:elasticsearch </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">-e ELASTICSEARCH_URL=http://elasticsearch:9200 </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--name kibana kibana</span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

启动Nginx容器来生产日志

docker run -d -p <span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">90</span>:<span class="hljs-number" style="font-size: inherit; color: #ae87fa; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">80</span> --<span class="hljs-built_in" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">log</span>-driver syslog --<span class="hljs-built_in" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">log</span>-opt <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />syslog-address=tcp:<span class="hljs-comment" style="font-size: inherit; color: #808080; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">//localhost:514 </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />--<span class="hljs-built_in" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">log</span>-opt tag=<span class="hljs-string" style="font-size: inherit; color: #eedc70; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">"nginx"</span> --name nginx nginx<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />
docker run -d -p 4560:4560 <br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">-v ~/logstash/logstash.conf:/etc/logstash.conf </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--link elasticsearch:elasticsearch </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" /><span class="hljs-deletion" style="font-size: inherit; color: #f82375; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;">--name logstash logstash </span><br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />logstash -f /etc/logstash.conf<br style="font-size: inherit; color: inherit; line-height: inherit; word-wrap: inherit !important; word-break: inherit !important;" />

很明显Docker容器中的Nginx应用日志转发到本地syslog服务中,然后由syslog服务将数据转给Logstash进行收集。

至此,日志中心搭建完毕,目前一共四个容器在工作:

实验验证

  • 浏览器打开localhost:90来打开Nginx界面,并刷新几次,让后台产生GET请求的日志
  • 打开Kibana可视化界面:localhost:5601

localhost:5601

  • 收集Nginx应用日志

收集Nginx应用日志

  • 查询应用日志 在查询框中输入program=nginx可查询出特定日志

查询应用日志

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏实战docker

Docker学习笔记之一:准备,安装,初体验

想实践一下Docker,手头是个windows电脑,由于想在linux下实践,所以第一步是装虚拟机,我用的是VMware Workstation 12 Play...

35910
来自专栏CodeSheep的技术分享

利用ELK搭建Docker容器化应用日志中心

24511
来自专栏耕耘实录

CentOS7.4中Docker以rw方式挂载volume报Permission denied的解决思路

版权声明:本文为耕耘实录原创文章,各大自媒体平台同步更新。欢迎转载,转载请注明出处,谢谢

1532
来自专栏微服务生态

Kafka常用命令行总结

./kafka-topics.sh -zookeeper127.0.0.1:2181-describe -topic testKJ1

1053
来自专栏bluesummer

Windows下docker的安装,将ASP.NET Core程序部署在docker中

参考文章: https://www.cnblogs.com/jRoger/p/aspnet-core-deploy-to-docker.html https...

4636
来自专栏木子昭的博客

解决linux端口被占用

解决端口占用问题的最快捷方式就是杀死相关进程,以杀死占用22端口的ssh为例 查看进程 如图所示,对应的进程名为sshd 进程号为 55312 输入kill ...

3826
来自专栏康怀帅的专栏

国内使用 minikube

OS:macOS GitHub:https://github.com/khs1994-docker/minikube GitHub: https://githu...

1.6K5
来自专栏静下来

腾讯云linux服务器登录和apache服务域名绑定

最近有点忙,今天又才把腾讯云给配置了下。之前一直没用,今天想申请认证的,结果说域名不在腾讯云服务器里面。 那就只能配置服务器,先上传个网页忽悠过去。 因为我还没...

9726
来自专栏jmeter高手高高手

容器化的Apache服务并监控

sudo docker run -d --name ubuntu -p 80:80 ubuntu:16.04

1193

Docker-如何清理磁盘占用

最近开始打包我们的一些应用,不得不说,我已经爱上了Docker这个工具。在AWS EC2上,它的存在使我们的工作方便了许多。然而,我们遇到的唯一问题是Docke...

3287

扫码关注云+社区

领取腾讯云代金券