通过ELK快速搭建一个你可能需要的集中化日志平台
ELK是Elasticsearch、Logstash、Kibana的简称,这三者是核心套件,但并非全部。
Elasticsearch是实时全文搜索和分析引擎,提供搜集、分析、存储数据三大功能;是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统。它构建于Apache Lucene搜索引擎库之上。
Logstash是一个用来搜集、分析、过滤日志的工具。它支持几乎任何类型的日志,包括系统日志、错误日志和自定义应用程序日志。它可以从许多来源接收日志,这些来源包括 syslog、消息传递(例如 RabbitMQ)和JMX,它能够以多种方式输出数据,包括电子邮件、websockets和Elasticsearch。
Kibana是一个基于Web的图形界面,用于搜索、分析和可视化存储在 Elasticsearch指标中的日志数据。它利用Elasticsearch的REST接口来检索数据,不仅允许用户创建他们自己的数据的定制仪表板视图,还允许他们以特殊的方式查询和过滤数据

修改主机名称
hostname elk-node1
bash
需要有java环境
yum install -y java
[root@192 ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
systemctl stop firewalld.service //关闭防火墙
setenforce 0
安装elasticsearch软件
Elasticsearch安装可以通过YUM安装、源码包安装,这里通过下载的rpm包进行安装,生产环境中你可以根据实际情况进行安装方式选择,都很容易。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.rpm
rpm -ivh elasticsearch-6.6.0.rpm

设置自动开机启动
systemctl daemon-reload
systemctl enable elasticsearch.service

grep -v "^#" /etc/elasticsearch/elasticsearch.yml
cluster.name: my-elk-cluster //集群名字
node.name: elk-node1 //节点名字
path.data: /var/lib/elasticsearch // 数据存放路径
path.logs: /var/log/elasticsearch //日志存放路径
bootstrap.memory_lock: false //不在启动的时候锁定内存
network.host: 0.0.0.0 //提供服务绑定的IP地址,0.0.0.0代表所有地址
http.port: 9200 //侦听端口为9200
discovery.zen.ping.unicast.hosts: ["elk-node1", "elk-node2"] //集群发现通过单播实现
http.cors.enabled: true //开启跨域访问支持,默认为false
http.cors.allow-origin: "*" //跨域访问允许的域名地址
启动elasticsearch并查看是否成功开启
[root@elk-node1 ~]# systemctl start elasticsearch.service
查看服务开启状态
systemctl status elasticsearch
[root@elk-node1 ~]# netstat -antp | grep 9200

验证elasticsearch
通过浏览器访问节点,可以看到节点信息情况

通过浏览器中输入http://192.168.239.214:9200/_cluster/health?pretty查看集群的健康情况,可以看到status为green绿色。

通过浏览器中输入http://192.168.239.214:9200/_cluster/state?pretty查看集群的状态信息

配置head插件
#获取head包wget http://cdn.npm.taobao.org/dist/node/v10.15.1/node-v10.15.1-linux-x64.tar.gz
[root@elk-node1 ~]# tar xf node-v10.15.1-linux-x64.tar.gz -C /usr/src/
[root@elk-node1 ~]# ln -s /usr/src/node-v10.15.1-linux-x64/bin/node /usr/bin/node
[root@elk-node1 ~]# ln -s /usr/src/node-v10.15.1-linux-x64/bin/npm /usr/bin/npm
[root@elk-node1 ~]# node -v
v10.15.1
[root@elk-node1 ~]# npm -v
6.4.1
[root@elk-node1 ~]# npm install -g grunt-cli
/usr/src/node-v10.15.1-linux-x64/bin/grunt -> /usr/src/node-v10.15.1-linux-x64/lib/node_modules/grunt-cli/bin/grunt
+ grunt-cli@1.3.2
added 152 packages from 122 contributors in 57.684s
[root@elk-node1 ~]# ln -s /usr/src/node-v10.15.1-linux-x64/lib/node_modules/grunt-cli/bin/grunt
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
#编辑head目录下的Gruntfile.js文件
vim Gruntfile.js
connect: {
server: {
options: {
port: 9100, #修改默认端口
hostname: '*', #这个位置加入这个参数
base: '.',
keepalive: true
}
}
}
npm run start &

验证head
打开浏览器,进行验证通过浏览器进行访问: http://192.168.239.214:9100/,并连接集群

可以看到集群很健康,健康值为green绿色。点击数据浏览,可以看到索引、类型为空

[root@elk-node1 ~]# curl -XPUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'Content-Type: application/json' -d '{ "user": "zhangsan","mesg":"hello world" }'

刷新浏览器,可以看到创建成功的索引。


安装logstash
什么是Logstash
Logstash是一个开源的、接受来自多种数据源(input)、过滤你想要的数据(filter)、存储到其他设备的日志管理程序。Logstash包含三个基本插件inputfilteroutput,一个基本的logstash服务必须包含input和output.Logstash如何工作:
Logstash数据处理有三个阶段,input–>filter–>output.input生产数据,filter根据定义的规则修改数据,output将数据输出到你定义的存储位置。

数据生产商,包含以下几个常用输出:
l file: 从文件系统中读取文件,类似使用tail -0F
l syslog: syslog服务,监听在514端口使用RFC3164格式
l redis: 从redis服务读取,使用redis管道和列表。
l beats: 一种代理,自己负责收集好数据然后转发给Logstash,常用的如filebeat.
Filters:
filters相当一个加工管道,它会一条一条过滤数据根据你定义的规则,常用的filters如下:
l grok: 解析无规则的文字并转化为有结构的格式。
l mutate: 丰富的基础类型处理,包括类型转换、字符串处理、字段处理等。
l drop: 丢弃一部分events不进行处理,例如: debug events
l clone: 负责一个event,这个过程中可以添加或删除字段。
l geoip: 添加地理信息(为前台kibana图形化展示使用)
Outputs:
l elasticserache elasticserache接收并保存数据,并将数据给kibana前端展示。
l output 标准输出,直接打印在屏幕上。
安装logstash并做一些日志搜集输出到elasticsearch中
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.6.0.rpm
[root@elk-node1 ~]# rpm -ivh logstash-6.6.0.rpm

[root@elk-node1 ~]# systemctl start logstash.service
[root@elk-node1 ~]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin
logstash基本使用
Logstash使用管道方式进行日志的搜集处理和输出。有点类似linux系统的管道命令 xxx | ccc | ddd,xxx执行完了会执行ccc,然后执行ddd。在logstash中,包括了三个阶段:
输入input --> 处理filter(不是必须的) --> 输出output
启动一个logstash,-e:在命令行执行;input输入,stdin标准输入,是一个插件;output输出,stdout:标准输出
报错
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N【故障处理】
在虚拟机的设置中,将处理器的处理器核心数量改成2,重新执行启动命令后,能够正常运行。若还是未能执行成功,可进一步将处理器数量也改成2.

[root@elk-node1 ~]# logstash -e 'input { stdin{} } output { stdout{} }'
使用rubydebug显示详细输出,codec为一种编×××
[root@elk-node1 ~]# logstash -e 'input { stdin{} } output { stdout{ codec =>rubydebug} }'
使用logstash将信息写入到elasticsearch中
[root@elk-node1 ~]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=> ["192.168.239.214:9200"]} }'



logstash配置文件使用
logstash配置文件基本上由三部分组成,input、output以及用户需要才添加的filter,因此标准的配置文件格式如下:input {...}
filter {...}
output {...}
在每个部分中,也可以指定多个访问方式,例如我想要指定两个日志来源文件,则可以这样写:
input {
file { path =>"/var/log/messages" type =>"syslog"}
file { path =>"/var/log/apache/access.log" type =>"apache"}
}
下面是一个收集系统日志的配置文件例子,将其放到/etc/logstash/conf.d/目录中,logstash启动的时候便会加载。注意要给logstash读取日志文件的权限。
[root@elk-node1 ~]# chmod o+r /var/log/messages
[root@elk-node1 ~]# ll /var/log/messages
-rw----r--. 1 root root 642106 2月 12 11:41 /var/log/messages
[root@elk-node1 ~]# cd /etc/logstash/conf.d/
[root@elk-node1 conf.d]# vim system.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["192.168.239.214:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
重启logstash服务
[root@elk-node1 conf.d]# systemctl restart logstash.service
在浏览器中即可看到新加索引及其内容


安装kibana
在elk-node1服务器上安装kibana,并设置开机启动
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.6.0-x86_64.rpm
[root@elk-node1 ~]# rpm -ivh kibana-6.6.0-x86_64.rpm

[root@elk-node1 ~]# systemctl enable kibana.service
设置kibana的主配置文件/etc/kibana/kibana.yml
[root@elk-node1 ~]# grep -v "^#" /etc/kibana/kibana.yml | grep -v "^$"
server.port: 5601 // kibana打开的端口
server.host: "0.0.0.0" // kibana侦听的地址
elasticsearch.url: http://192.168.239.214:9200 //和elasticsearch建立联系
kibana.index: ".kibana" //在elasticsearch中添加.kibana索引
启动kibana服务
[root@elk-node1 ~]# systemctl start kibana.service
[root@elk-node1 ~]# netstat -antp | grep 5601

使用浏览器访问http://192.168.239.214:5601,第一次登录需要添加一个elasticsearch索引,我们添加前面的索引system-2019.02.12。
