海量日志架构中的后端数据处理技术对比

1,913 阅读15分钟
原文链接: www.infoq.com

随着ELK技术的普及,Elasticsearch所提供的强大搜索、分析功能给大家处理各种类型的海量数据提供了可能。随之而来的是如何将各种类型的海量数据以一种通用、便捷、高效的方式进入到ES供其使用。传统的logstash具备这方面的能力,但由于其固有的缺陷无法避免,导致其处理性能较低,难于开发调试。

我们迫切需要一种通用的数据处理方式,实现从数据源到ES的全流程处理,最终需要达到:

  1. 通用性:接口丰富、有流程控制、数据类型转换、数据加工
  2. 易开发:便于快速开发调试
  3. 易管理:容易发现数据问题,性能瓶颈,清晰的流程
  4. 高性能:对数据处理有较高的吞吐量,较低的资源消耗

当然我们可以开发特定的Spark Streaming应用来实现数据到ES的流转,但从通用性、易用性方面来看,由于条件所限,这种方法代价较高,周期长,且性能、稳定性较难保证。

因此我们选用几种数据通用处理方法进行测试,对比各自的优缺点,发现他们所适应的场景:

  1. filebeat+ingest node
  2. logstash
  3. Apache Nifi
  4. Streamsets Data Collector(SDC)
  1. 测试简单Apache日志,普适于一般日志数据解析

a) 使用同一份Apache access log数据作为测试样本

b) 进行简单数据模式匹配,使用统一的COMBINEDAPACHELOG grok pattern对数据进行解析,将结果输入ES

  1. 测试复杂json结构的Twitter数据,对其字段进行转换、计算、筛选等处理,应对复杂、大结构的数据处理

a) 使用同一份Twitter的json数据作为测试样本

b) 由于原始的Twitter数据为非标准json数据,因此在对nifi进行处理之前先将其处理为标准的json array数据,生成数据文件tweet.nifi.json

  1. 每个测试重复三次,记录完成时间及相关运行指标
  2. 对于ES的性能数据收集采用kibana中自带的monitoring模块监控对应的index
  3. 对于操作系统的性能数据收集采用metricbeat,然后采用其自带的template在kibana中展现

3.1. 软硬件环境

  1. 虚拟机VMware® Workstation 10.0.1 build-1379776
  2. Ubuntu 16.04.1 LTS Linux version 4.4.0-59-generic
  3. 2 CPU/10G MEM
  4. ELK 6.2.2 + xpack + 2 nodes
  5. Apache Nifi 1.5.0
  6. SDC 3.1.0.0

3.2. 数据准备

1、测试所用Apache日志数据来源于www.secrepo.com

详见:

github.com/zhan-yl/ELK…

使用方式:

python3 download_data.py --start_date 2018-03-15 --output_folder ./data

将下载的日志数据整合到一个日志文件中:

-rw-rw-r-- 1 zhanyl zhanyl 169984631 3月 16 11:45 access.log

也可以使用日志生成器来生成数据:

github.com/kiritbasu/F…

2、测试所用Twitter数据来源于Twitter官网,通过Python脚本下载

详见:

github.com/zhan-yl/ELK…

使用方式:

python2.7 tweet.py @realDonaldTrump

python2.7 tweet.py @BBCWorld

python2.7 tweet.py @BBCBreaking

python2.7 tweet.py @TIME

python2.7 tweet.py @PDChina

python2.7 tweet.py @CNN

python2.7 tweet.py @CBSNews

python2.7 tweet.py @elastic

python2.7 tweet.py @golang

python2.7 tweet.py @Docker

python2.7 tweet.py @streamsets

生成的日志文件为:

-rw-rw-r-- 1 zhanyl zhanyl 277508582 4月 2 10:11 tweet.json

-rw-rw-r-- 1 zhanyl zhanyl 277561344 4月 2 15:56 tweet.nifi.json

3.3. 环境配置

3.3.1. Apache日志测试

3.3.1.1. Filebeat+ingest node

  1. 建立ingest的pipeline

详见:

github.com/zhan-yl/ELK…

导入:

curl -XPUT -H 'Content-Type: application/json' 'lzyl1:9200/_ingest/pipeline/secrepo_pipeline?pretty' -d @secrepo_pipeline.json -u elastic:zylelk

检查:

curl -XGET "http://lzyl1:9200/_ingest/pipeline?pretty&filter_path=secrepo_pipeline" -u elastic:zylelk

  1. Filebeat配置文件

详见:

github.com/zhan-yl/ELK…

3.3.1.2. Logstash

1、 建立配置文件

详见:

github.com/zhan-yl/ELK…

3.3.1.3. Apache nifi

1、 导入template

详见:

github.com/zhan-yl/ELK…

2、 说明

为了避免OutOfMemoryError和同时打开大量的文件,在流程里面采用了多个split text串联的方式予以解决。

同时在流程中未加入对于geo的地址解析。

在NIFI中对于Java heap和文件句柄的使用是一个需要谨慎处理的问题。

3.3.1.4. SDC

1、 导入pipeline

详见:

github.com/zhan-yl/ELK…

3.3.2. Twitter日志测试

3.3.2.1. Filebaet+ingest node

由于对数据的加工处理较为复杂,不对该类型进行测试

3.3.2.2. Logstash

虽然可以通过filter plugin进行相关类似的操作,但由于操作复杂、调试困难,不对该类型进行测试

3.3.2.3. Apache nifi

1. 导入template

详见:

github.com/zhan-yl/ELK…

2. 说明

由于下载的Twitter数据非标准json格式,因此在测试之前使用sed首先将其转换为标准json array数据,便于数据分割。

同时在流程中未加入对于geo的地址解析。

3.3.2.3. SDC

1. 导入pipeline

详见:

github.com/zhan-yl/ELK…

4.1. Apache日志测试

4.1.1. Filebeat+ingest node

4.1.1.1. 执行命令

初始化文件信息,便于重复执行

rm /var/lib/filebeat/registry

启动:

/usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat_secrepo.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat

4.1.1.2. 数据监控

1、 数据总量

897123

2、 完成时间

 

开始时间

结束时间

耗时(单位:分钟)

1

10:20

10:40

20

2

10:45

11:03

18

3

11:05

11:24

19

3、 性能图表

a) ES性能

b) 操作系统性能

c) ES数据样本

__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__{
  "_index": "ingest-pipeline",
  "_type": "doc",
  "_id": "5AZoLmIBmeYOsZrYcv72",
  "_version": 1,
  "_score": null,
  "_source": {
    "request": "/twitter-icon.png",
    "geoip": {
      "continent_name": "Asia",
      "city_name": "Attock",
      "country_iso_code": "PK",
      "region_name": "Punjab",
      "location": {
        "lon": 72.3873,
        "lat": 33.5937
      }
    },
    "offset": 8706062,
    "auth": "-",
    "ident": "-",
    "verb": "GET",
    "source": "/home/zhanyl/examples-master/Graph/apache_logs_security_analysis/data/access.log",
    "message": "103.255.6.250 - - [09/Mar/2018:02:24:56 -0800] \"GET /twitter-icon.png HTTP/1.1\" 200 27787 \"http://www.secrepo.com/\" \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0\" ",
    "referrer": "\"http://www.secrepo.com/\"",
    "@timestamp": "2018-03-09T10:24:56.000Z",
    "response": "200",
    "bytes": "27787",
    "clientip": "103.255.6.250",
    "beat": {
      "hostname": "zylxpack",
      "name": "zylxpack",
      "version": "6.2.2"
    },
    "httpversion": "1.1",
    "user_agent": {
      "major": "58",
      "minor": "0",
      "os": "Ubuntu",
      "name": "Firefox",
      "os_name": "Ubuntu",
      "device": "Other"
    }
  },
  "fields": {
    "@timestamp": [
      "2018-03-09T10:24:56.000Z"
    ]
  },
  "sort": [
    1520591096000
  ]
}
__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__

4.1.2. Logstash

4.1.2.1. 执行命令

初始化文件信息,便于重复执行:

rm /data/logstash/.sincedb

启动:

/usr/share/logstash/bin/logstash -f /home/zhanyl/examples-master/Graph/apache_logs_security_analysis/logstash/secrepo_logstash.conf --path.settings=/etc/logstash --path.data /data/logstash

4.1.2.2. 数据监控

1、 数据总量

897123

2、 完成时间

 

开始时间

结束时间

耗时(单位:分钟)

1

13:43

13:54

11

2

14:00

14:11

11

3

14:13

14:24

11

3、 性能图表

a) ES性能

b) 操作系统性能

c) ES数据样本

__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__{
  "_index": "secrepo-logstash",
  "_type": "doc",
  "_id": "vVswKGIBmeYOsZrYlb8C",
  "_version": 1,
  "_score": null,
  "_source": {
    "geoip": {
      "city_name": "Islamabad",
      "continent_code": "AS",
      "timezone": "Asia/Karachi",
      "longitude": 73.0113,
      "latitude": 33.6957,
      "country_name": "Pakistan",
      "region_code": "IS",
      "ip": "103.255.6.250",
      "postal_code": "44000",
      "country_code2": "PK",
      "region_name": "Islamabad Capital Territory",
      "country_code3": "PK",
      "location": {
        "lon": 73.0113,
        "lat": 33.6957
      }
    },
    "os_name": "Ubuntu",
    "device": "Other",
    "message": "103.255.6.250 - - [09/Mar/2018:02:24:56 -0800] \"GET /twitter-icon.png HTTP/1.1\" 200 27787 \"http://www.secrepo.com/\" \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0\" ",
    "path": "/home/zhanyl/examples-master/Graph/apache_logs_security_analysis/data/access.log",
    "major": "58",
    "@timestamp": "2018-03-09T10:24:56.000Z",
    "referrer": "\"http://www.secrepo.com/\"",
    "clientip": "103.255.6.250",
    "verb": "GET",
    "minor": "0",
    "os": "Ubuntu",
    "request": "/twitter-icon.png",
    "@version": "1",
    "host": "zylxpack",
    "build": "",
    "ident": "-",
    "auth": "-",
    "response": "200",
    "httpversion": "1.1",
    "bytes": "27787",
    "name": "Firefox"
  },
  "fields": {
    "@timestamp": [
      "2018-03-09T10:24:56.000Z"
    ]
  },
  "sort": [
    1520591096000
  ]
}
__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__

4.1.3. Apache nifi

4.1.3.1. 执行命令

初始化文件信息,便于重复执行:

启动:

选中相关的process,然后点击run

4.1.3.2. 数据监控

1、 数据总量

897123

2、 完成时间

 

开始时间

结束时间

耗时(单位:分钟)

1

14:08

14:34

26

2

14:38

15:07

29

3

15:08

15:37

29

3、 性能图表

a) ES性能

b) 操作系统性能

c) ES数据样本

__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__{
  "_index": "nifi",
  "_type": "nifi",
  "_id": "C5XrLGIBmeYOsZrYiU7w",
  "_version": 1,
  "_score": null,
  "_source": {
    "clientip": "103.255.6.250",
    "ident": "-",
    "auth": "-",
    "verb": "GET",
    "request": "/twitter-icon.png",
    "httpversion": "1.1",
    "rawrequest": null,
    "response": 200,
    "bytes": 27787,
    "referrer": "http://www.secrepo.com/",
    "agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0",
    "@timestamp": "2018-03-09T10:24:56.000Z"
  },
  "fields": {
    "@timestamp": [
      "2018-03-09T10:24:56.000Z"
    ]
  },
  "sort": [
    1520591096000
  ]
}
__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__

4.1.4. SDC

4.1.4.1. 执行命令

建立mapping。

注:如果@timestamp给出的是标准的timestamp字符串而不是timestamp类型则可以不建mapping,可以动态生成

PUT /sdc

{
  "mappings": {
    "sdc": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "geo": {
          "type": "geo_point"
        },
        "city": {
          "type": "text",
          "index": false
        }
      }
    }
  }
}

初始化文件信息,便于重复执行:

启动:

4.1.4.2. 数据监控

1、 数据总量

897070

缺少的53条记录是由于无法解析其地理位置而导致失败,如:

clientip :193.200.150.82

clientip :193.200.150.152

Address '91.197.234.102'

Address '103.234.188.37'

2、 完成时间

 

开始时间

结束时间

耗时(单位:分钟)

1

16:13

16:29

16

2

16:32

16:43

11

3

16:45

16:59

14

在第三次测试中出现exception,但并未中断处理

详见:

github.com/zhan-yl/ELK…

3、 性能图表

a) ES性能

b) 操作系统性能

c) ES数据样本

__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__{
  "_index": "sdc",
  "_type": "sdc",
  "_id": "Wur8LWIBmeYOsZrYnL-S",
  "_version": 1,
  "_score": null,
  "_source": {
    "request": "/twitter-icon.png",
    "agent": "\"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0\"",
    "auth": "-",
    "ident": "-",
    "verb": "GET",
    "referrer": "\"http://www.secrepo.com/\"",
    "response": 200,
    "bytes": 27787,
    "clientip": "103.255.6.250",
    "httpversion": "1.1",
    "rawrequest": null,
    "geo": {
      "lat": 33.6957,
      "lon": 73.0113
    },
    "@timestamp": 1520591096000,
    "city": "Islamabad"
  },
  "fields": {
    "@timestamp": [
      "2018-03-09T10:24:56.000Z"
    ]
  },
  "sort": [
    1520591096000
  ]
}
__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__

4.2. Twitter日志测试

4.2.1. Apache nifi

4.2.1.1. 执行命令

初始化文件信息,便于重复执行:

启动

选中相关的process,然后点击run

4.2.1.2. 数据监控

1、 数据总量

52759

2、 完成时间

 

开始时间

结束时间

耗时(单位:分钟)

1

20:12

20:17

5

2

20:18

20:22

4

3

20:23

20:27

4

3、 性能图表

a) ES性能

b) 操作系统性能

c) ES数据样本

__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__{
  "_index": "twitter-nifi",
  "_type": "doc",
  "_id": "u7NThmIBROKaR930KYYi",
  "_version": 1,
  "_score": null,
  "_source": {
    "geo": null,
    "id": 980626639842357200,
    "id_str": "980626639842357249",
    "lang": "en",
    "user_mentions": [],
    "favorite_count": 23,
    "favorited": false,
    "retweet_count": 25,
    "text": "Cops: Man arrested with weapons cache, bump stock claimed secret government mission https://t.co/XaHdZOmV8z https://t.co/N3ca4nAUSv",
    "user": {
      "contributors_enabled": false,
      "created_at": "Thu Jun 05 00:54:31 +0000 2008",
      "default_profile": false,
      "default_profile_image": false,
      "description": "Your source for original reporting and trusted news.",
      "entities": {
        "description": {
          "urls": []
        },
        "url": {
          "urls": [
            {
              "display_url": "CBSNews.com",
              "expanded_url": "http://CBSNews.com",
              "indices": [
                0,
                23
              ],
              "url": "https://t.co/VGut7r2Vg5"
            }
          ]
        }
      },
      "favourites_count": 270,
      "follow_request_sent": false,
      "followers_count": 6490476,
      "following": false,
      "friends_count": 431,
      "geo_enabled": false,
      "has_extended_profile": false,
      "id": 15012486,
      "id_str": "15012486",
      "is_translation_enabled": true,
      "is_translator": false,
      "lang": "en",
      "listed_count": 47812,
      "location": "New York, NY",
      "name": "CBS News",
      "notifications": false,
      "profile_background_color": "D9DADA",
      "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/736106551/37bf1f784305fe4a9c7e9105772c6e1a.jpeg",
      "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/736106551/37bf1f784305fe4a9c7e9105772c6e1a.jpeg",
      "profile_background_tile": false,
      "profile_banner_url": "https://pbs.twimg.com/profile_banners/15012486/1519827973",
      "profile_image_url": "http://pbs.twimg.com/profile_images/645966750941626368/d0Q4voGK_normal.jpg",
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/645966750941626368/d0Q4voGK_normal.jpg",
      "profile_link_color": "B12124",
      "profile_sidebar_border_color": "FFFFFF",
      "profile_sidebar_fill_color": "EAEDF0",
      "profile_text_color": "000000",
      "profile_use_background_image": true,
      "protected": false,
      "screen_name": "CBSNews",
      "statuses_count": 168688,
      "time_zone": "Eastern Time (US & Canada)",
      "translator_type": "none",
      "url": "https://t.co/VGut7r2Vg5",
      "utc_offset": -14400,
      "verified": true
    },
    "created_at": "Mon Apr 02 02:03:04 +0000 2018",
    "place": null,
    "total_count": 48,
    "@timestamp": "2018-04-02T02:03:04.000Z"
  },
  "fields": {
    "@timestamp": [
      "2018-04-02T02:03:04.000Z"
    ]
  },
  "sort": [
    1522634584000
  ]
}
__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__

4.2.2. SDC

4.2.2.1. 执行命令

初始化文件信息,便于重复执行:

启动:

4.2.2.2. 数据监控

1、 数据总量

52759

2、 完成时间

 

开始时间

结束时间

耗时(单位:分钟)

1

21:00

21:05

5

2

21:06

21:12

6

3

21:13

21:18

5

3、 性能图表

d) ES性能

e) 操作系统性能

f) ES数据样本

__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__{
  "_index": "twitter-sdc",
  "_type": "doc",
  "_id": "VbVuhmIBROKaR930GV0_",
  "_version": 1,
  "_score": null,
  "_source": {
    "created_at": "Mon Apr 02 02:03:04 +0000 2018",
    "entities": {
      "user_mentions": []
    },
    "favorite_count": 23,
    "favorited": false,
    "geo": null,
    "id": 980626639842357200,
    "id_str": "980626639842357249",
    "lang": "en",
    "place": null,
    "retweet_count": 25,
    "text": "Cops: Man arrested with weapons cache, bump stock claimed secret government mission https://t.co/XaHdZOmV8z https://t.co/N3ca4nAUSv",
    "user": {
      "description": "Your source for original reporting and trusted news.",
      "favourites_count": 270,
      "followers_count": 6490476,
      "friends_count": 431,
      "id": 15012486,
      "location": "New York, NY",
      "name": "CBS News"
    },
    "@timestamp": "2018-04-02T10:03:04.000+08",
    "lat": null,
    "lon": null,
    "total_count": 48
  },
  "fields": {
    "@timestamp": [
      "2018-04-02T02:03:04.000Z"
    ]
  },
  "sort": [
    1522634584000
  ]
}
__Fri May 04 2018 11:19:53 GMT+0800 (CST)____Fri May 04 2018 11:19:53 GMT+0800 (CST)__

5.1. 数据结果

在最终数据进入ES以后,虽然记录数相等,但是数据的大小存在差异,所记录的内容也存在差异,这也是导致执行时间存在差异的原因

5.2. 对比

5.2.1. Filebeat+ingest node

1、 优点

a) 比较轻量级,对系统资源消耗较少

b) Ingest node内置于ES集群内部,无需额外部署

c) 通过简单的json语句即可实现pipeline处理

2、 缺点

a) 所支持的功能有限,仅能对简单数据进行处理。能够处理的功能集,现阶段仅为logstash的子集

b) Ingest node本身不具备自主pull数据的功能,需要其他工具将数据写入。如果部署在kafka后端,则必须部署能够配合其数据写入的工具

c) 不支持可视化

5.2.2. Logstash

1、 优点

a) 有丰富的plugin进行各种数据加工、处理

b) 有大量的接口支持,技术较为成熟

2、 缺点

a) 开发、调试、跟踪困难,无法可视化呈现

b) 对于较为复杂的数据结构处理较难实现,会依赖于嵌套ruby来处理

c) 无法进行可视化监控发现性能瓶颈,新的kibana pipeline在一定程度上缓解该问题

5.2.3. Apache nifi

1、 优点

a) 可图形化编辑、监控整个处理流程

b) 可进行单步调试,在上一个处理环节得出正确结果以后再进入下一个处理环节。可以通过Data Provenance追踪数据的完整处理过程。

c) 对数据有back pressure机制

d) 在运行过程中可随时修改流程中的各个处理环节,调整资源使用量而不必中断整个流程

e) 有丰富的接口及数据处理机制(expression language),数据类型转换机制

f) 拥有rest接口,通过该接口可进行进一步应用开发

g) 由于使用文件作为载体,对所处理数据的大小基本无限制

h) 对于大数据结构、批量数据处理具有一定优势

2、 缺点

a) 由于使用文件,文件需要落地并频繁地打开、关闭,性能较低,需要依靠较高性能的磁盘设备

b) 同时对于内存的消耗较大,由其是生成大量attribute的情况下。极易造成OOM。

c) 对于能够同时打开文件的个数要求也较严格,需要设置较高的fileno

5.2.4. SDC

1、 优点

a) 可视化处理、对各环节都有监控指标展现

b) 可预览数据处理过程、检查数据处理结果

c) 拥有多种处理process和package,可形成复杂的处理pipeline,可使用expression language、groovy、JavaScript、jyphon等

d) 拥有rest接口,直接调用,也通过该接口可进行进一步应用开发

e) 自带异常告警机制

f) 由于在内存中按照批量进行处理,因此处理性能较高

g) 对于小数据结构、实时数据处理具有一定优势

2、 缺点

a) 由于在内存中进行数据处理,为避免OOM,对处理的event大小有限制,其buffer大小限制为1048576字节,超出部分将会被截断

b) 不能进行单步调用调试,无法在pipeline运行过程中干预其处理

作者介绍

詹玉林,中国民生银行系统管理中心大数据工程师,曾作为研发工程师开发过银行核心系统,IBM数据库支持工程师,现关注于大数据的实时解决方案。

如果Google已经不能解决你的问题,如果你想获取顶级架构师的非公开实践,如果你想学习100+海内外的私密架构案例,戳这里,即刻8折报名 ArchSummit全球架构师峰会[深圳站]