ELK日志分析系统

ELK简介

Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

日志服务器

1、提高安全性
2、集中存放日志
缺陷:
1、对日志的分析困难

ELK日志处理步骤

1、将日志进行集中化管理
2、将日志格式化(Logstash)并传输到Elasticsearch
3、对格式化后的数据进行索引和存储(Eleasticsearch)
4、前端数据的展示(kibana)

Elasticsearch

概述

提供了一个分布式多用户能力的全文搜索引擎

核心概念

1、接近事实
2、集群
3、节点
4、索引
·索引(库)→类型(表)→文档(记录)
5、分片和副本

配置

两台机器的操作基本一样
1、添加主机映射node1和node2,IP地址分别为:20.0.0.10、20.0.0.20
2、查看Java环境,如果没有需要自己搞jdk
3、把elasticsearch压缩包放到/opt目录并解压
4、重载守护进程
5、设为开机自启

[root@node1 ~]# vim /etc/hosts
20.0.0.10 node1
20.0.0.20 node2
[root@node1 ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@node1 opt]# rpm -ivh elasticsearch-5.5.0.rpm
[root@node1 opt]# systemctl daemon-reload
[root@node1 opt]# systemctl enable elasticsearch.service 

6、备份配置文件
7、修改配置文件
8、创建数据存放目录
9、给权限
10、启动ela服务
11、查看端口有没有开启

[root@node1 ~]# cd /etc/elasticsearch/
[root@node1 elasticsearch]# cp elasticsearch.yml elasticsearch.yml.bak
[root@node1 elasticsearch]# vim /etc/elasticsearch/elasticsearch.yml
17行 clushter.name: my-elk-cluster
23行 node.name: node1
33行 path.data: /data/elk_data
37行 path.logs: /var/log/elasticsearch/
43行 bootstrap.memory_lock: false
55行 network.host: 0.0.0.0
59行 http.port: 9200
68行 discovery.zen.ping.unicast.hosts: ["node1","node2"]
[root@node1 elasticsearch]# mkdir -p /data/elk_data
[root@node1 elasticsearch]# chown elasticsearch:elasticsearch /data/elk_data/
[root@node1 elasticsearch]# systemctl start elasticsearch.service
[root@node1 elasticsearch]# netstat -anpt |grep 9200
##有可能需要等会哈

两台机器都做完之后测试

测试

12、宿主机访问
在这里插入图片描述

13、集群健康检查和状态
在这里插入图片描述

安装图形界面管理

1、上传node-v8.2.1.tar.gz到/opt目录
2、解压数据包并安装
3、上传phantomjs软件包到/opt目录下(前端框架)
4、解压并把程序命令让系统所能识别
5、安装elasticsearch-head包##数据可视化工具

[root@node1 ~]# yum -y install gcc gcc-c++ make
[root@node1 ~]# cd /opt
[root@node1 opt]# tar zxvf node-v8.2.1.tar.gz
[root@node1 opt]# cd node-v8.2.1/
[root@node1 node-v8.2.1]# ./configure
[root@node1 node-v8.2.1]# make -j3  ##这边需要的时间比较长
[root@node1 node-v8.2.1]# make install
##############################################
[root@node1 ~]# tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
cd phantomjs-2.1.1-linux-x86_64/bin
[root@node1 bin]# cp phantomjs /usr/local/bin
##################################################
[root@node1 ~]# tar zxvf elasticsearch-head.tar.gz
[root@node1 ~]# npm install

6、修改主配置文件

[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
##在文本最后加上
http.cors.enabled: true
http.cors.allow-origin: "*"

7、重启服务并开启elasticsearch-head服务

[root@node1 ~]# systemctl restart elasticsearch
[root@node1 ~]# cd /opt/elasticsearch-head
[root@node1 elasticsearch-head]# npm run start &
[root@node1 elasticsearch-head]# netstat -anpt |grep 9100

测试

网页访问:20.0.0.10:9100
显示图形界面了,在Elasticsearch 边上搜索栏输入http://20.0.0.10:9200/ 然后点连接

Logstash

介绍

一款强大的数据处理工具
可时间数据传输、格式处理、格式化输出
数据输入、数据加工(如过滤,改写等)以及数据输出

主要组件

1、Shipper
2、Indexer
3、Broker
4、Search and Storage
5、Web Interface

配置

第三台虚拟机IP:20.0.0.30
关防火墙,核心防护
改主机名为apache
把包放到/opt目录下,rpm安装
做个软连接让系统所能识别他的命令

[root@apache opt]# rpm -ivh logstash-5.5.1.rpm
[root@apache opt]# systemctl start logstash.service 
[root@apache opt]# systemctl enable logstash.service 
[root@apache opt]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin

测试

采用标准输入和输出的方式测试一下
[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{} }'
##需要稍微等一会
*******
输入:www.baidu.com
显示:2020-10-29T03:31:32.572Z apache www.baidu.com

使用rubydebug显示详细输出,codec是一种编解码器
[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
##需要稍微等一会
********
输入:www.baidu.com
显示:
{
    "@timestamp" => 2020-10-29T03:46:17.817Z,
      "@version" => "1",
          "host" => "apache",
       "message" => "www.baidu.com"
}

使用logstash将信息写入elasticsearch中
[root@apache opt]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["20.0.0.10:9200"] } }'
*********
输入:
www.baidu.com
www.sina.com.cn
www.google.com.cn
输出:
终端没有显示,要去宿主机的浏览器访问20.0.0.10:9100
下面输入:http://20.0.0.10:9200/点击连接
发现概览里面多出一个logstash-2020.10.29的索引
点击数据浏览选择logstash-2020.10.29的索引,右边显示框会显示我们在终端输入的三个地址。

做对接配置

logstash配置文件主要由三个部分组成:input、output、filter(根据需要)
[root@apache opt]# cd /var/log/
[root@apache log]# ll messages
找到messages文件
-rw-------. 1 root     root   1033039 10月 29 11:58 messages
给他加个其他用户可读的权限
[root@apache log]# chmod o+r messages
[root@apache log]# ll messages
-rw----r--. 1 root root 1053833 10月 29 12:00 messages

新建对接配置文件
[root@apache log]# vim /etc/logstash/conf.d/system.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}"
         }
        }

测试

去宿主机的浏览器访问20.0.0.10:9100
下面输入:http://20.0.0.10:9200/点击连接
发现概览里面多出一个system-2020.10.29的索引

kibana

介绍

一个针对Elasticsearch的开源分析及可视化平台
搜索、查看存储在Elasticsearch索引中的数据
通过各种图表进行高级数据分析及展示

主要功能

1、Elasticsearch无缝之集成
2、整合数据,复杂数据分析
3、让更多团队成员受益
4、接口灵活,分享更容易
5、配置简单,可视化多数据源
6、简单数据导出

配置

关防火墙,核心防护
主机名改为kibana
上传kibana包到/opt目录下
[root@kibana opt]# rpm -ivh kibana-5.5.1-x86_64.rpm 
去到kibana目录,做个备份再编辑配置文件
[root@kibana opt]# cd /etc/kibana/
[root@kibana opt]# cp kibana.yml kibana.yml.bak
[root@kibana kibana]# vim kibana.yml
第二行:server.port: 5601
第七行:server.host: "0.0.0.0"
二十一:elasticsearch.url: "http://20.0.0.10:9200"
三十行:kibana.index: ".kibana"

开启kibana服务,设为开机自启
[root@kibana kibana]# systemctl start kibana.service 
[root@kibana kibana]# systemctl enable kibana.service 

测试

宿主机输入20.0.0.40:5601
首次登录创建一个索引名字:system-* ##这是对接系统日志文件
Index name or pattern ##下面输入system-*
然后点最下面的create创建按钮
创建完成之后点击页面左上角的Discover界面就能看到system-*的信息
然后鼠标悬停再system-*下面一排里面的host上,会出现一个add按钮
点击add按钮,会发现右边界面只有time和host选项了,这样显示方式更友好。

配置:切换到apache主机

最后对接apache主机的apache日志文件(访问的,错误的)

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.10:9200"]
         index => "apache_access-%{+YYYY.MM.dd}"
         }
        }
        if [type] == "error" {
        elasticsearch {
         hosts => ["20.0.0.10:9200"]
         index => "apache_error-%{+YYYY.MM.dd}"
         }
        }
}

[root@apache conf.d]# /usr/share/logstash/bin/logstash -f apache_log.conf

测试

宿主机访问20.0.0.10:9100
连接http://20.0.0.10:9200/
可以看到索引信息多了两条
apache_error-2020.10.29
apache_access-2020.10.29

再开一个网页访问20.0.0.40:5601
左边选项最后一个Management ----> Index Patterns ---->Create Index Pattern
分别创建 apache_access-* 和 apache_error-* 的索引
创建完之后要等一段时间才能有数据显示。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值