做者:Abhinav Dhasmana翻译:疯狂的技术宅前端
原文:https://blog.bitsrc.io/settin...node
未经容许严禁转载git
设置正确的日志记录基础结构可帮助咱们查找发生的问题、调试和监视应用程序。从最基本的角度来看,咱们应该从基础架构中获得如下内容:程序员
statusCode
进行搜索使用Bit(Github)在不一样项目之间共享和重用 JavaScript 组件。团队协做共享组件能够更快地构建应用程序。让 Bit 承担繁重的工做,可使你能够轻松地发布、安装和更新各个组件,而不会产生任何开销。 在此处了解更多信息。github
咱们将用 Docker 来管理服务。web
使用如下命令启动并运行 ElasticSearch面试
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name myES docker.elastic.co/elasticsearch/elasticsearch:7.4.1
能够经过如下命令检查你的容器是否已启动并运行docker
curl -X GET "localhost:9200/_cat/nodes?v&pretty"
能够用另外一个 docker run 命令启动 Kibana 并使其运行。apache
docker run —-link myES:elasticsearch -p 5601:5601 kibana:7.4.1
请注意,咱们正在使用 --link
命令连接 kibana 和弹性搜索服务器npm
若是转到 http://localhost:5601/app/kibana
,将会看到咱们的 kibana 仪表板。
如今,可使用 kibana 对咱们的弹性搜索集群运行全部查询。咱们能够导航到
http://localhost:5601/app/kibana#/dev_tools/console?_g=()
并运行咱们以前运行的查询(稍微冗长一些)
Fluentd 是对全部数据进行格式化的地方。
让咱们首先构建咱们的 Dockerfile。它有两件事:
适用于 fluentd 的 Dockerfile:
FROM fluent/fluentd:latest MAINTAINER Abhinav Dhasmana <Abhinav.dhasmana@live.com> USER root RUN apk add --no-cache --update --virtual .build-deps \ sudo build-base ruby-dev \ && sudo gem install fluent-plugin-elasticsearch \ && sudo gem install fluent-plugin-record-modifier \ && sudo gem install fluent-plugin-concat \ && sudo gem install fluent-plugin-multi-format-parser \ && sudo gem sources --clear-all \ && apk del .build-deps \ && rm -rf /home/fluent/.gem/ruby/2.5.0/cache/*.gem COPY fluent.conf /fluentd/etc/
fluent 的配置文件:
# Recieve events over http from port 9880 <source> @type http port 9880 bind 0.0.0.0 </source> # Recieve events from 24224/tcp <source> @type forward port 24224 bind 0.0.0.0 </source> # We need to massage the data before if goes into the ES <filter **> # We parse the input with key "log" (https://docs.fluentd.org/filter/parser) @type parser key_name log # Keep the original key value pair in the result reserve_data true <parse> # Use apache2 parser plugin to parse the data @type multi_format <pattern> format apache2 </pattern> <pattern> format json time_key timestamp </pattern> <pattern> format none </pattern> </parse> </filter> # Fluentd will decide what to do here if the event is matched # In our case, we want all the data to be matched hence ** <match **> # We want all the data to be copied to elasticsearch using inbuilt # copy output plugin https://docs.fluentd.org/output/copy @type copy <store> # We want to store our data to elastic search using out_elasticsearch plugin # https://docs.fluentd.org/output/elasticsearch. See Dockerfile for installation @type elasticsearch time_key timestamp_ms host 0.0.0.0 port 9200 # Use conventional index name format (logstash-%Y.%m.%d) logstash_format true # We will use this when kibana reads logs from ES logstash_prefix fluentd logstash_dateformat %Y-%m-%d flush_interval 1s reload_connections false reconnect_on_error true reload_on_failure true </store> </match>
让咱们使这台 Docker 机器跑起来
docker build -t abhinavdhasmana/fluentd .docker run -p 9880:9880 --network host abhinavdhasmana/fluentd
我已经建立了一个用于演示的小型 Node.js 程序,你能够在 https://github.com/abhinavdha... 中找到。这是一个用 Express Generator 建立的小型 Express 应用。它用 morgan 生成 apache 格式的日志。你也能够用本身的应用。只要输出保持不变,咱们的基础架构就不会在乎。让咱们构建并运行 docker 映像。
docker build -t abhinavdhasmana/logging .
固然,咱们能够经过下面给出的单个 docker compose 文件来获取全部 docker 容器。
为 EFK 设置撰写的 docker compose文件:
version: "3" services: fluentd: build: "./fluentd" ports: - "9880:9880" - "24224:24224" network_mode: "host" web: build: . ports: - "3000:3000" links: - fluentd logging: driver: "fluentd" options: fluentd-address: localhost:24224 elasticsearch: image: elasticsearch:7.4.1 ports: - "9200:9200" - "9300:9300" environment: - discovery.type=single-node kibana: image: kibana:7.4.1 links: - "elasticsearch" ports: - "5601:5601"
就是这样而已。咱们的基础架构已准备就绪。如今能够经过访问 http://localhost:3000
来生成一些日志。
如今,咱们再次转到 kibana 仪表板,并定义要使用的索引:
注意,在咱们的 fluent.conf
中提到了 logstash_prefix fluentd
,所以咱们在这里使用相同的字符串。接下来是一些基本的 kibana 设置。
弹性搜索使用动态映射来猜想其索引字段的 type
。下面的截图显示了这些:
让咱们检查一下如何知足开始时提到的要求:
path
。对其应用过滤器能够查找咱们感兴趣的 API。statusCode
进行搜索: 与上述相同。使用 code
字段并应用过滤器。discovery.type = single-node
在单节点模式下开始了弹性搜索。能够从集群模式开始,添加更多节点,或者在咱们选择的任何云提供商上使用托管解决方案。我已经尝试过了 AWS,而且易于设置。 AWS 还免费提供 Elasticsearch 的托管 kibana 实例。