logstash收集日志并写入Redis再到es集群

摘要:
Redis执行数据缓存图形架构:环境准备172.31.2.101es1+kibana172.31.2.102es172.31.2.103es3172.31.2.104日志堆栈172.31.2.105log堆栈172.31.2.10.6Redis172.31.107web1安装Redis[root@es-redis~]#Aptinstallredis-y更改Redis配置[root

redis做数据缓存

图形架构:

logstash收集日志并写入Redis再到es集群第1张

环境准备

172.31.2.101 es1 + kibana
172.31.2.102 es2
172.31.2.103 es3
172.31.2.104 logstash1
172.31.2.105 logstash2
172.31.2.106 Redis
172.31.2.107 web1

安装redis

[root@es-redis ~]# apt install redis -y

改redis 配置

[root@es-redis ~]# vim /etc/redis/redis.conf

bind 0.0.0.0
requirepass 123456

   save ""

#save 900 1
#save 300 10
#save 60 10000

重启

[root@es-redis ~]# systemctl restart redis

检查端口

[root@es-redis ~]# ss -tnl
6379

在web服务器Nginx-logstash配置改如下

建议把host写上

[root@es-redis ~]# vim /etc/logstash/conf.d/nginx-log-es.conf

input{
  file{
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
    stat_interval => 3
    type => "nginx-accesslog"
    codec => "json"
  }
}

output{
  if [type] == "nginx-accesslog" {
    redis {
      data_type => "list"
      host => "172.31.2.106"
      key => "nginx-accesslog"
      port => "6379"
      db => "1"
      password => "123456"                                   
  }}
}

重启

[root@es-redis ~]# systemctl restart logstash

访问nginx让其产生数据

在redis服务器测试

[root@es-redis ~]# redis-cli -h 172.31.2.106
172.31.2.106:6379> AUTH 123456
OK
172.31.2.106:6379> SELECT 1
OK
172.31.2.106:6379[1]> keys *
1) "nginx-accesslog"

172.31.2.106:6379[1]> LPOP nginx-accesslog

logstash服务器写到es 的配置

[root@es-web1 ~]# vim nginx-log-es.conf

input {
  redis {
    data_type => "list"
    key => "nginx-accesslog"
    host => "172.31.2.106"
    port => "6379"
    db => "1"
    password => "123456"                                    
    codec => "json"
  }
}

output {
  if [type] == "nginx-accesslog" {
    elasticsearch{
      hosts => ["172.31.2.101:9200"]
      index => "n826-long-nginx-accesslog-%{+YYYY.MM.dd}"
  }}
}

停止

[root@es-redis ~]# systemctl stop logstash.service

运行

[root@es-redis ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx-log-es.conf 

添加到kibana

创建视图

把Nginx错误日志也配置

[root@es-web1 ~]# cat /etc/logstash/conf.d/nginx-log-es.conf

input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
    stat_interval => 3
    type => "nginx-accesslog"
    codec => "json"
  }

  file {
    path => "/apps/nginx/logs/error.log"
    start_position => "beginning"
    stat_interval => 3
    type => "nginx-errorlog"
    #codec => "json"
  }
}

output {
  if [type] == "nginx-accesslog" {
    redis {
      data_type => "list"
      host => "172.31.2.106"
      key => "nginx-accesslog"
      port => "6379"
      db => "1"
      password => "123456"
  }}

  if [type] == "nginx-errorlog" {
    redis {
      data_type => "list"
      host => "172.31.2.106"
      key => "nginx-errorlog"
      port => "6379"
      db => "1"
      password => "123456"
  }}
}

重启

[root@es-redis ~]# systemctl restart logstash

制作错误日志信息

[root@es-web1 ~]# echo "error 654321 web" >> /apps/nginx/logs/error.log

[root@es-web1 ~]# echo "error 123456 web" >> /apps/nginx/logs/error.log

在把logstash写入es集群

[root@logstash1 ~]# cat /etc/logstash/conf.d/nginx-log-es.conf

input {
  redis {
    data_type => "list"
    key => "nginx-accesslog"
    host => "172.31.2.106"
    port => "6379"
    db => "1"
    password => "123456"
    codec => "json"
 }

  redis {
    data_type => "list"
    key => "nginx-errorlog"
    host => "172.31.2.106"
    port => "6379"
    db => "1"
    password => "123456"
 }
}

output {
  if [type] == "nginx-accesslog" {
    elasticsearch {
      hosts => ["172.31.2.101:9200"]
      index => "n826-long-nginx-accesslog-%{+YYYY.MM.dd}"
  }}

  if [type] == "nginx-errorlog" {
    elasticsearch {
      hosts => ["172.31.2.101:9200"]
      index => "n826-long-nginx-errorlog-%{+YYYY.MM.dd}"
  }}
}

重启

[root@es-redis ~]# systemctl restart logstash

当logstash去redis取数据,redis就会没有,如果数据多的话取一次就会少一次

logstash收集日志并写入Redis再到es集群第2张

logstash收集日志并写入Redis再到es集群第3张

logstash收集日志并写入Redis再到es集群第4张

免责声明:文章转载自《logstash收集日志并写入Redis再到es集群》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【JavaWeb项目】一个众筹网站的开发(四)后台用户注册功能php/phpmyadmin新手式环境搭建下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用 Elastic 技术栈构建 Kubernetes全栈监控

以下我们描述如何使用 Elastic 技术栈来为 Kubernetes 构建监控环境。可观测性的目标是为生产环境提供运维工具来检测服务不可用的情况(比如服务宕机、错误或者响应变慢等),并且保留一些可以排查的信息,以帮助我们定位问题。总的来说主要包括3个方面: 监控指标提供系统各个组件的时间序列数据,比如 CPU、内存、磁盘、网络等信息,通常可以用来显示系...

nginx配合IIS实现简单负载均衡

在linux下部署nginx这里就不多说了。这篇文章主要介绍nginx部署好后,如何配合iis实现负载均衡,没用过java不过原理应该是一样的。1.IIS 部署两个站点端口分别为8081和8082 8081站点和8082站点如下【随便写了个没有样式的很丑的页面】,我特意加了111和222区分 2.设置nginx配置文件,实现简单的负载均衡【推荐xsh...

Linux下查看Nginx的并发连接数和连接状态-乾颐堂

Linux下查看Nginx等的并发连接数和连接状态。 1、查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:  netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 或者: netstat -n | awk '/^tcp/ {++state[$NF...

nginx的平滑升级

一:解释nginx的平滑升级 随着nginx越来越流行,并且nginx的优势也越来越明显,nginx的版本迭代也来时加速模式,1.9.0版本的nginx更新了许多新功能,例如stream四层代理功能,伴随着nginx的广泛应用,版本升级必然越来越快,线上业务不能停,此时nginx的升级就是运维的工作了 Nginx方便地帮助我们实现了平滑升级。其原理简单概括...

使用podman容器部署飞儿云框架

首先 podman pull php:7.4-apache 删除之前运行的 podman rm -f 8082 然后 podman run -d --name 8082 -p 8082:80-v /home/firadio/firadio-yun-php/:/home/firadio/firadio-yun-php/-v /home/firadio/fir...

ELK&ElasticSearch5.1基础概念及配置文件详解【转】

1. 配置文件 elasticsearch/elasticsearch.yml 主配置文件 elasticsearch/jvm.options jvm参数配置文件 elasticsearch/log4j2.properties 日志配置文件 2. 基本概念 接近实时(NRT) Elasticsearch 是一个接近实时的搜索...