logstash解耦之redis消息队列

logstash解耦之redis消息队列

架构图如下:

说明:通过input收集日志消息放入消息队列服务中(redis,MSMQ、Resque、ActiveMQ,RabbitMQ),再通过output取出消息写入ES上,kibana显示。

好处:松耦合,降低logstash收集日志的负载对业务服务不受影响,前后端分离,消息能存储不影响ES维护。

 下面我们就用redis做消息队列存储,架构如下:

#安装redis,修改redis配置文件,bind和protected-mode

1
2
3
4
5
6
7
8
9
10
11
12
[root@elk-node1 conf.d]# yum install -y redis
[root@elk-node1 conf.d]# cp /etc/redis.conf{,.bak}
[root@elk-node1 conf.d]# grep "^[a-z]" /etc/redis.conf
bind 192.168.247.135
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
...

#启动redis服务

1
2
3
4
5
6
7
8
[root@elk-node1 conf.d]# systemctl start redis
You have new mail in /var/spool/mail/root
[root@elk-node1 conf.d]# ss -lntp|grep 6379
LISTEN     0      511    192.168.247.135:6379                     *:*                   users:(("redis-server",pid=18387,fd=4))
You have new mail in /var/spool/mail/root
[root@elk-node1 conf.d]# grep "^[a-z]" /etc/redis.conf^C
[root@elk-node1 conf.d]# redis-cli -h 192.168.247.135
192.168.247.135:6379> exit

#编写测试文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@elk-node1 conf.d]# cat redis-out.conf
input{
    stdin{
 
}
}
output{
    redis{
    host => "192.168.247.135"
    port => "6379"
    db => "6"
    data_type => "list"
    key => "demo"
}
}

#logstash配置文件运行输入hello world
[root@elk-node1 conf.d]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf
Settings: Default filter workers: 1
hello world
#另开一个窗口登录redis可以看到一条我们刚输入的hello world消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@elk-node1 ~]# redis-cli -h 192.168.247.135
192.168.247.135:6379> info
Logstash startup completed
# Keyspace
db6:keys=1,expires=0,avg_ttl=0
192.168.247.135:6379> select 6
OK
192.168.247.135:6379[6]> key *
(error) ERR unknown command 'key'
192.168.247.135:6379[6]> keys *
1) "demo"
192.168.247.135:6379[6]> LINDEX demo -1
"{\"message\":\"hello world\",\"@version\":\"1\",\"@timestamp\":\"2018-07-28T06:44:50.418Z\",\"host\":\"elk-node1\"}"
192.168.247.135:6379[6]>

#接下来我们把消息写入ES,首先再输入多条消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@elk-node1 conf.d]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf
Settings: Default filter workers: 1
Logstash startup completed
fsadf
dgdf
gdg
ad
fd
ds
cd
g
rgergerg
rg
qrg
rh
rg
q
34tr
34
f
gdf
df
 
df
f
sdv
sdf
 
re
ter
t4
^CSIGINT received. Shutting down the pipeline. {:level=>:warn}
 
Logstash shutdown completed

#写一个输入到ES的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@elk-node1 conf.d]# cat redis-int.conf
input{
    redis{
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "demo"
}
}
output{
        elasticsearch {
           hosts => ["192.168.247.135:9200"]
           index => "redis-demo-%{+YYYY.MM.dd}"
        }
    }

#logstash配置文件运行
[root@elk-node1 conf.d]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/redis-int.conf
Settings: Default filter workers: 1
Logstash startup completed
#这时我们看redis上的消息已经被消费了
192.168.247.135:6379[6]> LLEN demo
(integer) 0
我们在登录ES可以看到已经有记录了

#写一个系统监控的配置文件把日志写入redis,inpout里读取日志消息,output里写入redis。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
[root@elk-node1 conf.d]# cat shipper.conf
input {
    file {
      path => "/var/log/messages"
      type => "system"
      start_position => "beginning"
    }
    file {
       path => "/var/log/elasticsearch/hejianlai.log"
       type => "es-error"
       start_position => "beginning"
      codec => multiline {
          pattern => "^\["
          negate => true
          what => "previous"
        }
    }
       file {
       path => "/var/log/nginx/access_json.log"
       codec => json
       start_position => "beginning"
       type => "nginx-log"
    }
    syslog{
    type => "system-syslog"
    host => "192.168.247.135"
    port => "514"
}
}
output {
      
    if [type] == "system"{
    redis{
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "system"
    }
}
    if [type] == "es-error"{
        redis{
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "es-error"
    }
}
       if [type] == "nginx-log"{
        redis{
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "nginx-log"
    }
}
       if [type] == "system-syslog"{
    redis{
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "system-syslog"
 
    }
}
}

 #运行配置文件
[root@elk-node1 conf.d]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/shipper.conf
#查看redis已经生成了相应的key
192.168.247.135:6379[6]> keys *
1) "system"
2) "nginx-log"
3) "es-error"
192.168.247.135:6379[6]>
#写一个配置文件从redis中把日志写入ES,inpout里读取redis消息,output里写入ES.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
[root@elk-node2 conf.d]# cat display.conf
input {
    redis{
    type => "system-syslog"
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "system-syslog"
 
    }
    redis{
      type => "system"
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "system"
 
    }
    redis{
       type => "es-error"
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "es-error"
 
    }
    redis{
       type => "nginx-log"
        host => "192.168.247.135"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "nginx-log"
 
    }
 
}
output {
      
    if [type] == "system"{
        elasticsearch {
           hosts => ["192.168.247.135:9200"]
           index => "systemlog-%{+YYYY.MM.dd}"
        }
    }
  
    if [type] == "es-error"{
        elasticsearch {
           hosts => ["192.168.247.135:9200"]
           index => "es-error-%{+YYYY.MM.dd}"
        }
    }
       if [type] == "nginx-log"{
        elasticsearch {
           hosts => ["192.168.247.135:9200"]
           index => "nginx-log-%{+YYYY.MM.dd}"
        }
    }
       if [type] == "system-syslog"{
        elasticsearch {
           hosts => ["192.168.247.135:9200"]
           index => "system-syslog-log-%{+YYYY.MM.dd}"
        }
    }
}

 #运行配置文件,就可以收集日志了。

[root@elk-node2 conf.d]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/display.conf &

到此logstash+redis+elasticsearch+kibana的架构搭建基本结束~~~~~

 

posted @   西门运维  阅读(1406)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示