项目目录
ELK日志分析系统简介
日志服务器
- 提高安全性
- 集中存放日志
- 缺陷
- 对日志的分析困难
ELK日志分析系统
- Elasticsearch
- Logstash
- Kibana
日志处理步骤
- 将日志进行集中化管理
- 将日志格式化(Logstash)并输出到Elasticsearch
- 对格式化后的数据进行索引和存储(Elasticsearch)
- 前端数据的展示(Kibana)
Elasticsearch介绍
Elasticsearch的概述
- 提供了一个分布式多用户能力的全文搜索引擎
Elasticsearch核心概念
- 接近实时
- 集群
- 节点
- 索引
1. 索引(库)→>类型(表)→>文档(记录) - 分片和副本
Logstash介绍
- 一款强大的数据处理工具
- 可实现数据传输、格式处理、格式化输出
- 数据输入、数据加工(如过滤,改写等)以及数据输出
LogStash主要组件
- Shipper
- lndexer
- Broker
- Search and Storage
- Web Interface
Kibana介绍
- 一个针对Elasticsearch的开源分析及可视化平台
- 搜索、查看存储在Elasticsearch索引中的数据
- 通过各种图表进行高级数据分析及展示
Kibana主要功能
- Elasticsearch无缝之集成
- 整合数据,复杂数据分析
- 让更多团队成员受益
- 接口灵活,分享更容易
- 配置简单,可视化多数据源
- 简单数据导出
部署ELK日志分析系统
ELK基本部署
实验环境:
俩台服务器都进行下面相同设置
1、设置映射
[root@node1 ~]# vim /etc/hosts
20.0.0.10 elk1
20.0.0.20 elk
2、下载ELK源
[root@node1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
3、设置ELK源
[root@node1 ~]# vim /etc/yum.repos.d/elasticsearch.repo
[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
4、安装elasticsearch
[root@node1 opt]# yum install elasticsearch -y
5、安装java
[root@node1 ~]# yum -y install java
6、修改elasticsearch文件
[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
17行去掉#并修改如下
cluster.name: elkabc #设置群集名称
23行去掉#并修改如下(节点名称对应主机名称)
node.name: node1 #设置节点名称(服务器2节点名称为:node2)
33行去掉#并修改如下
path.data: /data/es-data #设置工作目录
37行去掉#并修改如下
path.logs: /var/log/elasticsearch/
43行去掉#
bootstrap.memory_lock: true
54行去掉#并修改如下
network.host: 0.0.0.0 #设置网络监听
58行去掉#
http.port: 9200 #设置监听端口
7、创建目录
[root@node1 ~]# mkdir -p /data/es-data
8、设置属主和属组
[root@node1 ~]# chown -R elasticsearch:elasticsearch /data/es-data/
9、重启服务器
[root@node1 ~]# systemctl start elasticsearch.service
测试
1、客户机测试是否开启
2、使用命名验证是否能被访问
[root@node1 ~]# curl -i -XGET 'http://20.0.0.10: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
}
}
使用ES插件进行管理
俩台服务器都安装ES
1、安装ES
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
安装位置: /usr/share/elasticsearch/bin/plugin
2、服务器本机测试
ES设置群集
俩台服务器都进行下面设置
1、修改配置文件
[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
69行去掉#进行如下设置
discovery.zen.ping.unicast.hosts: ["20.0.0.10", "20.0.0.20"]
2、重启服务
[root@node1 ~]# systemctl restart elasticsearch.service
测试
查看日志
[root@node1 ~]# less /var/log/elasticsearch/elkabc.log
[2020-11-17 21:28:35,108][WARN ][bootstrap ] Unable to lock JVM Memory: error=12,reason=无法分配内存
解决无法分配内存(俩台服务器都进行如下设置)
[root@node1 ~]# vim /etc/security/limits.conf
在尾部增加
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
安装kopf插件(俩台服务器都要设置)
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
重启服务器
[root@node1 ~]# systemctl restart elasticsearch.service
测试
logstash部署(俩台服务器都进行如下设置)
1、下载logstash源
[root@node1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
2、设置logstash源
[root@node1 ~]# vim /etc/yum.repos.d/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
3、安装logstash
[root@node1 ~]# yum -y install logstash
//定义输入和输出流,类似管道
[root@node1 ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'
输入如下内容(//显示一个事件)
{
"message" => "hello",
"@version" => "1",
"@timestamp" => "2018-08-19T22:27:29.207Z",
"host" => "localhost.localdomain"
}
//写入到elasticsearch中
[root@node1 ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.175.132:9200"] } }'
输入如下事件
abc123
tom456
123jerry
在elasticsearch的web中点击连接查看
点击数据浏览选项卡可以查看到事件的信息
设置logstash日志
[root@node1 ~]# vim /etc/logstash/conf.d/01-logstash.conf
input { stdin { } }
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}
[root@node1 ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/01-logstash.conf
输入:abc111
logstash收集系统日志
[root@node1 ~]# 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 => ["20.0.0.10:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
[root@node1 ~]# logstash -f /root/file.conf
收集java异常日志
[root@node1 ~]# vim 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"
}
}
output {
if [type] == "system" {
elasticsearch {
hosts => ["20.0.0.10:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["20.0.0.10:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}
[root@node1 ~]# logstash -f /root/file.conf
事件优化处理
codec插件处理堆栈信息
//引用正则表达式
[root@node1 ~]# vim multiline.conf
input {
stdin {
codec => multiline {
pattern => "^\["
negate => true
what => "previous"
}
}
}
output {
stdout {
codec => "rubydebug"
}
}
[root@node1 ~]# logstash -f /root/multiline.conf
输入测试,识别事件
[1]
[2]
[abc]
[abcd
efghi
jklmn]
[3]
//重新定义file.conf
[root@node1 ~]# vim 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 => ["20.0.0.10:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["20.0.0.10:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}
//添加多行日志内容进行验证
[root@node1 ~]# logstash -f /root/file.conf