狐狸小七 2019-02-13
日志分析平台有助于运维人员和程序员实现故障预警和故障分析,当下大多数程序员处理的方式为在日志文件中使用grep、awk来获取自己想要的信息。但是随着数据量的增加,在海量的数据日志当中,通过这种传统的方式处理,远远不能满足程序员的需求,往往存在这些问题:

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



以上是 ELK 技术栈的一个架构图。从图中可以清楚的看到数据流向:

注意:logs 泛指,各种日志文件以及日志信息:windows,negix,tomcat,webserver等等。
2.1 elasticsearch安装(以CentOS6为例)
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
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
yum install -y elasticsearch chkconfig --add elasticsearch
2.2 logstash安装
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
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
yum install -y logstash
2.3 kibana安装
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
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
yum install -y kibana chkconfig --add kibana
3.1 配置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
设置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"
}新版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/usr/share/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["IP地址:9200"] } }'在elasticsearch中查看logstash新加的索引
[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.confcurl -XPUT 'http://IP地址:9200/_all/_settings' -H 'Content-Type: application/json' -d '{
"index.number_of_replicas" : "1"
}'3.3 配置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"
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