一篇文章带你了解企业级开源日志平台ELK的架构和搭建
日志分析平台有助于运维人员和程序员实现故障预警和故障分析,当下大多数程序员处理的方式为在日志文件中使用grep、awk来获取自己想要的信息。但是随着数据量的增加,在海量的数据日志当中,通过这种传统的方式处理,远远不能满足程序员的需求,往往存在这些问题:
- 日志量过大,存在归档困难
- 文本的搜索太慢
- 无法进行多维度的查询

因此,需要一个平台能够集中式的、独立的、搜集管理各个服务和服务器上的日志信息,并且集中管理,提供良好的UI界面进行数据展示,处理分析。
ELK 是什么
ELK 是 elastic 公司旗下三款开源框架 ElasticSearch 、Logstash 、Kibana 的首字母的缩写。 它提供了一套解决方案,并且都是开源软件,三个软件之间相互配合使用,高效地满足了很多场景的应用。是当下主流的一种日志系统。

ELK 架构


以上是 ELK 技术栈的一个架构图。从图中可以清楚的看到数据流向:
- Beats 是单一用途的数据传输平台,它可以将多台机器的数据发送到 Logstash 或 ElasticSearch。目前Beats包含四种工具:
- Packetbeat(搜集网络流量数据)
- Topbeat(搜集系统、进程和文件系统级别的 CPU 和内存使用情况等数据)
- Filebeat(搜集文件数据)
- Winlogbeat(搜集 Windows 事件日志数据)
- Logstash 是一个动态数据收集管道。支持以 TCP/UDP/HTTP 多种方式收集数据(也可以接受 Beats 传输来的数据),并对数据做进一步丰富或提取字段处理。
- ElasticSearch 是一个基于 JSON 的分布式的搜索和分析引擎。作为 ELK 的核心,它集中存储数据。
- Kibana 是 ELK 的用户界面。它将收集的数据进行可视化展示(各种报表、图形化数据),并提供配置、管理 ELK 的界面。
ELK 经典应用场景

- Logstash部署至服务主机,对各个服务的日志进行采集、过滤、推送。
- Elasticsearch存储Logstash传送的结构化数据,提供给Kibana。
- Kibana提供用户UIweb页面进行,数据展示和分析形成图表等
注意:logs 泛指,各种日志文件以及日志信息:windows,negix,tomcat,webserver等等。
ELK 安装
2.1 elasticsearch安装(以CentOS6为例)
- 安装JDK1.8(此处略)
- 下载并安装GPG key
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
- 添加yum仓库
vim /etc/yum.repos.d/elasticsearch.repo [elasticsearch-6.x] name=Elasticsearch repository for 6.x packages baseurl=https://artifacts.elastic.co/packages/6.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
- 安装elasticsearch
yum install -y elasticsearch chkconfig --add elasticsearch
2.2 logstash安装
- 下载并安装GPG key
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
- 添加yum仓库
cat>/etc/yum.repos.d/logstash.repo<<'EOF' [logstash-6.x] name=Elastic repository for 6.x packages baseurl=https://artifacts.elastic.co/packages/6.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md EOF
- 安装logstash
yum install -y logstash
2.3 kibana安装
- 下载并安装GPG key
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
- 添加yum仓库
cat>/etc/yum.repos.d/kibana.repo<<EOF [kibana-6.x] name=Kibana repository for 6.x packages baseurl=https://artifacts.elastic.co/packages/6.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md EOF
- 安装kibana
yum install -y kibana chkconfig --add kibana
ELK 配置
3.1 配置elasticsearch
- 修改elasticsearch配置文件
vim /etc/elasticsearch/elasticsearch.yml cluster.name: zy-es-cluster node.name: node-1 path.data: /alidata/es-data path.logs: /var/log/elasticsearch #锁住内存,使内存不会再swap中使用 bootstrap.memory_lock: true bootstrap.system_call_filter: false #这个参数是用来同时设置bind_host和publish_host两个参数,一般设置这个参数就行,特殊情况下可能需要分别设置 network.host: 0.0.0.0 #设置绑定的ip地址,默认为0.0.0.0,绑定这台机器的任何一个ip #network.bind_host: 0.0.0.0 #设置其它节点和该节点交互的ip地址,如果不设置它会自动判断。 #network.publish_host: X.X.X.X #设置对外服务的http端口,默认为9200 http.port: 9200 #设置节点之间交互的tcp端口,默认是9300 transport.tcp.port: 9300 #设置集群中节点 discovery.zen.ping.unicast.hosts: ["IP地址"] mkdir -p /alidata/es-data chown elasticsearch.elasticsearch /alidata/es-data
- 优化系统参数
vim /etc/security/limits.conf * soft nofile 65536 * hard nofile 65536 * soft memlock unlimited * hard memlock unlimited vim /etc/security/limits.d/90-nproc.conf * soft nproc 4096 root soft nproc unlimited
- 启动elasticsearch
设置java路径:
ln -sv /home/jdk1.8.0_121/bin/java* /usr/bin/ service elasticsearch start netstat -lntp|grep 9200
- 验证
[root@ysc-251 elasticsearch]# curl -X GET localhost:9200
{
"name" : "node-1",
"cluster_name" : "zy-es-cluster",
"cluster_uuid" : "Hx_Vc1OASTi75iFCI2lWTQ",
"version" : {
"number" : "6.5.1",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "8c58350",
"build_date" : "2018-11-16T02:22:42.182257Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}- 安装Head插件
新版elasticsearch不支持直接在plugins里运行插件,需单独安装。
yum install -y git npm
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm config set strict-ssl false
npm install grunt -save
#修改head连接es的地址:
vim _site/app.js
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
把localhost修改成es服务器外网地址,如:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") ||
"http://IP地址:9200";
npm install
npm run start &
netstat -lntp |grep 9100
注:由于head插件监听的端口是9100,而elasticsearch的REST接口的端口是9200,因此需要设置elasticsearch允许跨域访问,才能使用head插件。
#修改elasticsearch支持跨域访问并重启
vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
service elasticsearch restart浏览器访问http://ES外网地址:9100 即可访问head插件
3.2 配置logstash
- 查看已安装插件:
/usr/share/logstash/bin/logstash-plugin list
- 基本的输入输出
/usr/share/logstash/bin/logstash -e 'input{stdin{}} output{stdout{}}'
在终端输入 helloworld,回车看返回结果:
2018-11-30T07:39:58.021Z ysc-251 helloworld- 写入到elasticsearch
/usr/share/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["IP地址:9200"] } }'在elasticsearch中查看logstash新加的索引
- 编写简单的测试conf文件
[root@ysc-251 conf.d]# cat normal.conf
input { stdin { } }
output {
elasticsearch { hosts => ["IP地址:9200"] }
stdout { codec => rubydebug }
}
执行 /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/normal.conf- 默认分片副本为1,修改分片副本数量为1
curl -XPUT 'http://IP地址:9200/_all/_settings' -H 'Content-Type: application/json' -d '{
"index.number_of_replicas" : "1"
}'3.3 配置kibana
- 修改kibana配置文件
[root@ysc-251 elasticsearch-head]# grep '^[a-Z]' /etc/kibana/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.url: "http://IP地址:9200" kibana.index: ".kibana"
- 启动kibana
service kibana start
- 验证
浏览器访问IP地址:5601 在kibana中添加一个es-*索引 验证es的muliline插件是否生效
http://IP地址/status 可以查看kibana运行状态。

实际应用
收集nginx的访问日志
logstash可以通过內建模板解析Nginx日志字符串,不过直接在Nginx配置中直接json字符串最为方便。
从1.11.8开始Nginx已经支持json格式,如果版本低于这个建议使用nginx-http-json-log扩展。
编辑nginx.conf,在server节定义json日志格式
log_format logstash_json '{"@timestamp":"$time_iso8601",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"status":"$status",'
'"host":"$host",'
'"uri":"$uri",'
'"server":"$server_name",'
'"port":"$server_port",'
'"protocol":"$server_protocol",'
'"request_uri":"$request_uri",'
'"request_body":"$request_body",'
'"request_method":"$request_method",'
'"http_referrer":"$http_referer",'
'"body_bytes_sent":"$body_bytes_sent",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"http_user_agent":"$http_user_agent",'
'"upstream_response_time":"$upstream_response_time",'
'"upstream_addr":"$upstream_addr"}';然后Nginx配置下指定json模板日志格式
access_log /alidata/tengine/logs/access.log logstash_json ;
重启Nginx
使用logstash将nginx访问日志收集起来,继续写到all.conf中
input {
file {
path => "/alidata/tengine/logs/default.log"
codec => json
start_position => "beginning"
type => "nginx-log"
}
}
output {
if [type] == "nginx-log" {
elasticsearch {
hosts => ["172.17.134.34:9200"]
index => "nginx-log-%{+YYYY.MM.dd}"
}
}
}收集系统日志
[root@ysc-251 conf.d]# cat system.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["172.17.134.34:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
[root@ysc-251 conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/system.conf