内容简介:部署相关 (๑• . •๑)这篇文章主要是参考
这是崔斯特的第九十三篇原创文章
部署相关 (๑• . •๑)
Docker通过EFK(Elasticsearch + Fluentd + Kibana)查询日志
这篇文章主要是参考 Docker Logging via EFK (Elasticsearch + Fluentd + Kibana) Stack with Docker Compose ,并在其基础上做了一些修改。
Elasticsearch是一个开源搜索引擎,以易用性着称。kibana是一个图形界面,可以在上面条件检索存储在 ElasticSearch 里数据,相当于提供了 ES 的可视化操作管理器。截图如下:
这里基本的架构是这样的
这里主要解决的问题是日志查询,日志来源是docker。我们使用 docker 部署任务时,可以使用 docker logs -f <容器id> 查看日志,也可以去 /var/lib/docker/containers/<容器id>/<容器id>-json.log 查看日志文件。但是这都很难去做查询,本文介绍的EFK就可以解决这个问题。
我们会创建四个容器:
- httpd (发送日志给EFK)
- Fluentd
- Elasticsearch
- Kibana
环境准备
请安装最新的docker及docker-compose,老版本会有些问题。
这里是我的版本
➜ ~ docker version Client: Docker Engine - Community Version: 18.09.2 API version: 1.39 Go version: go1.10.8 Git commit: 6247962 Built: Sun Feb 10 04:12:39 2019 OS/Arch: darwin/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.2 API version: 1.39 (minimum version 1.12) Go version: go1.10.6 Git commit: 6247962 Built: Sun Feb 10 04:13:06 2019 OS/Arch: linux/amd64 Experimental: false ➜ ~ docker-compose version docker-compose version 1.23.2, build 1110ad01 docker-py version: 3.6.0 CPython version: 3.6.6 OpenSSL version: OpenSSL 1.1.0h 27 Mar 2018
编写docker-compose.yml
Docker Compose是一个用于定义和运行多容器Docker应用程序的工具。
version: '2' services: web: image: httpd ports: - "1080:80" #避免和默认的80端口冲突 links: - fluentd logging: driver: "fluentd" options: fluentd-address: localhost:24224 tag: httpd.access fluentd: build: ./fluentd volumes: - ./fluentd/conf:/fluentd/etc links: - "elasticsearch" ports: - "24224:24224" - "24224:24224/udp" elasticsearch: image: elasticsearch:5.3.0 expose: - 9200 ports: - "9200:9200" kibana: image: kibana:5.3.0 links: - "elasticsearch" ports: - "5601:5601"
所有 web 里的日志会自动发送到 fluentd-address: localhost:24224 ,也就是 fluentd 容器。
Elasticsearch 和 Kibana并不支持最新的版本,这里选择的是5.3.0,如果想要选择更新的,可以去这里查看
Fluentd的配置和插件
新建文件 fluentd/Dockerfile ,使用官方镜像Fluentd’s official Docker image ,安装需要的插件
# fluentd/Dockerfile FROM fluent/fluentd:v0.12-debian RUN["gem","install","fluent-plugin-elasticsearch","--no-rdoc","--no-ri","--version","1.9.7"]
然后新建文件 fluentd/conf/fluent.conf ,编写Fluentd的配置文件
# fluentd/conf/fluent.conf
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<match *.**>
@type copy
<store>
@type elasticsearch
host elasticsearch
port 9200
logstash_format true
logstash_prefix fluentd
logstash_dateformat %Y%m%d
include_tag_key true
type_name access_log
tag_key @log_name
flush_interval 1s
</store>
<store>
@type stdout
</store>
</match>
官方设置文档 config-file
启动容器
在后台启动,使用 docker-compose up -d
➜ docker-compose up -d Recreating temp_elasticsearch_1 ... done Recreating temp_kibana_1 ... done Recreating temp_fluentd_1 ... done Recreating temp_web_1 ... done
查看所有容器
➜ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03d589afc2fd httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:1080->80/tcp temp_web_1 e84d0753ee58 kibana:5.3.0 "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:5601->5601/tcp temp_kibana_1 0f069b8b138f temp_fluentd "tini -- /bin/entryp…" About a minute ago Up About a minute 5140/tcp, 0.0.0.0:24224->24224/tcp, 0.0.0.0:24224->24224/udp temp_fluentd_1 fb4c6255e7ed elasticsearch:5.3.0 "/docker-entrypoint.…" 2 minutes ago Up About a minute 0.0.0.0:9200->9200/tcp, 9300/tcp temp_elasticsearch_1
产生日志
原文是 repeat 10 curl http://localhost:80/ ,但是我在docker-compose.yml中修改了端口,所以我这里是
➜ repeat 10 curl http://localhost:1080/ <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html> <html><body><h1>It works!</h1></body></html>
查看日志
打开 http://localhost:5601 ,提示需要先建索引,输入 fluentd-* 刷新即可
去 Discover 页面,然后就可以看到之前的日志了。
如何接入其他docker日志
这里是以docker-compose形式启动的一个服务,如果还有别的任务需要将日志发送到 fluentd ,需要这几个步骤。
默认情况下,docker-compose会为我们的应用创建一个网络,服务的每个容器都会加入该网络中。这样,容器就可被该网络中的其他容器访问,不仅如此,该容器还能以服务名称作为hostname被其他容器访问。
所以我们首先需要找到我们现在创建的EFK的网络名,
➜ docker network ls NETWORK ID NAME DRIVER SCOPE fd428cfa7264 bridge bridge local e08505f7bfe6 docker-compose-efk_default bridge local 650f2f690175 docker-efk_default bridge local 35f17aeb61e9 docker-elk_elk bridge local db29d28aa5cc esnet bridge local 56ea915974c9 host host local 4d0207065fb8 none null local 3dd60d8ddfce temp_default bridge local cf1a06702ae8 web_default bridge local
我是在temp目录下创建的docker-compose.yml文件,所以这里默认的名字就是 temp_default 。
再看看之前web的设置
web:
image: httpd
ports:
- "1080:80" #避免和默认的80端口冲突
links:
- fluentd
logging:
driver: "fluentd"
options:
fluentd-address: localhost:24224
tag: httpd.access
有几个关键设置是:links和logging,link 用于容器直接的互通,logging则是日志的输出设置。
那我们这里再启动一个新docker需要这些设置
docker run
--link temp_fluentd_1 \
--net temp_default \
--log-driver=fluentd \
--log-opt fluentd-address=localhost:24224 \
--log-opt tag=httpd.access \
-d hello-world
我们去kibana看看,果然,日志已经发送到kibana了
将log添加到选择字段,日志阅读起来更佳
版本选择
上面说到这里选择的是 5.3.0 ,我们试试新版本 6.7.1 ,再看看kibana。使用 docker-compose stop 停止服务,修改docker-compose文件再启动。
但是已启动es就挂了,最
后查看日志 docker-compose logs | grep elasticsearch_1 ,发现如下错误:
“Native controller process has stopped - no new native processes can be started”
最后在giuhub上找到答案: es crashes with “Native controller process has stopped - no new native processes can be started”
解决方法是,提高docker可用内存,mac上是这样设置,把内存从2G提高到4G,再启动就成功了。
这里是6.7.1的界面
最后想要做的就是如何在一台服务器上搜集所有的日志,理论上来说,只需要一台服务器部署上EFK,暴露端口,其他服务器去发送即可,实际上还没试过。
如果有好的意见,欢迎来提。
以上所述就是小编给大家介绍的《Docker通过EFK(Elasticsearch + Fluentd + Kibana)查询日志》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何通过SQL查询Elasticsearch
- Oracle通过递归查询父子兄弟节点方法示例
- Elasticsearch 技术分析(五):如何通过SQL查询Elasticsearch
- 通过在执行计划中删除排序运算符来优化SQL查询
- Mybatis关联查询(嵌套查询)
- MySQL高级查询---连接查询实例
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
500 Lines or Less
Amy Brown、Michael DiBernardo / 2016-6-28 / USD 35.00
This book provides you with the chance to study how 26 experienced programmers think when they are building something new. The programs you will read about in this book were all written from scratch t......一起来看看 《500 Lines or Less》 这本书的介绍吧!