ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。前端
192.168.10.157 linux-node1 192.168.10.161 linux-node2
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch //导入密钥 vim elasticsearch.repo //配置yum源 [elasticsearch-2.x] name=Elasticsearch repository for 2.x packages baseurl=http://packages.elastic.co/elasticsearch/2.x/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enable=1 yum install elasticsearch -y yum install java -y (1.8版本) java -version //查看java版本
cd /etc/elasticsearch/ vim elasticsearch.yml cluster.name: xxy //17行 集群名称 node.name: linux-node1 //23行 节点名称 path.data: /data/es-data //33行 工做目录 path.logs: /var/log/elasticsearch/ bootstrap.memory_lock: true //43行 防止交换swap分区 network.host: 0.0.0.0 //54行 监听网络 http.port: 9200 //58行 端口
mkdir -p /data/es-data chown -R elasticsearch:elasticsearch /data/es-data/ //属主属组设为elasticsearch systemctl start elasticsearch.service netstat -ntap | grep 9200
测试 http://192.168.10.157:9200
curl -i -XGET 'http://192.168.175.132:9200/_count?pretty' -d '{"query": {"match_all": {}}}' #显示 HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 95 { "count" : 0, "_shards" : { "total" : 0, "successful" : 0, "failed" : 0 } }
/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head //安装位置/usr/share/elasticsearch/plugins/head 测试 http://192.168.10.157:9200/_plugin/head/
复合查询 /index-demo/test POST { "user":"xxy", "mesg":"hello world" } 提交请求
/index-demo/test/AWVDUuVUPJxKK7V6Dj8E GET {}
/index-demo/test/AWVDUuVUPJxKK7V6Dj8E DELETE {}
修改配置文件java
cd /etc/elasticsearch/ vim elasticsearch.yml cluster.name: xxy //17行 集群名称 node.name: linux-node2 //23行 节点名称 discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.10.157"] //69行 自动发现机制 启动elasticsearch 一样在linux-node1中配置 discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.10.161"] //69行 单播列表自动发现机制
http://192.168.10.161:9200/_plugin/head/ 会看到主分片和副本分片
/usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf 浏览器输入:http://192.168.175.132:9200/_plugin/kopf/#!/cluster
Logstash 是一个接收,处理,转发日志的工具。支持系统日志,webserver日志,错误日志,应用日志,总之包括全部能够抛出来的日志类型。在一个典型的使用场景下(ELK):用 Elasticsearch做为后台数据的存储,kibana用来前端的报表展现。Logstash在其过程当中担任搬运工的角色,它为数据存储,报表查询和日志解析建立了一个功能强大的管道链。Logstash 提供了多种多样的 input,filters,codecs 和output 组件,让使用者轻松实现强大的功能。node
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch cd /etc/yum.repos.d/ vim logstash.repo [logstash-2.1] name=Logstash repository for 2.1.x packages baseurl=http://packages.elastic.co/logstash/2.1/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enable=1 yum install logstash -y
cd /opt/logstash/ ./bin/logstash -e 'input { stdin{} } output { stdout{} }'
cd /opt/logstash/ ./bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } }'
/opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.10.157:9200"] } }' 输入事件 abc123 tom456 123jerry
在elasticsearch的web中点击链接查看,点击数据浏览选项卡能够查看到事件的信息linux
/opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.10.157:9200"] } stdout { codec => rubydebug } }'
vim /etc/logstash/conf.d/01-logstash.conf input { stdin { } } output { elasticsearch { hosts => ["192.168.10.157:9200"] } stdout { codec => rubydebug } } /opt/logstash/bin/logstash -f /etc/logstash/conf.d/01-logstash.conf
ln -s /opt/logstash/bin/logstash /usr/bin/ [root@localhost ~]# vim file.conf input { file { path => "/var/log/messages" type => "system" start_position => "beginning" } } output { elasticsearch { hosts => ["192.168.10.157:9200"] index => "system-%{+YYYY.MM.dd}" } } logstash -f /root/file.conf
vim file.conf input { file { path => "/var/log/messages" type => "system" start_position => "beginning" } file { path => "/var/log/elasticsearch/xxy.log" type => "es-error" start_position => "beginning" } } output { if [type] == "system" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "system-%{+YYYY.MM.dd}" } } if [type] == "es-error" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "es-error-%{+YYYY.MM.dd}" } } } logstash -f /root/file.conf
codec插件处理堆栈信息 //引用正则表达式 vim multiline.conf input { stdin { codec => multiline { pattern => "^\[" negate => true what => "previous" } } } output { stdout { codec => "rubydebug" } } logstash -f /root/multiline.conf
输入测试,识别事件 [1] [2] [abc] [abcd efghi jklmn] [3]
#从新定义file.conf input { file { path => "/var/log/messages" type => "system" start_position => "beginning" } file { path => "/var/log/elasticsearch/yun.log" type => "es-error" start_position => "beginning" codec => multiline { pattern => "^\[" negate => true what => "previous" } } } output { if [type] == "system" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "system-%{+YYYY.MM.dd}" } } if [type] == "es-error" { elasticsearch { hosts => ["192.168.10.157:9200"] index => "es-error-%{+YYYY.MM.dd}" } } } //添加多行日志内容进行验证 logstash -f /root/file.conf
概览web
Kibana 也是一个开源和免费的工具,Kibana能够为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,能够帮助汇总、分析和搜索重要数据日志。正则表达式
wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz //下载软件包 tar zxvf kibana-4.3.1-linux-x64.tar.gz -C /opt/ mv kibana-4.3.1-linux-x64/ /usr/local/ mv kibana-4.3.1-linux-x64/ kibana //重命名 vim /usr/local/kibana/config/kibana.yml //修改配置文件 server.port: 5601 //2行 server.host: "0.0.0.0" //5行 elasticsearch.url: "http://192.168.10.157:9200" //12行 ES地址 kibana.index: ".kibana" //20行
Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户能够经过该软件同时链接多个本地或远程的命令行会话,并在其间自由切换。GNU Screen能够看做是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。json
yum install screen -y /usr/local/kibana/bin/kibana //启动监听 #ctrl+a+d 进行丢入后台
浏览器访问bootstrap
http://192.168.10.157:5601/