ELK梳理
就单纯从ELK上来讨论,其实是三个组件的缩写,即:ElasticSearch、Logstash、Kibana,当然结合实际业务,也可以把ELK发展到ELKF,也就是多了一个Filebeat;这里的Filebeat只是众多beat中较为出名、用的最多的一个,还有很多beat可以实现不同文件、类型的日志采集。
Elasticsearch
分布式搜索和分析引擎。具有高可伸缩、高可靠和易管理等特点。基于 Apache Lucene 构建,能对大容量的数据进行接近实时的存储、搜索和分析操作。
Logstash
日志收集器。搜集各种数据源,并对数据进行过滤、分析、格式化等操作,然后存储到 Elasticsearch。
Kibana
数据分析和可视化平台。与 Elasticsearch 配合使用,对其中数据进行搜索、分析、图表展示。
Filebeat
一个轻量级开源日志文件数据搜集器,Filebeat 读取文件内容,发送到 Logstash 进行解析后进入 Elasticsearch,或直接发送到 Elasticsearch 进行集中式存储和分析。
架构介绍
基于ELK的使用方式,Logstash 作为日志搜集器,Elasticsearch 进行日志存储,Kibana作为日志呈现,大致以下几种架构。
- 架构一

架构二

架构三

架构四

Beats 集合了多种单一用途数据采集器,每款采集器都是以用于转发数据的通用库 libbeat 为基石,beat 所占的系统CPU和内存几乎可以忽略不计,libbeat平台还提供了检测机制,当下游服务器负载高或网络拥堵时,会自动降低发生速率。下面的例子我们使用 Filebeat 来对文件日志的收集,其他的 beat 可以忽略。
架构四相比于架构二,如果将每台服务器上部署的 Logstash 都换成对应的 Beats ,那就是更理想的架构了。
不管怎么样,对于日志解析和过滤的 Logstash 资源消耗还是比较高的,所以如果需要,可以将 Logstash 的部署使用分布式,Elasticsearch 的部署使用集群来强化整个日志系统。
转载于: https://mp.weixin.qq.com/s?subscene=23&__biz=MzU3MTUxOTIwMw==&mid=2247484837&idx=1&sn=7f0de3eeea3fc6ee299587704f5c44cd&chksm=fcdfbc29cba8353fd894582da7ae2c93b5920c4dbb1e197b011a817dc0962e317842a32d5560&scene=7&key=5ea26212a5cd15891be4fdf15216ffd079717165e9e3e989d51726b6571985c13168df15537822de411fa02ce25171be33150a939901564fd28512cd14f35a47b112e58be9d0c2b7c5164cb1b9f9ed7f7bf158e1e77a7bd55bd18035a4d50c3c32d5dfa297605b97547fdbed906d79c7a93ff3b4e28c67ac463e57fcde47f4aa&ascene=0&uin=MTA5MTc2NDg4NA%3D%3D&devicetype=Windows+7+x64&version=63000011&lang=zh_CN&exportkey=AQGVUGZhVEp0YYnsAmcs5Bk%3D&pass_ticket=irzP86a7YzJp5K5TuVkPByd4VOsDelQvRJDy7sf7KGB1Zr6oEDkh9iS%2FC%2FSukwev&wx_header=0&fontgear=2
收集日志的agent采用轻量的filebeat,并且中间使用redis进行数据写入的缓冲,整体架构如下图

主机规划:
-
192.168.2.141 :部署应用 nginx, filebeat
-
192.168.2.142 :部署redis
-
192.168.2.143 :部署logstash
-
192.168.2.144 :部署elasticsearch
-
192.168.2.145 :部署kibana
部署nginx与filebeat
-
安装nginx
-
[root@nginx-filebeat ~]# yum install nginx -y
-
安装filebeat
-
[root@nginx-filebeat sowft]# yum install filebeat-5.6.10-x86_64.rpm -y
-
查看filebeat安装后的相关路径
-
[root@nginx-filebeat sowft]# rpm -ql filebeat
-
配置filebeat,将日志输出到redis
在
/etc/filebeat/filebeat.full.yml文件中有对redis配置的内容,将里面的对应内容复制添加到/etc/filebeat/filebeat.yml中,如下配置
-
#------------------------------- Redis output ---------------------------------- -
output.redis: -
# Boolean flag to enable or disable the output module. -
enabled: true -
-
# The list of Redis servers to connect to. If load balancing is enabled, the -
# events are distributed to the servers in the list. If one server becomes -
# unreachable, the events are distributed to the reachable servers only. -
hosts: ["192.168.2.142:6379"] -
-
# The Redis port to use if hosts does not contain a port number. The default -
# is 6379. -
port: 6379 -
-
# The name of the Redis list or channel the events are published to. The -
# default is filebeat. -
key: filebeat -
-
# The password to authenticate with. The default is no authentication. -
#password: -
-
# The Redis database number where the events are published. The default is 0. -
db: 0 -
-
# The Redis data type to use for publishing events. If the data type is list, -
# the Redis RPUSH command is used. If the data type is channel, the Redis -
# PUBLISH command is used. The default value is list. -
datatype: list
部署redis
-
[root@redis-server ~]# yum install reids -y
-
修改监听端口,其他设置可以暂不修改
-
bind 0.0.0.0
安装logstash
-
安装jdk
-
yum install java-1.8.0-openjdk-devel.x86_64 -y
-
安装logstash
-
[root@logstash logstash]# yum localinstall logstash-5.6.10.rpm -y
-
将logstash指令加入环境变量
-
[root@logstash ~]# vim /etc/profile.d/logstash.s -
export PATH=/usr/share/logstash/bin:$PATH
-
[root@logstash ~]# source /etc/profile.d/logstash.sh
-
修改vim /etc/logstash/jvm.options文件
-
-Xms1g -
-Xmx1g -
#两个值保持一样,如果不一样logstash无法传输文档到ESserver
-
测试logstash正确性
[root@logstash ~]# cd /etc/logstash/conf.d
[root@logstash conf.d]# vim test.conf
input
{
stdin{}
}
output
{
stdout{}
}
-
执行测试指令
-
[root@logstash conf.d]# logstash -f ./test.conf -t -
Configuration OK
logstash上的配置文件暂时可以不配置,等部署完elasticsearch后在配置logstash的过滤规则,这里只做正确性测试即可
部署elasticsearch
-
安装jdk
-
[root@elastic ~]# yum install java-1.8.0-openjdk-devel.x86_64 -y
-
安装elasticsearch
-
[root@elastic opt]# yum install elasticsearch-5.6.10.rpm -y
-
修改配置
-
[root@elastic ~]# vim /etc/elasticsearch/elasticsearch.yml -
cluster.name: my-application -
node.name: node-1 -
path.data: /elk/data -
path.logs: /elk/logs -
network.host: 192.168.2.144 -
http.port: 9200
-
创建日志和数据目录
-
[root@elastic ~]# mkdir -p /elk/{data,logs}
-
将目录属主改为elasticsearch用户
-
[root@elastic ~]# chown -R elasticsearch.elasticsearch /elk/
-
启动服务
-
[root@elastic ~]# systemctl start elasticsearch
在nginx应创建测试数据访问
-
创建测试页面
-
[root@nginx-filebeat html]# for i in {1..150};do echo test$i > test$i.html;done
-
模拟不同地址访问生成日志
-
[root@nginx-filebeat ~]#
while true;
do
ip=$[$RANDOM%223+1];
curl --header "X-Forwarded-For: $ip.33.22.100" http://192.168.2.141/test$ip.html;
sleep 2;
done
以上的配置可以自动测试访问nginx,并生成不同的ip地址段
配置logstash过滤规则
-
input { -
-
# file { -
# -
# start_position => "end" -
# path => ["/var/log/nginx/access.log"] -
# -
# } -
# 以上file段配置是针对logstash作为agent端收集日志的场景使用,从本地文件输入 -
-
redis { -
host => "192.168.2.142" -
port => "6379" -
key => "filebeat" #这里的key要与filebeat.yml中定义的key相同 -
data_type => "list" -
# threads => "5" -
db => "0" -
} -
-
} -
-
filter { -
grok { -
match => { "message" => "%{HTTPD_COMBINEDLOG} \"%{DATA:realclient}\"" } -
remove_field => "message" -
-
} -
# \"%{DATA:realclient}\" 可以记录nginx的真实客户端地址 -
-
date { -
match => [ "timestamp","dd/MMM/YYYY:H:m:s Z" ] -
remove_field => "timestamp" -
} -
-
-
} -
-
output { -
# elasticsearch { #输出到elasticsearch服务器,先输出到本地终端查看,没有问题后输出到elasticsearch -
# hosts => "192.168.2.144:9200" -
# index => "logstash-%{+YYYY.MM.dd}" -
# document_type => "nginx_logs" -
# } -
-
stdout { #输出到屏幕测试 -
codec => rubydebug -
} -
}
-
以上的logstash的日志输出格式如下:
-
{ -
"request" => "/test1.html", -
"agent" => "\"curl/7.29.0\"", -
"offset" => 11699, -
"auth" => "-", -
"ident" => "-", -
"input_type" => "log", -
"verb" => "GET", -
"source" => "/var/log/nginx/access.log", -
"type" => "log", -
"referrer" => "\"-\"", -
"@timestamp" => 2018-09-27T08:35:35.000Z, -
"realclient" => "1.33.22.100", -
"response" => "200", -
"bytes" => "6", -
"clientip" => "192.168.2.141", -
"beat" => { -
"name" => "nginx-filebeat", -
"hostname" => "nginx-filebeat", -
"version" => "5.6.10" -
}, -
"@version" => "1", -
"httpversion" => "1.1" -
}
-
把配置文件中的配置output输出对象改为输出到elasticsearch
-
output { -
elasticsearch { -
hosts => "192.168.2.144:9200" -
index => "logstash-%{+YYYY.MM.dd}" -
document_type => "nginx_logs" -
} -
-
# stdout { #输出到屏幕测试 -
# codec => rubydebug -
# } -
}
注意:/etc/logstash/conf.d/ 目录下的所有文件都会被加载,不仅仅是以.conf结尾的文件,所以正常情况下该目录只存在一个确定使用的配置文件,其他文件都不要放在这
-
启动logstash服务
-
systemctl start logstash
-
查看elasticsearch服务生成的数据文件,实时访问服务,看看数据文件大小是否变化
-
[root@logstash ~]# curl -XGET 192.168.2.144:9200/_cat/indices -
yellow open logstash-2018.09.27 vOhS-Hq7RG-oAYTUJng91Q 5 1 93 0 560.7kb 560.7kb -
-
[root@logstash ~]# curl -XGET 192.168.2.144:9200/_cat/indices -
yellow open logstash-2018.09.27 vOhS-Hq7RG-oAYTUJng91Q 5 1 106 0 651.2kb 651.2kb -
-
#最开头以yellow开头说明配置项有点小问题。 -
#这个地方以红黄蓝颜色标记数据传输状态。 -
#黄色是缺失索引文件副本,ESserver是以集群的方式工作的,但这里是单节点
部署配置kibana
-
[root@kibana opt]# yum localinstall kibana-5.6.1-x86_64.rpm -y
-
配置
-
server.port: 5601 -
server.host: "0.0.0.0" -
server.name: "kibana" -
elasticsearch.url: "http://192.168.2.144:9200"
-
启动
-
[root@kibana ~]# systemctl start kibana
登录Kibana只需在浏览器数据kibana服务器地址+5601端口即可,经过访问测试,以上配置登录kibana调取日志访问成功
配置用户的ip归属地
ip地址的归属地查询需要使用互联网上的地图的解析库,地图的解析库会定期地更新,下载到服务器本地,解压,在logstash的配置文件中指定解压后的
.mmdb后缀的文件路径
地图解析库下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/
-
logstash的配置文件添加 geoip段,最终配置段如下:
![]()
![]()


浙公网安备 33010602011771号