ELK日志分析系统部署

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                      ⭐ELK日志分析系统简介⭐

(1)日志服务器

提高安全性

集中存放日志

缺陷:对日志的分析困难

ELK日志分析系统部署

收集数据:LogstashAgent

建立索引:ElasticSearchCluster

数据可视乎:KilbanaServer

(2)ELK日志分析系统

Elasticsearch

Logstash

Kibana

(3)日志处理步骤

将日志进行集中化管理

将日志格式化( Logstash )并输出到Elasticsearch

对格式化后的数据进行索引和存储( Elasticsearch )

前端数据的展示( Kibana )

2、Elasticsearch介绍

(1)Elasticsearch的概述

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

(2)Elasticsearch的概念

接近实时

集群

节点

索引:索引(库)-->类型(表)-->文档(记录)

分片和副本

3、Logstash介绍

(1)Logstash介绍

一款强大的数据处理工具,可以实现数据传输、格式处理、格式化输出

数据输入、数据加工(如过滤,改写等)以及数据输出

(2)LogStash主要组件

Shipper

Indexer

Broker

Search and Storage

Web Interface

4、Kibana介绍

(1)Kibana介绍

一个针对Elasticsearch的开源分析及可视化平台

搜索、查看存储在Elasticsearch索引中的数据

通过各种图表进行高级数据分析及展示

(2)Kibana主要功能

Elasticsearch无缝之集成

整合数据,复杂数据分析

让更多团队成员受益

接口灵活,分享更容易

配置简单,可视化多数据源

简单数据导出

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                  ⭐部署ELK日志分析系统⭐

(1)需求描述

配置ELK日志分析群集

使用Logstash收集日志

使用Kibana查看分析日志

(2)准备安装环境

关闭防火墙和Selinux

Node1、Node2节点内存分配4G,Apache节点分配1G内存

通过VMware虛拟网络Vmnet8连接

(3)安装步骤分析

环境准备

部署Elasticsearch软件

安装Elasticsearch-head插件

Logstash安装及使用方法

安装Kibana

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                      ⭐环境部署:⭐

[[email protected] ~]# hostnamectl set-hostname node1                    //更改主机名

[[email protected] ~]# systemctl stop firewalld.service                  //关闭防火墙
[[email protected] ~]# setenforce 0

[[email protected] ~]# vim /etc/hosts                       

192.168.35.142 node1
192.168.35.182 node2

[[email protected] ~]# 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)

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                 ⭐部署elasticsearch软件⭐

1、安装elasticsearch-5.5.0.rpm
mkdir /abc
mount.cifs //192.168.100.7/rpm /abc/
cd /abc/elk/
rpm -ivh elasticsearch-5.5.0.rpm

2、加载系统服务
systemctl daemon-reload
systemctl enable elasticsearch.service 

3、更改elasticsearch主配置文件
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
vim /etc/elasticsearch/elasticsearch.yml

17/ cluster.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                        #提供服务绑定的IP地址,0.0.0.0代表所有地址
59/ http.port: 9200                       #侦听端口为9200
68/ discovery.zen.ping.unicast.hosts: ["node1", "node2"]        #集群发现通过单播实现

grep -v "^#" /etc/elasticsearch/elasticsearch.yml
cluster.name: my-elk-cluster
node.name: node1
path.data: /data/elk_data
path.logs: /var/log/elasticsearch/
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["node1", "node2"]

4、创建数据存放路径并授权
mkdir -p /data/elk_data
chown elasticsearch:elasticsearch /data/elk_data

5、启动elasticsearch是否成功开启
systemctl start elasticsearch.service 
netstat -natp | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      125554/java  

6、查看节点信息,用真机的浏览器打开http://192.168.35.142:9200,有文件打开,下面是节点的信息

{
  "name" : "node1",
  "cluster_name" : "my-elk-cluster",
  "cluster_uuid" : "mi3-z72CRqS-ofc4NhjXdQ",
  "version" : {
    "number" : "5.5.0",
    "build_hash" : "260387d",
    "build_date" : "2017-06-30T23:16:05.735Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                         ⭐安装elasticsearch-head插件⭐

1、编译安装node组件依赖包
cd /abc/elk/
tar zxvf node-v8.2.1.tar.gz -C /opt/

cd /opt/
yum install gcc gcc-c++ make -y

cd node-v8.2.1/
./configure
make && make install

2、安装phantomjs——前端框架
cd /abc/elk/
tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/src/

cd /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin/
cp phantomjs /usr/local/bin/

3、安装elasticsearch-head——数据可视化工具
cd /abc/rpm/elk/
tar zxvf elasticsearch-head.tar.gz -C /usr/local/src/

cd /usr/local/src/elasticsearch-head/
npm install

4、修改主配置文件
vim /etc/elasticsearch/elasticsearch.yml
//在配置文件末尾插入
http.cors.enabled: true
http.cors.allow-origin: "*"

systemctl restart elasticsearch.service 

5、启动
cd /usr/local/src/elasticsearch-head/
[[email protected] elasticsearch-head]# npm run start &        //切换到后台运行
[1] 28270
[[email protected] elasticsearch-head]# 
> [email protected] start /usr/local/src/elasticsearch-head
> grunt server

Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

[[email protected] ~]# netstat -lnupt | grep 9100
tcp        0      0 0.0.0.0:9100            0.0.0.0:*               LISTEN      44696/grunt         
[[email protected] ~]# netstat -lnupt | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      11956/java          

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                    ⭐真机上打开浏览器⭐

输入http://192.168.35.131:9100/可 以看见群集很健康是绿色
在Elasticsearch后面的栏目中输入http://192.168.35.131:9200
然后点连接会发现:集群健康值: green (0 of 0)
node1信息动作
node2信息动作

ELK日志分析系统部署

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                     ⭐登录node1主机⭐

索引为index-demo,类型为test,可以看到成功创建
[[email protected] ~]# curl -XPUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
{
  "_index" : "index-demo",
  "_type" : "test",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                    ⭐真机上打开浏览器⭐

输入http://192.168.35.142:9100/    查看索引信息

node1 信息动作   01234
node2 信息动作   01234

上面图可以看见索引默认被分片5个,并且有一个副本:
点击数据浏览一会发现在node1上创建的索引为index-demo,类型为test,相关的信息

ELK日志分析系统部署

ELK日志分析系统部署

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                    ⭐登录主机apache⭐

安装logstash并做一些日志搜集输出到elasticsearch中:

1、更改主机名:
hostnamectl set-hostname apache

2、关闭防火墙:
systemctl stop firewalld.service
setenforce 0

3、安装Apache服务(httpd)
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# systemctl start httpd.service

4、安装Java环境
[[email protected] ~]# java -version      //如果没装,安装yum install -y java
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)

5、安装logstash
[[email protected] ~]# mount.cifs //192.168.100.7/rpm /abc/
Password for [email protected]//192.168.100.7/rpm:  
[[email protected] ~]# cd /abc/elk/
[[email protected] elk]# rpm -ivh logstash-5.5.1.rpm 
[[email protected] elk]# systemctl start logstash.service                 //开启服务
[[email protected] elk]# systemctl enable logstash.service              //设为开机自启
[[email protected] elk]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/       //建立logstash

6、logstash(apache)和elasticsearch(node)功能是否正常,做对接测试
用logstash命令测试:(字段描述解释)
-f:通过这个选项可以指定logstash的配置文件,根据配置文件配置logstash.
-e:后面跟着字符串,该字符串可以被当做logstash的配置(如果是””, 则默认使用stdin做为输入、stdout作为输出)
-t:测试配置文件是否正确,然后退出

7、输入采用标准输入输出采用标准输出,登录192.168.35.142,在Apache服务器上
[[email protected] elk]# logstash -e 'input { stdin{} } output { stdout{} }'
//省略
17:19:26.109 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com                   //输入www.baidu.com
2019-12-18T09:20:34.209Z apache www.baidu.com
www.sina.com.cn                 //输入www.sina.com.cn 
2019-12-18T09:20:47.380Z apache www.sina.com.cn

8、使用rud解ebuy显示详细输出,codec为一种编码器
[[email protected] elk]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug }}'
//省略
17:29:30.023 [[main]-pipeline-manager] INFO  logstash.pipeline - Pipeline main started
The stdin plugin is now waiting for input:
17:29:30.065 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com                        //输入www.baidu.com
{
    "@timestamp" => 2019-12-18T09:30:19.273Z,
      "@version" => "1",
          "host" => "apache",
       "message" => "www.baidu.com"
}
www.sina.com.cn                      //输入www.sina.com.cn
{
    "@timestamp" => 2019-12-18T09:32:09.568Z,
      "@version" => "1",
          "host" => "apache",
       "message" => "www.sina.com.cn"
}

8、使用logstash将信息写入elasticsearch中:输入、输出、对接
[[email protected] elk]# logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.35.142:9200"] } }'
//省略
17:37:49.537 [[main]-pipeline-manager] INFO  logstash.pipeline - Pipeline main started
The stdin plugin is now waiting for input:
17:37:49.574 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com              //输入内容
www.sina.com.cn            //输入内容

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                   ⭐真机上打开浏览器⭐

输入http://192.168.35.142:9100/查看索引信息
多出logstash-2019.12.18
点击数浏览查看响应的内容

ELK日志分析系统部署

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                         ⭐登录Apache主机,做对接配置⭐

1、logstash配置文件

logstash配置文件由三部分组成:input、output、filter

[[email protected] log]# chmod o+r messages 
[[email protected] log]# ll /var/log/messages 
-rw----r--. 1 root root 483737 Dec 18 17:54 /var/log/messages

[[email protected] log]# vim /etc/logstash/conf.d/system.conf
input {
        file{
          path => "/var/log/messages"
          type => "system"
          start_position => "beginning"
          }
        }
output {
        elasticsearch {
          hosts => ["192.168.35.142:9200"]
          index => "system-%{+YYYY.MM.dd}"
          }
        }

[[email protected] log]# systemctl restart logstash.service    //重启服务

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                        ⭐真机上打开浏览器⭐

输入http://192.168.35.142:9100/查看索引信息

多出:system-2019.12.18

ELK日志分析系统部署

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                            ⭐登录node1⭐

在node1主机上安装kibana

[[email protected] ~]# cd /abc/elk/
[[email protected] elk]# rpm -ivh kibana-5.5.1-x86_64.rpm 
[[email protected] kibana]# cp kibana.yml kibana.yml.bak
[[email protected] kibana]# vim kibana.yml
2/ server.port: 5601                 //打开端口
7/ server.host: "0.0.0.0"                    //监听的地址
21/ elasticsearch.url: "http://192.168.35.142:9200"      //与elasticsearch建立联系
30/ kibana.index: ".kibana"                  //在elasticsearch中添加.kibana索引

[[email protected] kibana]# systemctl start kibana.service         //开启kibana服务
[[email protected] kibana]# systemctl enable kibana.service       //设为开机自启动

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                     ⭐真机上打开浏览器⭐

在浏览器输入http://192.168.35.142:5601/
首次登录时创建一个索引(对接系统日志文件):
输入:system-*

点击create按钮进行创建

ELK日志分析系统部署

点击Discover按钮,会发现system-*信息

ELK日志分析系统部署

点击下面的host旁边的add按钮,会发现只有time和host选项,比较友好

ELK日志分析系统部署

ELK日志分析系统部署

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                      ⭐登录Apache主机⭐

Apache日志文件(访问的、错误的)

[[email protected] log]# cd /etc/logstash/conf.d/
[[email protected] conf.d]# vim apache_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 => ["192.168.35.142:9200"]
          index => "apache_access-%{+YYYY.MM.dd}"
          }
        }
        if [type] == "error" {
        elasticsearch {
          hosts => ["192.168.35.142:9200"]
          index => "apache_error-%{+YYYY.MM.dd}"
          }
        }
        }

[[email protected] conf.d]# /usr/share/logstash/bin/logstash -f apache_log.conf 

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

                                                           ⭐登录node主机⭐

http://192.168.35.175/

ELK日志分析系统部署

http://192.168.35.142:9100/

多了apache_access-2019.12.18和apache_error-2019.12.18

ELK日志分析系统部署