前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >011.ELK使用Kafka做缓存收集Nginx日志

011.ELK使用Kafka做缓存收集Nginx日志

作者头像
CoderJed
发布2020-05-04 21:12:51
6220
发布2020-05-04 21:12:51
举报
文章被收录于专栏:Jed的技术阶梯Jed的技术阶梯

1. 流程说明

2. 配置过程

2.1 nginx配置

代码语言:javascript
复制
log_format json  '{"time_local": "$time_local", '
                          '"remote_addr": "$remote_addr", '
                          '"referer": "$http_referer", '
                          '"request": "$request", '
                          '"status": $status, '
                          '"bytes": $body_bytes_sent, '
                          '"agent": "$http_user_agent", '
                          '"x_forwarded": "$http_x_forwarded_for", '
                          '"up_addr": "$upstream_addr", '
                          '"up_host": "$upstream_http_host", '
                          '"upstream_time": "$upstream_response_time", '
                          '"request_time": "$request_time"}';
# 使用json日志格式
access_log  /var/log/nginx/access.log main;

2.2 filebeat配置

代码语言:javascript
复制
filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["access"]
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]
output.kafka:
  hosts: ["10.0.0.110:9092","10.0.0.111:9092","10.0.0.112:9092"]
  topic: nginx_log

2.3 logstash配置

代码语言:javascript
复制
input {
  kafka {
    bootstrap_servers => "10.0.0.110:9092,10.0.0.111:9092,10.0.0.112:9092"
    topics => ["nginx_log"]
    group_id => "logstash"
    codec => "json"
  }
}

filter {
  mutate {
    convert => ["upstream_time", "float"]
    convert => ["request_time", "float"]
  }
}

output {
    if "access" in [tags] {
      elasticsearch {
        hosts => "http://10.0.0.101:9200"
        manage_template => false
        index => "nginx_access-%{+yyyy.MM}"
      }
    }
    if "error" in [tags] {
      elasticsearch {
        hosts => "http://10.0.0.101:9200"
        manage_template => false
        index => "nginx_error-%{+yyyy.MM}"
      }
    }
}

3. 测试

  • 创建kafka topic
代码语言:javascript
复制

 [root@kafka01 ~]# /opt/kafka/bin/kafka-topics.sh --create --bootstrap-server 
10.0.0.110:9092,10.0.0.111:9092,10.0.0.111:9092 --replication-factor 3 --partitions 3 --topic nginx_log
 
  • 监听kafka topic
代码语言:javascript
复制

 [root@kafka03 ~]# /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 
10.0.0.110:9092,10.0.0.111:9092,10.0.0.112:9092 --topic nginx_log --from-beginning
 
  • 启动相关服务
代码语言:javascript
复制

 [root@nginx01 ~]# systemctl start nginx [root@es01 ~]# systemctl start elasticsearch 
[root@es01 ~]# systemctl start kibana [root@nginx01 ~]# systemctl start filebeat [root@es01 ~]# /usr/share/logstash/bin/logstash -f /root/logstash.yml
 
  • 发送测试请求
代码语言:javascript
复制

 [root@nginx01 opt]# ab -c 10 -n 1000 http://10.0.0.109:80/ [root@nginx01 opt]# ab -c 10 -n 1000 http://10.0.0.109:80/baidu
 
  • kafka-console-consumer消费到数据
代码语言:javascript
复制

 [root@kafka03 ~]# /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 
10.0.0.110:9092,10.0.0.111:9092,10.0.0.112:9092 
 --topic nginx_log --from-beginning 
{"@timestamp":"2020-04-27T09:09:34.585Z","@metadata":{
 "beat":"filebeat","type":"doc","version":"6.6.0","topic":"nginx_log"},
"beat":{"name":"nginx01","hostname":"nginx01","version":"6.6.0"},
 "x_forwarded":"-","source":"/var/log/nginx/access.log","bytes":153,
"request":"GET /baidu HTTP/1.0","status":404,"offset":552760,
 "up_host":"-","input":{"type":"log"},"time_local":"27/Apr/2020:17:09:34 +0800",
"tags":["access"],"host":{"name":"nginx01"},"log":{"file":{"path":"/var/log/nginx/access.log"}},
 "up_addr":"-","remote_addr":"10.0.0.109","request_time":"0.000",
"upstream_time":"-","referer":"-","agent":"ApacheBench/2.3","prospector":{"type":"log"}} 
 {"@timestamp":"2020-04-27T09:09:34.585Z","@metadata":{"beat":"filebeat",
"type":"doc","version":"6.6.0","topic":"nginx_log"},
"agent":"ApacheBench/2.3","request_time":"0.000","prospector":{"type":"log"},
"beat":{"version":"6.6.0","name":"nginx01","hostname":"nginx01"},"host":{"name":"nginx01"},
"status":404,"up_host":"-","remote_addr":"10.0.0.109","tags":["access"],
"source":"/var/log/nginx/access.log","log":{"file":{"path":"/var/log/nginx/access.log"}},
"up_addr":"-","referer":"-","request":"GET /baidu HTTP/1.0","x_forwarded":"-","bytes":153,
"time_local":"27/Apr/2020:17:09:34 +0800","input":{"type":"log"},"offset":553880,"upstream_time":"-"} ......
 
  • 查看kibana
代码语言:javascript
复制

 GET _cat/indices  yellow open nginx_access-2020.04            
apimPU-QTAmP7GeE7l8evQ 5 1 2000  0   689kb   689kb yellow open nginx_error-2020.04             
 WH3Lme3gQuqBSK8MFYfsSw 5 1 1000  0 754.2kb 754.2kb  GET nginx_access-2020.04/_search  
{   "took" : 4,   "timed_out" : false,   "_shards" : {     "total" : 5,     "successful" : 5,     
 "skipped" : 0,     "failed" : 0   },   "hits" : {     "total" : 1999,     "max_score" : 1.0,     
"hits" : [       {         "_index" : "nginx_access-2020.04",         "_type" : "doc",         
 "_id" : "avBpunEBINm9vG5xGD9v",         "_score" : 1.0,         "_source" : {           
"tags" : [             "access"           ],           "request" : "GET / HTTP/1.0",          
  "offset" : 246975,           "time_local" : "27/Apr/2020:14:49:37 +0800",           
"referer" : "-",           "beat" : {             "hostname" : "nginx01",             
  "version" : "6.6.0",             "name" : "nginx01"           },           "input" : {            
 "type" : "log"           },           "host" : {             "name" : "nginx01"           },     
        "status" : 200,           "up_addr" : "-",           "up_host" : "-",          
  "prospector" : {             "type" : "log"           },           "bytes" : 612,          
         "@version" : "1",           "agent" : "ApacheBench/2.3",          
   "upstream_time" : 0.0,           "request_time" : 0.0,           "@timestamp" :
          "2020-04-27T06:49:45.660Z",           "source" : "/var/log/nginx/access.log",          
    "log" : {             "file" : {               "path" : "/var/log/nginx/access.log"             }
                     },           "x_forwarded" : "-",           "remote_addr" : "10.0.0.109"         }       }     ]   } }
 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 流程说明
  • 2. 配置过程
    • 2.1 nginx配置
      • 2.2 filebeat配置
        • 2.3 logstash配置
        • 3. 测试
        相关产品与服务
        Elasticsearch Service
        腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档