以往运维人员在分析日志的时候,相信大家用的最多的方法就是逐个登陆到服务器上面使用sed和awk工具分析,或者撸一个shell脚本或者Python脚本来分析日志。但是此方法不单不直观,而且效率很低。通常,日志被分散的储存不同的设备上。如果你管理数十上百台服务器,那你就有得玩了,虽然有像Ansible这样的自动化工具,但也不会很高效。当务之急我们使用集中化的日志管理,例如:开源的syslog,将所有服务器上的日志收集汇总。例如:开源的syslog,将所有服务器上的日志收集汇总。

集中化管理日志后,日志的统计和检索又成为一件比较麻烦的事情,一般我们使用grep、awk和wc等Linux命令能实现检索和统计,但是对于要求更高的查询、排序和统计等要求和庞大的机器数量依然使用这样的方法难免有点力不从心。

ELK介绍

开源实时日志分析ELK平台能够完美的解决我们上述的问题,ELK由ElasticSearch、Logstash和Kiabana三个开源工具组成。官方网站:https://www.elastic.co/products

Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash是一个完全开源的工具,他可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。
Kibana 也是一个开源和免费的工具,它Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。

下图是elk工作原理:
CentOS7搭建ELK日志集中分析平台
如图:Logstash收集AppServer产生的Log,并存放到ElasticSearch集群中,而Kibana则从ES(或者单机)集群中查询数据生成图表,再返回给Browser。

ELK平台搭建

一般ELK平台分为集群和单机,由于手头机器资源部够,所以本篇文章中以单机搭建为例。

系统环境

<li>System: CentOS Linux release 7.1.1503 (Core)</li>

<li>ElasticSearch: 5.5.2</li>

<li>Logstash: 5.5.2</li>

<li>Kibana: 5.5.2</li>

<li>Java: openjdk version "1.8.0_141"</li>

注:由于Logstash的运行依赖于Java环境, 而Logstash 1.5以上版本不低于java 1.7,因此推荐使用最新版本的Java。因为我们只需要Java的运行环境,所以可以只安装JRE,不过这里我依然使用JDK,请自行搜索安装。

ELK软件下载

CentOS7搭建ELK日志集中分析平台

[root@tokyo software]# ls
elasticsearch-5.5.2.rpm  kibana-5.5.2-x86_64.rpm  logstash-5.5.2.rpm

Elasticsearch安装配置

[root@tokyo software]# rpm -ivh elasticsearch-5.5.2.rpm 
[root@tokyo software]# vim /etc/elasticsearch/elasticsearch.yml  #elasticsearch主配置文件
# ---------------------------------- Cluster (集群配置)-----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application  #集群名称
# ------------------------------------ Node(节点配置) ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1  #节点名称
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
# ----------------------------------- Paths (目录路径配置) ------------------------------------
# 
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data #数据存放目录
#
# Path to log files:
#
#path.logs: /path/to/logs #日志存放目录
#
# ----------------------------------- Memory (内存配置) -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network (网络配置)-----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1 #配置监听IP地址
#
# Set a custom port for HTTP:
#
#http.port: 9200  #配置监听端口
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 3
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

Logstash安装配置

CentOS7搭建ELK日志集中分析平台
其实它就是一个收集器而已,我们需要为它指定Input和Output(当然Input和Output可以为多个)。由于我们需要把Java代码中Log4j的日志输出到ElasticSearch中,因此这里的Input就是Log4j,而Output就是ElasticSearch。

[root@tokyo software]# rpm -ivh logstash-5.5.2.rpm 
[root@tokyo software]# vim /etc/logstash/conf.d/logstash-svr.conf # 定义logstash配置文件
# 定义日志来源
input {
  file {
    path => ["/data/wwwlogs/logstash_json.log"] #定义日志来源
    type => "nginx"  # 定义日志类型
    codec => json  # 定义日志编码格式
  }
}
# 对日志进行格式化(切片)
filter {
    mutate {
        split => [ "upstreamtime", "," ]
    }
    mutate {
        convert => [ "upstreamtime", "float" ]
    }
    geoip {
        source => "clientip"
        fields => ["ip","city_name","country_name"]
    }
}
# 定义日志输出目的地
output {
  elasticsearch {
  hosts => ["localhost:9200"]
  index => "logstash-%{+YYYY.MM.dd}"
        }
}

配置好了之后,可以使用logstash的命令测试一下配置文件是否无误。

[root@tokyo logstash]#  ./bin/logstash  -f /etc/logstash/conf.d/logstash-svr.conf

Kibana安装配置

[root@tokyo software]# rpm -ivh kibana-5.5.2-x86_64.rpm 

kibana一般会配置三个地方,根据自己的情况,参照如下配置。

#server.port: 5601 #配置kibana监听端口

# The URL of the Elasticsearch instance to use for all your queries.
#elasticsearch.url: "http://localhost:9200"
elasticsearch.url: "http://localhost:9200" #配置elasticsearch的地址和端口

# 如果安装了x-pack的话(后续再单独说),需要在这里配置elasticsearch的账号密码
# is proxied through the Kibana server.
#elasticsearch.username: "user"   
#elasticsearch.password: "pass"

此刻,您应该可以使用http://IP+5601访问到kibana了。

结束语

限于篇幅,此文中只是介绍了ELK和ELK的安装配置。在实际的工作中,其实他们都是可以相对自由组合,这些就不在本文介绍了。本人也是刚刚接触elk没多久,文中如有谬误,望请各位大神批评指正。

参考资料:

<li>IBM官方:https://www.ibm.com/developerworks/cn/opensource/os-cn-elk/</li>
<li>让编程成为一种习惯:http://www.cnblogs.com/beautiful-code/p/6125584.html</li>
<li>hello dog:https://wsgzao.github.io/post/elk/</li>


文章目录