1 安装一个redis服务

[root@node4 ~]# yum -y install redis

直接启动

[root@node4 ~]# systemctl restart redis

[root@node4 ~]# systemctl status redis

[root@node4 ~]# redis-cli  -h 127.0.0.1

2 配置filebeat,把数据传给redis

[root@node4 ~]# vim /etc/filebeat/filebeat.yml

filebeat.inputs:
#####################################################
## Nginx log
#####################################################
- type: log
enabled: true
paths:
- /usr/local/nginx/logs/access.log
json.key_under_root: true
json.overwrite_keys: true
tags: ["access"] #- type: log
# enabled: true
# paths:
# - /usr/local/nginx/logs/error.log
# tags: ["error"] #####################################################
## tomcat log
#####################################################
- type: log
enabled: true
paths:
- /var/log/tomcat/localhost_access_log.*.txt
json.key_under_root: true
json.overwrite_keys: true
tags: ["tomcat"] #####################################################
## java log
#####################################################
- type: log
enabled: true
paths:
- /usr/local/elasticsearch/logs/my-elktest-cluster.log
tags: ["es-java"]
multiline.pattern: '^\['
multiline.negate: true
multiline.match: "after" #####################################################
## docker log
#####################################################
- type: docker
containers.ids:
- '*'
json.key_under_root: true
json.overwrite_keys: true
tags: ["docker"] #####################################################
## outout redis
#####################################################
output.redis:
hosts: ["127.0.0.1"]
key: "filebeat"
db: 0
timeout: 5

[root@node4 ~]# systemctl restart filebeat

访问产生日志

3 查看redis

127.0.0.1:6379> keys *
1) "filebeat"
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "filebeat"
127.0.0.1:6379> type filebeat #查看类型
list
127.0.0.1:6379> llen filebeat #查看长度
(integer) 22
127.0.0.1:6379> LRANGE filebeat 1 22
1) "{\"@timestamp\":\"2020-01-20T14:22:15.291Z\",\"@metadata\":{\"beat\":\"filebeat\",\"type\":\"_doc\",\"version\":\"7.4.2\"},\"agent\":{\"hostname\":\"node4\",\"id\":\"bb3818f9-66e2-4eb2-8f0c-3f35b543e025\",\"version\":\"7.4.2\",\"type\":\"filebeat\",\"ephemeral_id\":\"663027a7-1bdc-4a9f-b9d3-1297ef06c0b0\"},\"log\":{\"offset\":21185,\"file\":{\"path\":\"/usr/local/nginx/logs/error.log\"}},\"message\":\"2020/01/20 09:22:08 [error] 2790#0: *32 open() \\\"/usr/local/nginx/html/favicon.ico\\\" failed (2: No such file or directory), client: 192.168.132.1, server: localhost, request: \\\"GET /favicon.ico HTTP/1.1\\\", host: \\\"192.168.132.134\\\", referrer: \\\"http://192.168.132.134/\\\"\",\"tags\":[\"error\"],\"input\":{\"type\":\"log\"},\"ecs\":{\"version\":\"1.1.0\"},\"host\":{\"name\":\"node4\"}}"

使用json解析

{
"@timestamp": "2020-01-20T14:22:15.293Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "7.4.2"
},
"log": {
"offset": 21460,
"file": {
"path": "/usr/local/nginx/logs/access.log"
}
},
"json": {
"size": 612,
"xff": "-",
"upstreamhost": "-",
"url": "/index.html",
"domain": "192.168.132.134",
"upstreamtime": "-",
"@timestamp": "2020-01-20T09:22:08-05:00",
"clientip": "192.168.132.1",
"host": "192.168.132.134",
"status": "200",
"http_host": "192.168.132.134",
"Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",
"responsetime": 0,
"referer": "-"
},
"tags": ["access"],
"input": {
"type": "log"
},
"ecs": {
"version": "1.1.0"
},
"host": {
"name": "node4"
},
"agent": {
"id": "bb3818f9-66e2-4eb2-8f0c-3f35b543e025",
"version": "7.4.2",
"type": "filebeat",
"ephemeral_id": "663027a7-1bdc-4a9f-b9d3-1297ef06c0b0",
"hostname": "node4"
}
}

4 使用logstash收集消费resdis的数据

再node4节点安装logstash

[root@node4 ~]#  wget https://artifacts.elastic.co/downloads/logstash/logstash-7.5.1.rpm

[root@node4 ~]# rpm -ivh logstash-7.5.1.rpm

[root@node4 ~]# vim /etc/logstash/conf.d/logsatsh.conf

input {
redis {
host => "127.0.0.1"
port => "6379"
db => "0"
key => "filebeat"
data_type => "list"
}
}
filter{
mutate {
convert => ["upstream_time","float"]
convert => ["request_time","float"]
}
} output{
stdout {}
elasticsearch {
hosts => "192.168.132.131:9200"
manage_template => false
index => "nginx_access-%{+yyyy.MM.dd}"
}
}

[root@node4 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logsatsh.conf

已经有索引

访问

[root@node5 ~]# ab -n 20000 -c 20 http://192.168.132.134

查看redis

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> keys *
1) "filebeat"
127.0.0.1:6379> LLEN filebeat
(integer) 16875
127.0.0.1:6379> LLEN filebeat
(integer) 16000
127.0.0.1:6379> LLEN filebeat
(integer) 15125
127.0.0.1:6379> LLEN filebeat
(integer) 14375
127.0.0.1:6379> LLEN filebeat
(integer) 13625
127.0.0.1:6379> LLEN filebeat
(integer) 13000
127.0.0.1:6379> LLEN filebeat
(integer) 12375

使用kibana查看

{
"_index": "nginx_access-2020.01.20",
"_type": "_doc",
"_id": "A1J-w28BOF7DoSFdyQr8",
"_version": 1,
"_score": null,
"_source": {
"host": {
"name": "node4"
},
"tags": [
"access"
],
"input": {
"type": "log"
},
"ecs": {
"version": "1.1.0"
},
"log": {
"file": {
"path": "/usr/local/nginx/logs/access.log"
},
"offset": 12386215
},
"json": {
"host": "192.168.132.134",
"upstreamtime": "-",
"xff": "-",
"status": "200",
"referer": "-",
"http_host": "192.168.132.134",
"Agent": "ApacheBench/2.3",
"url": "/index.html",
"responsetime": 0,
"domain": "192.168.132.134",
"size": 612,
"clientip": "192.168.132.135",
"upstreamhost": "-",
"@timestamp": "2020-01-20T10:07:11-05:00"
},
"agent": {
"hostname": "node4",
"id": "bb3818f9-66e2-4eb2-8f0c-3f35b543e025",
"type": "filebeat",
"ephemeral_id": "efddca40-1d19-4036-9724-410b1b6d4c8b",
"version": "7.4.2"
},
"@version": "1",
"@timestamp": "2020-01-20T15:07:16.439Z"
},
"fields": {
"json.@timestamp": [
"2020-01-20T15:07:11.000Z"
],
"@timestamp": [
"2020-01-20T15:07:16.439Z"
]
},
"sort": [
1579532836439
]
}

5 filebeat添加错误日志

- type: log
enabled: true
paths:
- /usr/local/nginx/logs/error.log
tags: ["error"]
output.redis:
hosts: ["127.0.0.1"]
keys:
- key: "nginx_access"
when.contains:
tags: "access"
- key: "nginx_error"
when.contains:
tags: "error"

访问的错误日志

[root@node5 ~]# ab -n 20000 -c 200 http://192.168.132.134/hehe

127.0.0.1:6379> keys *
1) "nginx_error"
2) "nginx_access"

6 有两个key,配置logstash

[root@node4 ~]# cat /etc/logstash/conf.d/logsatsh.conf

input {
redis {
host => "127.0.0.1"
port => "6379"
db => "0"
key => "nginx_access"
data_type => "list"
}
redis {
host => "127.0.0.1"
port => "6379"
db => "0"
key => "nginx_error"
data_type => "list"
}
}
filter{
mutate {
convert => ["upstream_time","float"]
convert => ["request_time","float"]
}
} output{
stdout {}
if "access" in [tags]{
elasticsearch {
hosts => "192.168.132.131:9200"
manage_template => false
index => "nginx_access-%{+yyyy.MM.dd}"
}
}
if "error" in [tags]{
elasticsearch {
hosts => "192.168.132.131:9200"
manage_template => false
index => "nginx_error-%{+yyyy.MM.dd}"
}
}
}

[root@node4 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logsatsh.conf

7 启动后,redis被消费

127.0.0.1:6379> keys *
1) "nginx_error"
2) "nginx_access"
127.0.0.1:6379> LLEN nginx_error
(integer) 20000
127.0.0.1:6379> LLEN nginx_error
(integer) 20000
127.0.0.1:6379> LLEN nginx_error
(integer) 20000
127.0.0.1:6379> LLEN nginx_error
(integer) 20000
127.0.0.1:6379> LLEN nginx_error
(integer) 16125
127.0.0.1:6379> LLEN nginx_error
(integer) 15750
127.0.0.1:6379> LLEN nginx_access
(integer) 14625
127.0.0.1:6379> LLEN nginx_access
(integer) 14375
127.0.0.1:6379> LLEN nginx_error
(integer) 14000
127.0.0.1:6379> LLEN nginx_error

查看索引

8 优化配置

通过上面的传输,logstah重视通过tags来区分日志,所以再logstash的input中可以不配置两个key,只需要配置一个key即可

[root@node4 ~]# vim /etc/logstash/conf.d/logsatsh.conf

input {
redis {
host => "127.0.0.1"
port => "6379"
db => "0"
key => "nginx"
data_type => "list"
}
}
filter{
mutate {
convert => ["upstream_time","float"]
convert => ["request_time","float"]
}
} output{
stdout {}
if "access" in [tags]{
elasticsearch {
hosts => "192.168.132.131:9200"
manage_template => false
index => "nginx_access-%{+yyyy.MM.dd}"
}
}
if "error" in [tags]{
elasticsearch {
hosts => "192.168.132.131:9200"
manage_template => false
index => "nginx_error-%{+yyyy.MM.dd}"
}
}
}

filebeat配置

#####################################################
## outout redis
#####################################################
output.redis:
hosts: ["127.0.0.1"]
keys: "nginx"

[root@node4 ~]# systemctl restart filebeat

[root@node4 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logsatsh.conf

访问生成日志

[root@node5 ~]# ab -n 20000 -c 200 http://192.168.132.134/hehe

查看redis

127.0.0.1:6379> keys *
1) "nginx"
127.0.0.1:6379> LLEN nginx
(integer) 20125
127.0.0.1:6379> LLEN nginx
(integer) 19625
127.0.0.1:6379> LLEN nginx
(integer) 18250
127.0.0.1:6379> LLEN nginx
(integer) 17500
127.0.0.1:6379> LLEN nginx
(integer) 16750
127.0.0.1:6379> LLEN nginx
(integer) 0

查看索引

索引生成,实验完成

ELK学习实验019:ELK使用redis缓存的更多相关文章

  1. ELK学习实验014:Nginx日志JSON格式收集

    1 Kibana的显示配置 https://demo.elastic.co/app/kibana#/dashboard/welcome_dashboard 环境先处理干净 安装nginx和httpd- ...

  2. ELK学习实验020:ELK使用kafka缓存

    首先安装一个kafka集群,但是zookeeper使用单节点,可以让kafka快速跑起来,后续再研究kafka和zokkeeper的集群 1 安装Kafka集群 下面是三个节点都要做 [root@no ...

  3. ELK学习实验013:ELK的一个完整的配置操作

    前面做了关于ELK组件的各个实验,但是并没有真正的把各个组件结合起来做一个实验,现在使用一个脚本简单的生成日志,模拟生产不断产生日志的效果 一 流程说明 使用脚本产生日志,模拟用户的操作 日志的格式 ...

  4. ELK学习实验002:Elasticsearch介绍及单机安装

    一 简介 ElasticSearch是一个基于Luncene的搜索服务器.它提供了一个分布式多用户能力全文搜索引擎,基于RESTful web接口,ElsticSearch使用Java开发的,并作为A ...

  5. ELK学习实验005:beats的一些工具介绍

    一 背景需求 Nginx是一个非常优秀的web服务器,往往Nginx服务会作为项目的访问入口,那么,nginx的性能保障就会变得非常重要,如果nginx的运行出现了问题就会对项目有较大的影响,所以,我 ...

  6. ELK学习实验001:Elastic Stack简介

    1 背景介绍 在我们日常生活中,我们经常需要回顾以前发生的一些事情:或者,当出现了一些问题的时候,可以从某些地方去查找原因,寻找发生问题的痕迹.无可避免需要用到文字的.图像的等等不同形式的记录.用计算 ...

  7. ELK学习实验012:Logstash的安装和使用

    一 logstash安装 1.1下载包 [root@node1 ~]# cd /usr/local/src/ [root@node1 src]# wget https://artifacts.elas ...

  8. ELK学习实验011:Logstash工作原理

    Logstash事件处理管道包括三个阶段:输入→过滤器→输出.输入会生成事件,过滤器会对其进行修改,输出会将它们发送到其他地方.输入和输出支持编解码器,使您可以在数据进入或退出管道时对其进行编码或解码 ...

  9. ELK学习实验008:Kibana的介绍

    一 简介 Kiana是一款开源的数据分析和可视化平台,它是 Elastic Stack成员之一,设计用于和 Elasticsearch协作.您可以使用 Kiana对 Elasticsearch索引中的 ...

随机推荐

  1. Dynamic CRM登陆界面的客制化(持续更新)

    Dynamic CRM的登陆页面比较西化,不是很适合中国人使用.目前先把注销跳转的问题解决了. 服务端使用下面命令,将文件导出来 Export-AdfsWebTheme –Name default – ...

  2. 【笔记】《Redis设计与实现》chapter2 简单动态字符串

    ------------恢复内容开始------------ 2.1 SDS的定义 struct sdshdr{ // 记录buf数组中已使用字节的数量 // 等于SDS所保存字符串的长度(不含'\0 ...

  3. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作fedora27-18

    自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作fedora27-18 欢迎加QQ群:1026880196 进行交流学习   制作OpenSt ...

  4. 12.VUE - v-bind 详解

    v-bind指令用于给html标签设置属性. <!-- 完整语法 --> <a v-bind:href="url"></a> <!-- 缩 ...

  5. Python 并行计算那点事 -- 译文 [原创]

    Python 并行计算的那点事1(The Python Concurrency Story) 英文原文:https://powerfulpython.com/blog/python-concurren ...

  6. 【Feign/Ribbon】记录一次生产上的SpringCloudFeign的重试问题

    在上周在的微供有数项目中(数据产品),需要对接企业微信中第三方应用,在使用Feign的去调用微服务的用户模块用微信的code获取access_token以及用户工厂信息时出现Feign重试超时报错的情 ...

  7. pwn 好的联系网站

    https://pwnable.kr/ https://w3challs.com/challenges/wargame http://overthewire.org/wargames/ http:// ...

  8. adbi学习:安装和使用

    adbi 是一个android平台(arm 32 )的so注入+挂钩框架,源码开放在github上 :  ADBI 项目 .从github上下载来目录如下: 执行主目录下build.sh编译后目录如下 ...

  9. YII框架安装步骤(yii框架版本1.1.20,时间是2018/11)

    0x01 首先中文官网下载https://www.yiichina.com/download 0x02 解压压缩包到www目录下(方便以后调试) 0x02-1 如果想看一下你的电脑是否能匹配yii框架 ...

  10. 使用Windows全局钩子打造键盘记录器

    简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...