1. 程式人生 > 實用技巧 >logstash 收集日誌到redis

logstash 收集日誌到redis

一、Logstash與Redis那點事

在企業中,日誌規模的量級遠遠超出我們的想象,這就是為什麼會有一家公司 日誌易 專門做日誌收集,給大型金融公司收集日誌,比如銀行,因為你有可能看到,1秒鐘好幾千萬的日誌量,往伺服器寫入,那麼企業中的叢集,架構都不是單臺的,而是多臺的,一臺如果是1千萬,那麼5臺的量級,10臺的量級,我們要對他們進行收集,進行分析,難免會在網路傳輸過程中,丟資料。

日誌是什麼?
日誌對於企業來說,有什麼作用?
使用者使用我們的產品,體驗如何?
使用者的客訴,我們能拿出什麼樣的資料來說話?
...

一系列的問題,都和日誌相關,如果至關重要的那個資料丟失了,那麼公司的損失可不僅僅是一條日誌那麼簡單。如果我們不知道,使用者對我們產品最感興趣的地方在哪,那麼產品的壽命也就越來越短。如果被攻擊了,惡意攻擊的IP源我們都找不到,那麼或許就不是產品的壽命越來越短,而是這個企業存在的壽命,越來越短。

好吧,一頓排比句,說的那麼浮誇,說白了,我就是想要告訴你們,一個大規模日誌量級的企業想要做到資料的安全性,資料的一致性,我們需要訊息佇列:Redis , Kafka,在ELK5版本中,建議使用Redis來做訊息佇列,Kafka能不能用?也能,只不過會有一些不必要的坑,需要我們去爬。在ELK6版本中,開始使用Kafka來做訊息佇列。

話不多說,我們接下來就開始將Logstash收集到的日誌,輸出到Redis中。

二,配置logstash收集單個日誌到redis

0.環境準備
主機 IP 服務
web01 10.0.0.7 nginx,tomcat,logstash
redis01 10.0.0.81 redis
redis02 10.0.0.82 logstash
es01 10.0.0.91 ES,kibana
1.收集日誌到redis
1)安裝redis
[root@redis01 ~]# yum install -y redis
2)配置redis
[root@redis01 ~]# vim /etc/redis.conf
bind 172.16.1.81 127.0.0.1
3)啟動redis
[root@redis01 ~]# systemctl start redis
4)配置logstash收集日誌寫入redis
[root@web01 ~]# vim /etc/logstash/conf.d/file_redis.conf
input {
  file {
    path => "/var/log/nginx/access.log"
    start_positon => "end"
    codec => "json"
  }
}

output {
  redis {
    host => "172.16.1.81"
    port => "6379"
    key => "nginx_log"
    data_type => "list"
  }
}

2.將redis資料取出寫入ES

1)安裝Java環境
[root@redis02 ~]# yum localinstall -y jdk-8u181-linux-x64.rpm
2)安裝logstash
[root@redis02 ~]# yum localinstall -y logstash-6.6.0.rpm
3)配置logstash取出redis資料寫入ES
[root@redis02 ~]# vim /etc/logstash/conf.d/redis_es.conf
input {
  redis {
    host => "172.16.1.81"
    port => "6379"
    data_type => "list"
    key => "nginx_log"
  }
}

output {
  elasticsearch {
    hosts => ["10.0.0.71:9200"]
    index => "nginx_redis_es_%{+YYYY-MM-dd}"
  }
}
4)啟動
[root@redis02 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis_es.conf

三,配置logstash收集多個日誌到redis

1)配置
[root@web01 ~]# cat /etc/logstash/conf.d/file_redis.conf
input {
  file {
    type => "nginx_log"
    path => "/var/log/nginx/access.log"
    start_position => "end"
    codec => "json"
  }
  file {
    type => "tomcat_log"
    path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
    start_position => "end"
    codec => "json"
  }
}

output {
  if [type] == "nginx_log" {
    redis {
      host => "172.16.1.81"
      port => "6379"
      key => "nginx_log"
      data_type => "list"
      db => "0"
    }
  }
  if [type] == "tomcat_log" {
    redis {
      host => "172.16.1.81"
      port => "6379"
      key => "tomcat_log"
      data_type => "list"
      db => "1"
    }
  }
}
2)啟動
[root@web01 ~]# logstash -f /etc/logstash/conf.d/file_redis.conf
3)訪問測試檢視redis資料
[root@redis01 ~]# redis-cli 
127.0.0.1:6379> KEYS *
1) "nginx_log"
127.0.0.1:6379> LLEN nginx_log
(integer) 12

127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> KEYS *
1) "tomcat_log"
127.0.0.1:6379[1]> LLEN tomcat_log
(integer) 18
3.配置logstash取出redis資料寫入ES
1)配置
[root@redis02 ~]# vim /etc/logstash/conf.d/redis_es.conf
input {
  redis {
    host => "172.16.1.81"
    port => "6379"
    data_type => "list"
    key => "nginx_log"
    db => "0"
  }
  redis {
    host => "172.16.1.81"
    port => "6379"
    data_type => "list"
    key => "tomcat_log"
    db => "1"
  }
}

output {
  if [type] == "nginx_log" {
    elasticsearch {
      hosts => ["10.0.0.71:9200"]
      index => "nginx_redis_es_%{+YYYY-MM-dd}"
    }
  }
  if [type] == "tomcat_log" {
    elasticsearch {
      hosts => ["10.0.0.71:9200"]
      index => "tomcat_redis_es_%{+YYYY-MM-dd}"
    }
  }
}
2)啟動
[root@redis02 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis_es.conf
3)檢視redis資料
127.0.0.1:6379[1]> LLEN tomcat_log
(integer) 55
127.0.0.1:6379[1]> LLEN tomcat_log
(integer) 0

127.0.0.1:6379[1]> SELECT 0
OK
127.0.0.1:6379> LLEN nginx_log
(integer) 0