ELK过程初步探索

一 :ELK= ElasticSearch + LogStash+Kibana

Elasticsearch,负责日志的存储和检索;

Elasticsearch 是一个实时的分布式搜索和分析引擎,它可以用于全文搜索,结构化搜索以及分析。它是一个建立在全文搜索引擎 Apache Lucene 基础上的搜索引擎,使用 Java 语言编写。

Logstash,负责日志的收集、过滤和格式化;

           Logstash 是一个具有实时渠道能力的数据收集引擎。使用 JRuby 语言编写。其作者是世界著名的运维工程师乔丹西塞

Kibana,负责日志数据的可视化;

Kibana 是一款基于 Apache 开源协议,使用 JavaScript 语言编写,为 Elasticsearch 提供分析和可视化的 Web 平台。它可以在 Elasticsearch 的索引中查找,交互数据,并生成各种维度的表图

Beats 是一类轻量级数据采集器 本文试用Filebeat 用来收集文件数据

Beats家族根据跟功能划分,主要有

Filebeat  负责收集文件数据

Packetbeat 负责收集网络流量数据

Metricbeat  负责收集系统级的 CPU 使用率、内存、文件系统、磁盘 IO 和网络 IO 统计数据。

安装java

换安装源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

sudo vim /etc/apt/sources.list

清华大学的安装源   https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

选择自己适合的源我这里是14.04

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

# 预发布软件源,不建议启用

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse

执行 apt-update

apt install default-jre

java -version

二  安装 elk

2.1 安装elasticsearch 默认端口9200

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.2.tar.gz

解压

tar  -xvf elasticsearch-5.6.2.tar.gz

启动

/home/jack/elk/elasticsearch-5.6.2/bin/elasticsearch -d

2.2 安装kibana   端口号 5601

wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.2-linux-x86_64.tar.gz

解压

 tar -xvf   kibana-5.6.2-linux-x86_64.tar.gz

编辑kibana.yml

server.port: 5601

server.host: "0.0.0.0"

启动

/home/jack/elk/kibana-5.6.2-linux-x86_64/bin/kibana &

netstat -nultp | grep 5601

在浏览器访问 http://127.0.0.1:5601

2.3 安装 logstash 端口号5046

wget https://artifacts.elastic.co/downloads/logstash/logstash-5.6.2.tar.gz

解压

tar -xvf /home/jack/elk/logstash-5.6.2.tar.gz

修改logstash.yml

path.config: /usr/local/elk/logstash/config/conf.d

启动

/home/jack/elk/logstash-5.6.2/bin/logstash &

测试 bin/logstash -e 'input { stdin { } } output { stdout {}}'

netstat -tulnp | grep 5046

2.4 安装 filebeat 这个不依赖JDK

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.6.2-linux-x86_64.tar.gz

解压

tar -xvf  filebeat-5.6.2-linux-x86_64.tar.gz

新建一个filebeat.test.yml

filebeat.prospectors:

- type: log

  paths:

    - /tmp/jack1.log

  tags: ["test1"]

  document_type: jack1

- type: log

  paths:

    - /tmp/jack2.log

  tags: ["test2"]

  document_type: jack2

output.elasticsearch:

  hosts: ["127.0.0.1:9200"]

  index: "test-filebeat"

配置说明:

filebeat.prospectors:

type 日志类型,默认log

input_type 输入类型,默认log

paths 采集的日志,可以使用通配符。支持多个

tags 自定义标签,是个数组。自定义

document_type 自定义字段,用于Logsatsh区分来源,在Logsatsh里用变量type表示

一个-表示一个filebeat.prospector,这里设置了2个。日志发送到elasticsearch,索引index 是test-filebeat。

这个要修改权限:chmod 600  filebeat.test.yml

启动/home/jack/elk/filebeat-5.6.2-linux-x86_64/filebeat -c filebeat.test.yml

配置完毕

三  开始 往paths里的配置文件,写数据

echo "jack is very handsome" >> /tmp/jack1.log

echo "jack is very handsome2 " >> /tmp/jack2.log

index 为test-filebeat

curl http://127.0.0.1:9200/test-filebeat/_search?q=*

应该会看到查询的数据以json格式显示出来

echo "aha,i think you r right" >> /tmp/jack1.log

curl http://127.0.0.1:9200/test-filebeat/_search?q=*&size=1&sort=@timestamp:desc

{

  "took": 0,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": 3,

    "max_score": null,

    "hits": [

      {

        "_index": "test-filebeat",

        "_type": "test1",

        "_id": "AWYL-BjvFzjnbYlB_DSQ",

        "_score": null,

        "_source": {

          "@timestamp": "2019-09-24T14:24:55.654Z",

          "beat": {  #filebeat 相关信息 数组类型

            "hostname": "210e5asfabce",

            "name": "2107e5dsfabce",

            "version": "5.6.2"

          },

          "input_type": "log",

          "message": "aha,i think you r right", #日志原文

          "offset": 1,#当前采集的日志的偏移量

          "source": "/tmp/jack1.log", #日志所在文件

          "tags": [ # 自定义标签,数组类型

            "jack1"

          ],

          "type": "jack1" #document_type字段定义的内容

        },

        "sort": [

          1537750095654

        ]

      }

    ]

  }

}

打开kibana web地址:http://127.0.0.1:5601,依次打开:Management

-> Kibana -> Index Patterns ,选择Create Index Pattern:

a. Index pattern 输入:test-filebeat ;

b. Time Filter field name 选择 @timestamp。

c. 点击Create。

filebeat内容发送到logstash

接下来,我们将日志使用filebeat发送到logstash,然后通过logstash处理后发送到ElasticSearch。

首先我们要修改上一节里filebeat的配置:

vim filebeat.test.yml

改为:

filebeat.prospectors:

- type: log

  paths:

    - /tmp/jack1.log

  tags: ["jack1"]

  document_type: jack1

- type: log

  paths:

    - /tmp/jack2.log

  tags: ["jack2"]

  document_type: jack2

output.logstash:

  hosts: ["127.0.0.1:5046"]

#output.elasticsearch:

#  hosts: ["127.0.0.1:9200"]

#  index: "test-filebeat"

我们把output.elasticsearch注释了,新增了output.logstash。

然后新增logstash配置:

vim logstash/config/conf.d/filebeat.test.conf

input {

    beats {

        port => 5046

    }

}

output {

    elasticsearch {

        hosts => ["127.0.0.1:9200"]

        index => "test-filebeat-%{type}"

    }

    stdout { codec => rubydebug }

}

这里的type变量就是filebeat里面的document_type。端口指定为5046(自定义即可),和filebeat里面配置的一致。logstash可以有多个子配置,所以也就能配置多个端口。此时,logstash是作为服务端运行的,filebeat是客户端。

接下来我们启动logstash和filebeat:

./logstash/bin/logstash &       &表示后台运行:

这样就能在kibana里面看到新写的日志了。

四 部署多台服务器

我这里只是在另外一台服务器(192.168.22.87)上 部署了filebeat

配置文件 只需稍加修改

filebeat.prospectors:

- type: log

  paths:

    - /home/vagrant/www/xcx_api/storage/logs/jack-*.log

  tags: ["jack_64"]

  document_type: jack_64

output.logstash:

  hosts: ["192.168.22.56:5046"]

把output.logstash 的hosts 改写为安装有elk的服务器的ip

这里先用telnet 192.168.22.56 5046 看下能连接成功不能,我第一次没成功是因为56这个服务器防火墙端口没开放

ufw allow 5046/tcp

ufw allow from ufw allow from 192.168.22.87 to any port 5046 开放端口

可以在/home/vagrant/www/xcx_api/storage/logs/ 下面新建一个文件

echo  "firstx "  >> jack-2019-09-27.log

这样就可以在 56的kibana上面查到firstx 这个消息了。

总结:

这次实验仅在自己的虚拟机上玩了一把,请勿在生产环境直接玩,

本次搭建了elk+filebeat,实现了多日的想玩elk的初步探索,利用filebeat收集信息,把内容输出到logstash,实现了两台服务器日志查询的简单配置。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注