ELK学习实验019:ELK使用redis缓存
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缓存的更多相关文章
- ELK学习实验014:Nginx日志JSON格式收集
1 Kibana的显示配置 https://demo.elastic.co/app/kibana#/dashboard/welcome_dashboard 环境先处理干净 安装nginx和httpd- ...
- ELK学习实验020:ELK使用kafka缓存
首先安装一个kafka集群,但是zookeeper使用单节点,可以让kafka快速跑起来,后续再研究kafka和zokkeeper的集群 1 安装Kafka集群 下面是三个节点都要做 [root@no ...
- ELK学习实验013:ELK的一个完整的配置操作
前面做了关于ELK组件的各个实验,但是并没有真正的把各个组件结合起来做一个实验,现在使用一个脚本简单的生成日志,模拟生产不断产生日志的效果 一 流程说明 使用脚本产生日志,模拟用户的操作 日志的格式 ...
- ELK学习实验002:Elasticsearch介绍及单机安装
一 简介 ElasticSearch是一个基于Luncene的搜索服务器.它提供了一个分布式多用户能力全文搜索引擎,基于RESTful web接口,ElsticSearch使用Java开发的,并作为A ...
- ELK学习实验005:beats的一些工具介绍
一 背景需求 Nginx是一个非常优秀的web服务器,往往Nginx服务会作为项目的访问入口,那么,nginx的性能保障就会变得非常重要,如果nginx的运行出现了问题就会对项目有较大的影响,所以,我 ...
- ELK学习实验001:Elastic Stack简介
1 背景介绍 在我们日常生活中,我们经常需要回顾以前发生的一些事情:或者,当出现了一些问题的时候,可以从某些地方去查找原因,寻找发生问题的痕迹.无可避免需要用到文字的.图像的等等不同形式的记录.用计算 ...
- ELK学习实验012:Logstash的安装和使用
一 logstash安装 1.1下载包 [root@node1 ~]# cd /usr/local/src/ [root@node1 src]# wget https://artifacts.elas ...
- ELK学习实验011:Logstash工作原理
Logstash事件处理管道包括三个阶段:输入→过滤器→输出.输入会生成事件,过滤器会对其进行修改,输出会将它们发送到其他地方.输入和输出支持编解码器,使您可以在数据进入或退出管道时对其进行编码或解码 ...
- ELK学习实验008:Kibana的介绍
一 简介 Kiana是一款开源的数据分析和可视化平台,它是 Elastic Stack成员之一,设计用于和 Elasticsearch协作.您可以使用 Kiana对 Elasticsearch索引中的 ...
随机推荐
- JavaScript深入理解-Set、Map、WeakSet和WeakMap
Set Set 对象允许储存任何类型的唯一值,无论是原始值或者是对象引用 本质:构造函数,用来生成 Set 数据结构 描述 Set 对象是值的集合,你可以按照插入的顺序迭代它的元素.Set 中的元素只 ...
- OO Unit2 总结
OO Unit2 总结 OO课Unit2电梯仿真项目技术回顾 BUAA.1823.邓新宇 2020/4/17 Part1 设计策略 从多线程的协同和同步控制方面,分析和总结自己三次作业的设计策略 第一 ...
- MySQL批量删除数据表
SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TABLES` WHERE table_schema='数据库 ...
- 关于Number、parseInt、isNaN转化参数
1.首先,关于NaN的相等判断 alert(NaN==NaN) //返回的是false: 2.isNaN 确定这个参数是否是数值或者是否可以被转化为数值:NaN是not a number 的缩写,所以 ...
- git 避免重复输入用户名密码问题解决
"store" 模式会将凭证用明文的形式存放在磁盘中,并且永不过期. 这意味着除非你修改了你在 Git 服务器上的密码,否则你永远不需要再次输入你的凭证信息. 这种方式的缺点是你的 ...
- animation几个比较好玩的属性(alternate,及animation-fill-mode)
<!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; backg ...
- POJ2226 不错的最小顶点覆盖
题意: 给你一个n * m 的矩阵,上面有" * " 和 " . " ,让你用少的木板吧所有" * "覆盖,木板宽度是1,长度 ...
- 解决Metasploit中shell乱码的问题
我们在kali中用 Metasploit 获取到windows主机的shell后,会出现乱码问题,归根到底,就是windows和linux系统的中文编码不同,所以导致windows系统上的中文在lin ...
- drbd虚拟机宕机恢复方法
问题现象 云南计算节点YN-ec-compute-19因系统盘损坏宕机且操作系统无法恢复,其上本地虚拟机无法疏散且无法迁移 拟采用drbd备份的数据对compute19上的虚拟机进行恢复 恢复方法 1 ...
- 【python】Leetcode每日一题-丑数
[python]Leetcode每日一题-丑数 [题目描述] 给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质因数 2.3 和 ...