ELK概述
Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash是一个完全开源的工具,他可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。
Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。
日志处理步骤
1.将日志进行集中化管理
2.将日志格式化(Logstash)并输出到Elasticsearch
3.对格式化后的数据进行索引和存储(Elasticsearch)
4.前端数据的展示(Kibana)
Elasticsearch概述
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。
- 提供了一个分布式多用户能力的全文搜索引擎
核心概念
- 接近实时
- 集群
- 节点
- 索引
- 索引(库)→>类型(表)→>文档(记录)
- 分片和副本
Logstash概述
- 一款强大的数据处理工具
- 可实现数据传输、格式处理、格式化输出
- 数据输入、数据加工(如过滤,改写等)以及数据输出
LogStash主要组件
- Shipper
- Indexer
- Broker
- Search and Storage
- Web Interface
Kibana介绍
- 一个针对Elasticsearch的开源分析及可视化平台
- 搜索、查看存储在Elasticsearch索引中的数据
- 通过各种图表进行高级数据分析及展示
Kibana主要功能
- Elasticsearch无缝之集成
- 整合数据,复杂数据分析
- 让更多团队成员受益
- 接口灵活,分享更容易
- 配置简单,可视化多数据源
- 简单数据导出
部署ELK日志分析系统
192.168.110.10 node01
192.168.110.15 node02
192.168.110.20 web01
三台查看主机java环境
[root@node01 ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@node01 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node01 ~]# systemctl stop firewalld
[root@node01 ~]# setenforce 0
[root@node01 ~]# vim /etc/sysconfig/selinux
SELINUX=disable
节点1、节点2 设置本地解析
[root@node01 ~]# vim /etc/hosts
192.168.110.10 node01
192.168.110.15 node02
节点1、节点2安装部署 elasticsearch
[root@node01 ~]# cd /opt/
[root@node01 opt]# ls
elasticsearch-5.5.0.rpm logstash-5.5.1.rpm
elasticsearch-head.tar.gz node-v8.2.1.tar.gz
kibana-5.5.1-x86_64.rpm phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node01 opt]# rpm -ivh elasticsearch-5.5.0.rpm
[root@node01 opt]# cp -p /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak ##保持良好的行为习惯,记得备份原配置文件
[root@node01 opt]# vim /etc/elasticsearch/elasticsearch.yml ##修改主配置文件
17 cluster.name: ELK #els群集名称
23 node.name: node01 #节点名
33 path.data: /data/elk_data #数据存放路径
37 path.logs: /var/log/elasticsearch #自身日志存放位置
43 bootstrap.memory_lock: false #启动时不锁定内存
55 network.host: 0.0.0.0 #监听全部IP
59 http.port: 9200 #端口
68 discovery.zen.ping.unicast.hosts: ["node01", "node02"] ##单播节点名
创建数据存放路径并更改属组属主
[root@node01 opt]# mkdir -p /data/elk_data
[root@node01 opt]# chown elasticsearch:elasticsearch /data/elk_data/
启动 ES
[root@node01 opt]# systemctl daemon-reload #加载系统服务
[root@node01 opt]# systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
[root@node01 opt]# systemctl start elasticsearch.service
[root@node01 ~]# netstat -ntap |grep 9200 #起不来就重启主机
tcp6 0 0 :::9200 :::* LISTEN 6677/java
检查健康:地址后方加入/_cluster/health?pretty
检查状态:地址后方加入/_cluster/state?pretty
安装 elasticsearch-head 插件
[root@node01 bao]# tar zxvf node-v8.2.1.tar.gz
[root@node01 bao]# cd node-v8.2.1/
[root@node01 node-v8.2.1]# ./configure
[root@node01 node-v8.2.1]# make -j4 ##用四线程编译,我用了十分钟左右
[root@node01 node-v8.2.1]# make install
安装phantomjs
[root@node01 bao]# tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node01 bao]# cd phantomjs-2.1.1-linux-x86_64/
[root@node01 phantomjs-2.1.1-linux-x86_64]# cd bin/
[root@node01 bin]# ls
phantomjs
[root@node01 bin]# cp phantomjs /usr/local/bin/
安装elasticsearch-head
[root@node01 bao]# tar zxvf elasticsearch-head.tar.gz
[root@node01 bao]# cd elasticsearch-head/
[root@node01 elasticsearch-head]# npm install
重新修改主配置文件 并 重启服务
[root@node01 elasticsearch-head]# cd /
[root@node01 /]# vim /etc/elasticsearch/elasticsearch.yml
#末行插入
http.cors.enabled: true #开启跨域访问支持,默认为false
http.cors.allow-origin: "*" #跨域访问允许的域名地址,支持使用正则
[root@node01 elasticsearch-head]# systemctl restart elasticsearch.service
启动elasticsearch-head 并 检测端口是否开启
[root@node01 elasticsearch-head]# npm run start &
[1] 64179
[root@node01 elasticsearch-head]#
> elasticsearch-head@0.0.0 start /bao/elasticsearch-head
> grunt server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
[root@node01 elasticsearch-head]# netstat -ntap |grep 9100
tcp 0 0 0.0.0.0:9100 0.0.0.0:* LISTEN 64189/grunt
用宿主机浏览器打开
创建索引,进行验证
[root@node01 elasticsearch-head]# curl -XPUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"Tony","mesg":"I am Iron Man"}'
{
"_index" : "index-demo",
"_type" : "test",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : false
}
后面不会了 复制别人的适当修改
20.0.0.11 主机名 node01
20.0.0.12 主机名 node02
20.0.0.3 主机名 web01
Logstash
在web01上安装httpd服务,并且部署安装 logstash
[root@web01 ~]# yum -y install httpd
[root@web01 ~]# systemctl start httpd
#下面是安装 logstash
[root@web01 opt]# rpm -ivh logstash-5.5.1.rpm
[root@web01 opt]# systemctl start logstash.service
[root@web01 opt]# systemctl enable logstash.service
Created symlink from /etc/systemd/system/multi-user.target.wants/logstash.service to /etc/systemd/system/logstash.service.
[root@web01 opt]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
与ES 节点进行对接测试
Logstash这个命令测试
字段描述解释:
-f 通过这个选项可以指定logstash的配置文件,根据配置文件配置logstash
-e 后面跟着字符串 该字符串可以被当做logstash的配置(如果是” ”,则默认使用stdin做为输入、stdout作为输出)
-t 测试配置文件是否正确,然后退出
[root@web01 opt]# logstash -e 'input { stdin{} } output { stdout{} }'
#省略部分…………
The stdin plugin is now waiting for input:
15:03:07.416 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
2020-09-15T07:03:41.604Z web01 www.baidu.com
helloworld
2020-09-15T07:03:46.225Z web01 helloworld
ruby 测试
codec为一种编解码器
[root@web01 opt]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
The stdin plugin is now waiting for input:
15:08:05.669 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
{
"@timestamp" => 2020-09-15T07:08:16.417Z,
"@version" => "1",
"host" => "web01",
"message" => "www.baidu.com"
}
使用 logstash 将信息写入 ES 中
[root@web01 opt]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["20.0.0.12:9200"] } }'
The stdin plugin is now waiting for input:
15:12:34.073 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
www.CSDN.com
hello world
对接配置
Logstash配置文件主要由三部分组成:input、output、filter(根据需要来处理)
[root@web01 opt]# ll /var/log/messages
-rw-------. 1 root root 742933 9月 15 15:18 /var/log/messages
[root@web01 opt]# chmod o+r /var/log/messages
[root@web01 opt]# cd /etc/logstash/conf.d/
[root@web01 conf.d]# ls
[root@web01 conf.d]# vim system.conf
input {
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["20.0.0.12:9200"]
index => "system-%{+YYYY.MM.dd}" #索引名
}
}
[root@web01 conf.d]# systemctl restart logstash.service
Kibana
[root@node01 opt]# rpm -ivh kibana-5.5.1-x86_64.rpm
[root@node01 opt]# cd /etc/kibana/
[root@node01 kibana]# ls
kibana.yml
[root@node01 kibana]# cp kibana.yml kibana.yml.bak
[root@node01 kibana]# vim kibana.yml
2 server.port: 5601 #端口
7 server.host: "0.0.0.0" #监听全部IP
21 elasticsearch.url: "http://20.0.0.12:9200" #与ES建立联系
30 kibana.index: ".kibana" #在ES中添加 .kibana 索引
[root@node01 kibana]# systemctl start kibana.service
[root@node01 kibana]# systemctl enable kibana.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /etc/systemd/system/kibana.service.
输入 IP:5601 验证
对接 web 主机
[root@web01 conf.d]# vim web_log.conf
input {
file{
path => "/etc/httpd/logs/access_log"
type => "access"
start_position => "beginning"
}
file{
path => "/etc/httpd/logs/error_log"
type => "error"
start_position => "beginning"
}
}
output {
if [type] == "access" {
elasticsearch {
hosts => ["20.0.0.12:9200"]
index => "apache_access-%{+YYYY.MM.dd}"
}
}
if [type] == "error" {
elasticsearch {
hosts => ["20.0.0.12:9200"]
index => "apache_error-%{+YYYY.MM.dd}"
}
}
}
[root@web01 conf.d]# logstash -f web_log.conf #指定配置文件做测试
多了两个 索引
进入 kibana 创建对应 索引,就可以使用图形界面查看日志分析了