一、Elasticsearch + Kibana + Fluentd 简单介绍
EFK 架构可用于日志的收集、存储、搜索和分析。组件的功能说明如下:
- Fluentd 用以收集日志。
- Elasticsearch 简称
es
主要用以数据的存储并提供索引与搜索功能。 - Kibana 搜索与分析。
二、环境准备
在滴滴云官网购买一台 DC2 实例,规格无需太大,需要带 EIP 方便下载和安装。
三、Elasticsearch + Kibana + Fluentd 安装
登陆滴滴云服务器进行如下操作(本例使用的操作系统版本为 CentOS7.4)
1. 安装部署 Fluentd
1 2 3 4 5 |
curl -L 'https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh' | sh #安装es插件 /usr/sbin/td-agent-gem install fluent-plugin-elasticsearch |
2. 修改 Fluentd 配置文件 td-agent.conf(本例以收集nginx的日志为模板)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
vim /etc/td-agent/td-agent.conf <source> @type tail path /var/log/nginx.log # 要收集的日志文件路径 pos_file /var/log/td-agent/nginx.log.pos tag nginx.log # 标签 # 分析提取日志信息,最终的正则表达式以nginx的字段为准。 format /^(?<time>[^\]]*) (?<remote_ip>[^ ]*) (?<user>[^ ]*) "(?<host>[^ ]*)" "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*) "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" (?<request_time>[^ ]*) (?<upstream_time>[^ ]*) (?<pipe>[^ ]*)$/ </source> <match nginx.log> @type copy <store> # debug模式 (tail -f /var/log/td-agent/td-agent.log)可以查看日志收集情况 @type stdout </store> <store> @type elasticsearch # 使用的数据存储的插件 host dc2实例内网的IP地址 # es服务器地址 port 9200 # es端口 logstash_format true flush_interval 10 </store> </match> |
3. 启动 Fluentd 服务
1 2 3 4 5 |
systemctl start td-agent systemctl status td-agent |
4. 安装部署 Elasticsearch
1 2 3 4 5 |
wget -S 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.rpm' rpm -ivh elasticsearch-6.6.1.rpm |
5. 修改 elasticsearch.yml 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 |
vim /etc/elasticsearch/elasticsearch.yml ... network.host: dc2实例内网的IP地址 ... http.port: 9200 ... |
6. 启动 Elasticsearch 服务
1 2 3 4 5 |
systemctl start elasticsearch systemctl status elasticsearch |
7. 安装 Kibana
1 2 3 4 5 6 |
wget -S 'https://artifacts.elastic.co/downloads/kibana/kibana-6.6.1-x86_64.rpm' rpm -ivh kibana-6.6.1-x86_64.rpm |
8. 修改 kibana.yml 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
vim /etc/kibana/kibana.yml ... server.port: 5601 ... server.host: "dc2实例内网的IP地址" ... elasticsearch.hosts: ["http://dc2实例内网的IP地址:9200"] ... |
9. 启动 Kibana 服务
1 2 3 4 5 |
systemctl start kibana systemctl status kibana |
10. 配置滴滴云 SLB 和安全组规则
- 安全规则设置:安全组中新增 5601 端口的入方向规则,授权对象为:0.0.0.0/0
11. 服务验证
- 浏览器中输入 http://EIP:5601 即可看到 Kibana 的主页,在简单的配置之后即可看到收集的 Nginx 日志信息。
本文作者:杜建成